Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

gRPC integration #12

Merged
merged 30 commits into from
Sep 22, 2017

Conversation

acetechnologist
Copy link
Contributor

Added grpc handlers.
Added sumOfSquaredDeviation to aggregation distribution (to mimic stackdriver) and simplified its implementation to always include the oldest bucket.

…e and aggregator. Also added equality as an interface to aggregationValue
…ains()... Also renamed some methods to be more succint and fixed some documentation issues
…ntually support conversions to other formats
package stats

import (
"context"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gRPC-go still imports "golang.org/x/net/context" (grpc/grpc-go#711).
It will be a massive incompatible API change for all users using <=go1.8 if we change that.

go treats func("context".Context) and func("golang.org/x/net/context".Context) as two different types, which means the stats handler here won't work (it doesn't compile, with <=go1.8).

go1.9 introduced a new feature called type alias, which makes "golang.org/x/net/context" and "context" the same type. So, this actually works fine with >=go1.9.

The solution I can think of right now is to import "golang.org/x/net/context" in the stats package.

Or, if we decide that the stats package only supports users with >=go1.9, the current code will be fine.

Copy link
Contributor Author

@acetechnologist acetechnologist Sep 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is our first release, I think we should avoid creating unnecessary confusion and say that we support >= go1.9 if using stats with grpc.
Wondering if @rakyll have a different opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it more. I think it is better to use "golang.org/x/net/context" so we can get more adoption and since once everybody moves beyond 1.8 it will be easy to move to "context".Context


// ClientHandler is the type implementing the "google.golang.org/grpc/stats.Handler"
// interface to process lifecycle events from the GRPC client.
type ClientHandler struct{}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexport this and add an exported function instead?

Copy link
Contributor Author

@acetechnologist acetechnologist Sep 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand your comment. You mean adding an exported "constructor" for it: eg func NewClientHandler() stats.Handler {...}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

ctx = metadata.NewOutgoingContext(ctx, metadata.Join(md, metadata.Pairs(tagsKey, string(encoded))))

tsb := tags.NewTagSetBuilder(ts)
tsb.UpsertString(keyService, serviceName)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this function return a TagSetBuilder if it's called to update the builder?

Copy link
Contributor Author

@acetechnologist acetechnologist Sep 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it easier to use the shortcut version when modifying multiple tags. It returns the same TagSetBuilder. It is just a syntactic sugar to allow for the short form:
tags.NewTagSetBuilder(ts).UpsertString(keyService, serviceName).UpsertString(key2, value2).AddString(key3, value3)...

@@ -0,0 +1,75 @@
// Copyright 2017, OpenCensus Authors

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files are Copyright 2017 Google Inc....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Thank you. Fixed now.

"github.com/golang/glog"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/stats"
/*

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// tagsKey is the metadata key used to identify both the stats tags in the GRPC
// context metadata, as well as the RpcServerStats info sent back from the
// server to the client in the GRPC context metadata.
const tagsKey = "grpc-tags-bin"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metadata keys start with grpc- are actually reserved, so we should avoid having a grpc- prefixed key explicitly in the code...

To make this work, we added functions to set/get trace info: https://github.com/grpc/grpc-go/blob/master/stats/stats.go#L269.

Please use the new functions instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

return &AggregationDistributionValue{
countPerBucket: countPerBucket,
bounds: bounds,
count: count,
min: min,
max: max,
sum: sum,
mean: mean,
sumOfSquaredDeviation: sumOfSquaredDeviation,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not formatted correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

ret.sum = ret.min
ret.incrementBucketCount(ret.min)
return ret
ret := newAggregationDistributionValue(a.bounds)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you miss anything? Parameter fraction is not used in this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is part of the interface signature: "type AggregationValue interface".
We decided that for this specific aggregation we will not use the "fraction" for simplicity

…for Go1.9 to integrate with libraries still using golang.org/x/net/context.Context. Fixed the license headings to refer to opencensus instead of google.
Copy link

@menghanl menghanl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two nits, otherwise LGTM.

README.md Outdated
@@ -20,7 +20,8 @@ To install this package, you need to install Go and setup your Go workspace on y
$ go get -u github.com/census-instrumentation/opencensus-go

## Prerequisites
This requires Go 1.8 or later as it uses the convenience function sort.Slice(...) in few places.
This requires Go 1.8 or later as it uses the convenience function sort.Slice(...) introduced in Go 1.8.
It also uses "golang.org/x/net/context".Context introduced in Go 1.7 heavily. Therefore, in order to integrate with any library still using "golang.org/x/net/context".Context, Go 1.9 or higher is required to take advantage of type aliasing.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to keep this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No we don't. Deleted it.

package stats

import (
"golang.org/x/net/context"
Copy link

@menghanl menghanl Sep 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goimports would complain about the order of the imported packages.

We do those checks on travis in gRPC: https://github.com/menghanl/grpc-go/blob/master/vet.sh#L51-L54

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Fixed.

@acetechnologist acetechnologist merged commit 2d3c093 into census-instrumentation:master Sep 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants