Skip to content

Commit

Permalink
Add a SpanDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
bhs committed Sep 10, 2016
1 parent f0aea2e commit bad9516
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
3 changes: 3 additions & 0 deletions go/otgrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ func OpenTracingClientInterceptor(tracer opentracing.Tracer, optFuncs ...Option)
clientSpan.LogEventWithPayload("gRPC error", err)
ext.Error.Set(clientSpan, true)
}
if otgrpcOpts.decorator != nil {
otgrpcOpts.decorator(clientSpan, method, req, resp, err)
}
return err
}
}
19 changes: 19 additions & 0 deletions go/otgrpc/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package otgrpc

import "github.com/opentracing/opentracing-go"

// Option instances may be used in OpenTracing(Server|Client)Interceptor
// initialization.
//
Expand All @@ -15,11 +17,28 @@ func LogPayloads() Option {
}
}

// SpanDecoratorFunc provides an (optional) mechanism for otgrpc users to add
// arbitrary tags/logs/etc to the opentracing.Span associated with client
// and/or server RPCs.
type SpanDecoratorFunc func(
span opentracing.Span,
method string,
req, resp interface{},
grpcError error)

// SpanDecorator binds a function that decorates gRPC Spans.
func SpanDecorator(decorator SpanDecoratorFunc) Option {
return func(o *options) {
o.decorator = decorator
}
}

// The internal-only options struct. Obviously overkill at the moment; but will
// scale well as production use dictates other configuration and tuning
// parameters.
type options struct {
logPayloads bool
decorator SpanDecoratorFunc
}

// newOptions returns the default options.
Expand Down
2 changes: 1 addition & 1 deletion go/otgrpc/package.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// otgrpc provides OpenTracing support for any gRPC client or server in Go.
// Package otgrpc provides OpenTracing support for any gRPC client or server.
//
// See [go/otgrpc/README.md](https://github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc/README.md)
package otgrpc
17 changes: 10 additions & 7 deletions go/otgrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,28 @@ func OpenTracingServerInterceptor(tracer opentracing.Tracer, optFuncs ...Option)
// don't know where to put such an error and must rely on Tracer
// implementations to do something appropriate for the time being.
}
span := tracer.StartSpan(
serverSpan := tracer.StartSpan(
info.FullMethod,
ext.RPCServerOption(spanContext),
gRPCComponentTag,
)
defer span.Finish()
defer serverSpan.Finish()

ctx = opentracing.ContextWithSpan(ctx, span)
ctx = opentracing.ContextWithSpan(ctx, serverSpan)
if otgrpcOpts.logPayloads {
span.LogEventWithPayload("gRPC request", req)
serverSpan.LogEventWithPayload("gRPC request", req)
}
resp, err = handler(ctx, req)
if err == nil {
if otgrpcOpts.logPayloads {
span.LogEventWithPayload("gRPC response", resp)
serverSpan.LogEventWithPayload("gRPC response", resp)
}
} else {
ext.Error.Set(span, true)
span.LogEventWithPayload("gRPC error", err)
ext.Error.Set(serverSpan, true)
serverSpan.LogEventWithPayload("gRPC error", err)
}
if otgrpcOpts.decorator != nil {
otgrpcOpts.decorator(serverSpan, info.FullMethod, req, resp, err)
}
return resp, err
}
Expand Down

0 comments on commit bad9516

Please sign in to comment.