Skip to content

Commit

Permalink
Merge pull request #89 from instana/instrument_grpc
Browse files Browse the repository at this point in the history
Add instrumentation for google.golang.org/grpc library
  • Loading branch information
Andrew Slotin authored Mar 18, 2020
2 parents 211434f + d236a76 commit cd9ac16
Show file tree
Hide file tree
Showing 16 changed files with 1,450 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ matrix:
include:
- go: tip
- go: 1.8.x
env: EXCLUDE_DIRS="./instrumentation/instagrpc"
- go: 1.9.x
- go: 1.10.x
- go: 1.11.x
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
MODULES = $(shell find . -name go.mod -exec dirname {} \;)

MODULES = $(filter-out $(EXCLUDE_DIRS), $(shell find . -name go.mod -exec dirname {} \;))
LINTER ?= $(shell go env GOPATH)/bin/golangci-lint

ifdef RUN_LINTER
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ The provided `parentSpan` is the incoming request from the request handler (see

The request is, after injection, executing using the provided `http.Client` instance. Like the normal `(*http.Client).Do()` operation, the call will return a `http.Response` instance or an error proving information of the failure reason.

### GRPC servers and clients

[`github.com/instana/go-sensor/instrumentation/instagrpc`](./instrumentation/instagrpc) provides both unary and stream interceptors to instrument GRPC servers and clients that use `google.golang.org/grpc`.

## Sensor

To use sensor only without tracing ability, import the `instana` package and run
Expand Down
Binary file removed example/rpc/rpc
Binary file not shown.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ require (
github.com/looplab/fsm v0.1.0
github.com/opentracing/basictracer-go v1.0.0
github.com/opentracing/opentracing-go v1.1.0
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 h1:Ao/3l156eZf2AW5wK8a7/smtodRU+gha3+BeqJ69lRk=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
21 changes: 21 additions & 0 deletions instrumentation/instagrpc/LICENSE.md
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.
69 changes: 69 additions & 0 deletions instrumentation/instagrpc/README.md
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
128 changes: 128 additions & 0 deletions instrumentation/instagrpc/client.go
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
}
Loading

0 comments on commit cd9ac16

Please sign in to comment.