-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add gRPC client handler. #95
Merged
Merged
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
882b748
Add gRPC client handler.
anuraaga d8db0ec
Add extension point for adding tags, more test coverage.
anuraaga 6bfafab
Newlines
anuraaga 2f86054
Newline
anuraaga 27c53a5
Assert span name
anuraaga 08b9860
Fix endpoint name
anuraaga e506b81
Move test service into test suite file.
anuraaga 5a3d67e
Cleanup
anuraaga 8760fa0
Cleanup
anuraaga 8773577
Formatting
anuraaga 990fb01
Simplify rs.Error check since nil is handled correctly and add test f…
02d7a8a
Remove 1.8 from travis
7c9cb05
Remove 1.8 check from Makefile
98445f7
Don't introduce unnecessary interface and other lint fixes.
095b9fc
Remove dot imports.
f146583
Fix imports
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
module github.com/openzipkin/zipkin-go | ||
|
||
require ( | ||
cloud.google.com/go v0.33.1 // indirect | ||
github.com/Shopify/sarama v1.19.0 | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/eapache/go-resiliency v1.1.0 // indirect | ||
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect | ||
github.com/eapache/queue v1.1.0 // indirect | ||
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 // indirect | ||
github.com/golang/protobuf v1.2.0 | ||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect | ||
github.com/gorilla/context v1.1.1 // indirect | ||
github.com/gorilla/mux v1.6.2 | ||
github.com/pierrec/lz4 v2.0.5+incompatible // indirect | ||
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a // indirect | ||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3 // indirect | ||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a | ||
golang.org/x/oauth2 v0.0.0-20181120190819-8f65e3013eba // indirect | ||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect | ||
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b // indirect | ||
golang.org/x/tools v0.0.0-20181122213734-04b5d21e00f1 // indirect | ||
google.golang.org/appengine v1.3.0 // indirect | ||
google.golang.org/genproto v0.0.0-20181109154231-b5d43981345b // indirect | ||
google.golang.org/grpc v1.16.0 | ||
honnef.co/go/tools v0.0.0-20180920025451-e3ad64cb4ed3 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/stats" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/openzipkin/zipkin-go" | ||
"github.com/openzipkin/zipkin-go/model" | ||
"github.com/openzipkin/zipkin-go/propagation/b3" | ||
) | ||
|
||
type ClientHandler interface { | ||
stats.Handler | ||
} | ||
|
||
type clientHandler struct { | ||
tracer *zipkin.Tracer | ||
} | ||
|
||
func NewClientHandler(tracer *zipkin.Tracer) ClientHandler { | ||
return &clientHandler{ | ||
tracer, | ||
} | ||
} | ||
|
||
// HandleConn exists to satisfy gRPC stats.Handler. | ||
func (c *clientHandler) HandleConn(ctx context.Context, cs stats.ConnStats) { | ||
// no-op | ||
} | ||
|
||
// TagConn exists to satisfy gRPC stats.Handler. | ||
func (c *clientHandler) TagConn(ctx context.Context, cti *stats.ConnTagInfo) context.Context { | ||
// no-op | ||
return ctx | ||
} | ||
|
||
// HandleRPC implements per-RPC tracing and stats instrumentation. | ||
func (c *clientHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) { | ||
span := zipkin.SpanFromContext(ctx) | ||
switch rs := rs.(type) { | ||
case *stats.Begin: | ||
span.Tag("grpctrace.failfast", strconv.FormatBool(rs.FailFast)) | ||
case *stats.InPayload: | ||
span.Annotate(time.Now(), "grpctrace.message_receive") | ||
case *stats.OutPayload: | ||
span.Annotate(time.Now(), "grpctrace.message_sent") | ||
case *stats.End: | ||
if rs.Error != nil { | ||
s, ok := status.FromError(rs.Error) | ||
if ok { | ||
zipkin.TagError.Set(span, s.Message()) | ||
} else { | ||
zipkin.TagError.Set(span, rs.Error.Error()) | ||
} | ||
} | ||
span.Finish() | ||
} | ||
} | ||
|
||
// TagRPC implements per-RPC context management. | ||
func (c *clientHandler) TagRPC(ctx context.Context, rti *stats.RPCTagInfo) context.Context { | ||
name := spanName(rti) | ||
span, ctx := c.tracer.StartSpanFromContext(ctx, name, zipkin.Kind(model.Client)) | ||
md, ok := metadata.FromOutgoingContext(ctx) | ||
if !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit pick, what about doing it the other way around? |
||
md = metadata.New(nil) | ||
} else { | ||
md = md.Copy() | ||
} | ||
_ = b3.InjectGRPC(&md)(span.Context()) | ||
ctx = metadata.NewOutgoingContext(ctx, md) | ||
return ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"github.com/openzipkin/zipkin-go" | ||
"github.com/openzipkin/zipkin-go/reporter/recorder" | ||
"net" | ||
"testing" | ||
|
||
"google.golang.org/grpc" | ||
|
||
service "github.com/openzipkin/zipkin-go/proto/testing" | ||
) | ||
|
||
type testHelloService struct{} | ||
|
||
func (s *testHelloService) Hello(context.Context, *service.HelloRequest) (*service.HelloResponse, error) { | ||
return &service.HelloResponse{ | ||
Payload: "World", | ||
}, nil | ||
} | ||
|
||
func TestGRPCClient(t *testing.T) { | ||
lis, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
t.Fatalf("failed to listen: %v", err) | ||
} | ||
grpcServer := grpc.NewServer() | ||
service.RegisterHelloServiceServer(grpcServer, &testHelloService{}) | ||
go func() { | ||
grpcServer.Serve(lis) | ||
}() | ||
defer grpcServer.Stop() | ||
|
||
reporter := recorder.NewReporter() | ||
defer reporter.Close() | ||
|
||
ep, _ := zipkin.NewEndpoint("httpClient", "") | ||
tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(ep)) | ||
if err != nil { | ||
t.Fatalf("unable to create tracer: %+v", err) | ||
} | ||
|
||
conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure(), grpc.WithStatsHandler(NewClientHandler(tracer))) | ||
if err != nil { | ||
t.Fatalf("Could not connect to gRPC server: %v", err) | ||
} | ||
defer conn.Close() | ||
|
||
client := service.NewHelloServiceClient(conn) | ||
|
||
_, err = client.Hello(context.Background(), &service.HelloRequest{Payload: "Hello"}) | ||
if err != nil { | ||
t.Fatalf("Error from gRPC server: %v", err) | ||
} | ||
|
||
spans := reporter.Flush() | ||
if len(spans) == 0 { | ||
t.Errorf("Span Count want 1+, have 0") | ||
} | ||
|
||
span := tracer.StartSpan("ParentSpan") | ||
ctx := zipkin.NewContext(context.Background(), span) | ||
client.Hello(ctx, &service.HelloRequest{Payload: "Hello"}) | ||
|
||
spans = reporter.Flush() | ||
if len(spans) == 0 { | ||
t.Errorf("Span Count want 1+, have 0") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
Package grpc contains several gRPC handlers which can be used for instrumenting calls with Zipkin. | ||
*/ | ||
package grpc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package grpc | ||
|
||
import ( | ||
"strings" | ||
|
||
"google.golang.org/grpc/stats" | ||
) | ||
|
||
func spanName(rti *stats.RPCTagInfo) string { | ||
name := strings.TrimPrefix(rti.FullMethodName, "/") | ||
name = strings.Replace(name, "/", ".", -1) | ||
return name | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me again being nit picker but I believe we could make this more readable by declaring the type of the variable
span
before so we do:and then it is clear we override
ctx
. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit picks are welcome :)