OpenTracing breaking API change: SpanContext
Bas van Beek
7 years ago
36 | 36 | return func(next endpoint.Endpoint) endpoint.Endpoint { |
37 | 37 | return func(ctx context.Context, request interface{}) (interface{}, error) { |
38 | 38 | parentSpan := opentracing.SpanFromContext(ctx) |
39 | clientSpan := tracer.StartSpanWithOptions(opentracing.StartSpanOptions{ | |
40 | OperationName: operationName, | |
41 | Parent: parentSpan, // may be nil | |
42 | }) | |
39 | clientSpan := tracer.StartSpan( | |
40 | operationName, | |
41 | opentracing.ChildOf(parentSpan.Context()), | |
42 | ) | |
43 | 43 | defer clientSpan.Finish() |
44 | 44 | otext.SpanKind.Set(clientSpan, otext.SpanKindRPCClient) |
45 | 45 | ctx = opentracing.ContextWithSpan(ctx, clientSpan) |
4 | 4 | "strings" |
5 | 5 | |
6 | 6 | "github.com/opentracing/opentracing-go" |
7 | "github.com/opentracing/opentracing-go/ext" | |
7 | 8 | "golang.org/x/net/context" |
8 | 9 | "google.golang.org/grpc/metadata" |
9 | 10 | |
19 | 20 | return func(ctx context.Context, md *metadata.MD) context.Context { |
20 | 21 | if span := opentracing.SpanFromContext(ctx); span != nil { |
21 | 22 | // There's nothing we can do with an error here. |
22 | if err := tracer.Inject(span, opentracing.TextMap, metadataReaderWriter{md}); err != nil { | |
23 | if err := tracer.Inject(span.Context(), opentracing.TextMap, metadataReaderWriter{md}); err != nil { | |
23 | 24 | logger.Log("err", err) |
24 | 25 | } |
25 | 26 | } |
36 | 37 | // The logger is used to report errors and may be nil. |
37 | 38 | func FromGRPCRequest(tracer opentracing.Tracer, operationName string, logger log.Logger) func(ctx context.Context, md *metadata.MD) context.Context { |
38 | 39 | return func(ctx context.Context, md *metadata.MD) context.Context { |
39 | span, err := tracer.Join(operationName, opentracing.TextMap, metadataReaderWriter{md}) | |
40 | var span opentracing.Span | |
41 | wireContext, err := tracer.Extract(opentracing.TextMap, metadataReaderWriter{md}) | |
40 | 42 | if err != nil { |
43 | logger.Log("err", err) | |
44 | } | |
45 | if wireContext != nil { | |
46 | span = tracer.StartSpan(operationName, ext.RPCServerOption(wireContext)) | |
47 | } else { | |
41 | 48 | span = tracer.StartSpan(operationName) |
42 | if err != opentracing.ErrTraceNotFound { | |
43 | logger.Log("err", err) | |
44 | } | |
45 | 49 | } |
46 | 50 | return opentracing.ContextWithSpan(ctx, span) |
47 | 51 | } |
35 | 35 | |
36 | 36 | // There's nothing we can do with any errors here. |
37 | 37 | if err = tracer.Inject( |
38 | span, | |
38 | span.Context(), | |
39 | 39 | opentracing.TextMap, |
40 | 40 | opentracing.HTTPHeaderTextMapCarrier(req.Header), |
41 | 41 | ); err != nil { |
56 | 56 | func FromHTTPRequest(tracer opentracing.Tracer, operationName string, logger log.Logger) kithttp.RequestFunc { |
57 | 57 | return func(ctx context.Context, req *http.Request) context.Context { |
58 | 58 | // Try to join to a trace propagated in `req`. |
59 | span, err := tracer.Join( | |
60 | operationName, | |
59 | var span opentracing.Span | |
60 | wireContext, err := tracer.Extract( | |
61 | 61 | opentracing.TextMap, |
62 | 62 | opentracing.HTTPHeaderTextMapCarrier(req.Header), |
63 | 63 | ) |
64 | 64 | if err != nil { |
65 | logger.Log("err", err) | |
66 | } | |
67 | if wireContext != nil { | |
68 | span = tracer.StartSpan(operationName, ext.RPCServerOption(wireContext)) | |
69 | } else { | |
65 | 70 | span = tracer.StartSpan(operationName) |
66 | if err != opentracing.ErrTraceNotFound { | |
67 | logger.Log("err", err) | |
68 | } | |
69 | 71 | } |
70 | 72 | return opentracing.ContextWithSpan(ctx, span) |
71 | 73 | } |