-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from instana/instrument_grpc
Add instrumentation for google.golang.org/grpc library
- Loading branch information
Showing
16 changed files
with
1,450 additions
and
4 deletions.
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
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
Binary file not shown.
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
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2016 Instana | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,69 @@ | ||
Instana instrumentation for go-grpc library | ||
=========================================== | ||
|
||
This module contains instrumentation code for GRPC servers and clients that use `google.golang.org/grpc` library. | ||
|
||
[![GoDoc](https://img.shields.io/static/v1?label=godoc&message=reference&color=blue)][godoc] | ||
|
||
Installation | ||
------------ | ||
|
||
Unlike the Instana Go sensor, GRPC instrumentation module requires Go v1.9+ which is the minimal version for `google.golang.org/grpc`. | ||
|
||
```bash | ||
$ go get github.com/instana/go-sensor/instrumentation/instagrpc | ||
``` | ||
|
||
Usage | ||
----- | ||
|
||
For detailed usage example see [the documentation][godoc] or [`example_test.go`](./example_test.go). | ||
|
||
This instrumentation requires an `opentracing.Tracer` to initialize spans and handle the trace context propagation. | ||
You can create a new instance of Instana tracer using `instana.NewTracer()`. | ||
|
||
### Instrumenting a server | ||
|
||
To instrument your GRPC server instance include `instagrpc.UnaryServerInterceptor()` and `instagrpc.StreamServerInterceptor()` | ||
into the list of server options passed to `grpc.NewServer()`. These interceptors will use the provided `instana.Sensor` to | ||
handle the OpenTracing headers, start a new span for each incoming request and inject it into the handler: | ||
|
||
```go | ||
// initialize a new tracer instance | ||
sensor := instana.NewSensor("my-server") | ||
|
||
// instrument the server | ||
srv := grpc.NewServer( | ||
grpc.UnaryInterceptor(instagrpc.UnaryServerInterceptor(sensor)), | ||
grpc.StreamInterceptor(instagrpc.StreamServerInterceptor(sensor)), | ||
// ... | ||
) | ||
``` | ||
|
||
The parent span can be than retrieved inside the handler using `instana.ContextFromSpan()`: | ||
|
||
```go | ||
func (s MyServer) SampleCall(ctx context.Context, req *MyRequest) (*MyResponse, error) { | ||
parentSpan, ok := instana.SpanFromContext(ctx) | ||
// ... | ||
} | ||
``` | ||
|
||
### Instrumenting a client | ||
|
||
Similar to the server instrumentation, to instrument a GRPC client add `instagrpc.UnaryClientInterceptor()` and | ||
`instagrpc.StreamClientInterceptor()` into the list of dial options passed to the `grpc.Dial()` call. The interceptor | ||
will inject the trace context into each outgoing request made with this connection: | ||
|
||
```go | ||
conn, err := grpc.Dial( | ||
serverAddr, | ||
grpc.WithUnaryInterceptor(instagrpc.UnaryClientInterceptor(sensor)), | ||
grpc.WithStreamInterceptor(instagrpc.StreamClientInterceptor(sensor)), | ||
// ... | ||
) | ||
``` | ||
|
||
If the context contains an active span stored using `instana.ContextWithSpan()`, the tracer of this span will be used instead. | ||
|
||
[godoc]: https://pkg.go.dev/github.com/instana/go-sensor/instrumentation/instagrpc |
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,128 @@ | ||
// +build go1.9 | ||
|
||
package instagrpc | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net" | ||
"net/http" | ||
|
||
instana "github.com/instana/go-sensor" | ||
ot "github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/ext" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
// UnaryClientInterceptor returns a tracing interceptor to be used in grpc.Dial() calls. | ||
// It injects Instana OpenTracing headers into outgoing unary requests to ensure trace propagation | ||
// throughout the call. | ||
// If the server call results with an error, its message will be attached to the span logs. | ||
func UnaryClientInterceptor(sensor *instana.Sensor) grpc.UnaryClientInterceptor { | ||
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, callOpts ...grpc.CallOption) error { | ||
sp := startClientSpan(ctx, cc.Target(), method, "unary", sensor.Tracer()) | ||
defer sp.Finish() | ||
|
||
if err := invoker(outgoingTracingContext(ctx, sp), method, req, reply, cc, callOpts...); err != nil { | ||
addRPCError(sp, err) | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// StreamClientInterceptor returns a tracing interceptor to be used in grpc.Dial() calls. | ||
// It injects Instana OpenTracing headers into outgoing stream requests to ensure trace propagation | ||
// throughout the call. The span is finished as soon as server closes the stream or returns an error. | ||
// Any error occurred during the request is attached to the span logs. | ||
func StreamClientInterceptor(sensor *instana.Sensor) grpc.StreamClientInterceptor { | ||
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) { | ||
|
||
sp := startClientSpan(ctx, cc.Target(), method, "stream", sensor.Tracer()) | ||
stream, err := streamer(outgoingTracingContext(ctx, sp), desc, cc, method, opts...) | ||
if err != nil { | ||
addRPCError(sp, err) | ||
sp.Finish() | ||
|
||
return nil, err | ||
} | ||
|
||
return wrappedClientStream{ | ||
ClientStream: stream, | ||
Span: sp, | ||
ServerStreams: desc.ServerStreams, | ||
}, nil | ||
} | ||
} | ||
|
||
func startClientSpan(ctx context.Context, target, method, callType string, tracer ot.Tracer) ot.Span { | ||
host, port, err := net.SplitHostPort(target) | ||
if err != nil { | ||
// TODO: log this error using the sensor logger | ||
// log.Printf("INFO: failed to extract server host and port from request metadata: %s", err) | ||
|
||
// take our best guess and use :authority as a host if the net.SplitHostPort() fails to parse | ||
host, port = target, "" | ||
} | ||
|
||
opts := []ot.StartSpanOption{ | ||
ext.SpanKindRPCClient, | ||
ot.Tags{ | ||
"rpc.flavor": "grpc", | ||
"rpc.call": method, | ||
"rpc.call_type": callType, | ||
"rpc.host": host, | ||
"rpc.port": port, | ||
}, | ||
} | ||
|
||
if parentSpan, ok := instana.SpanFromContext(ctx); ok { | ||
tracer = parentSpan.Tracer() // use the same tracer as the parent span does | ||
opts = append(opts, ot.ChildOf(parentSpan.Context())) | ||
} | ||
|
||
return tracer.StartSpan("rpc-client", opts...) | ||
} | ||
|
||
func outgoingTracingContext(ctx context.Context, span ot.Span) context.Context { | ||
// gather opentracing headers and inject them into request metadata omitting empty values | ||
md, ok := metadata.FromOutgoingContext(ctx) | ||
if !ok { | ||
md = metadata.New(nil) | ||
} | ||
|
||
headers := http.Header{} | ||
span.Tracer().Inject(span.Context(), ot.HTTPHeaders, ot.HTTPHeadersCarrier(headers)) | ||
|
||
for k := range headers { | ||
if v := headers.Get(k); v != "" { | ||
md.Set(k, v) | ||
} | ||
} | ||
|
||
return metadata.NewOutgoingContext(ctx, md) | ||
} | ||
|
||
type wrappedClientStream struct { | ||
grpc.ClientStream | ||
Span ot.Span | ||
ServerStreams bool | ||
} | ||
|
||
func (cs wrappedClientStream) RecvMsg(m interface{}) error { | ||
err := cs.ClientStream.RecvMsg(m) | ||
if err != nil { | ||
if err != io.EOF { | ||
addRPCError(cs.Span, err) | ||
} | ||
} | ||
|
||
if err != nil || !cs.ServerStreams { | ||
cs.Span.Finish() | ||
} | ||
|
||
return err | ||
} |
Oops, something went wrong.