Export zipkin.FromContext so it can be used to annotate spans inside your service's business logic methods
Bas van Beek
8 years ago
60 | 60 | func AnnotateServer(newSpan NewSpanFunc, c Collector) endpoint.Middleware { |
61 | 61 | return func(next endpoint.Endpoint) endpoint.Endpoint { |
62 | 62 | return func(ctx context.Context, request interface{}) (interface{}, error) { |
63 | span, ok := fromContext(ctx) | |
63 | span, ok := FromContext(ctx) | |
64 | 64 | if !ok { |
65 | 65 | span = newSpan(newID(), newID(), 0) |
66 | 66 | ctx = context.WithValue(ctx, SpanContextKey, span) |
81 | 81 | return func(next endpoint.Endpoint) endpoint.Endpoint { |
82 | 82 | return func(ctx context.Context, request interface{}) (interface{}, error) { |
83 | 83 | var clientSpan *Span |
84 | parentSpan, ok := fromContext(ctx) | |
84 | parentSpan, ok := FromContext(ctx) | |
85 | 85 | if ok { |
86 | 86 | clientSpan = newSpan(parentSpan.TraceID(), newID(), parentSpan.SpanID()) |
87 | 87 | } else { |
123 | 123 | // a child/client span. |
124 | 124 | func ToRequest(newSpan NewSpanFunc) func(ctx context.Context, r *http.Request) context.Context { |
125 | 125 | return func(ctx context.Context, r *http.Request) context.Context { |
126 | span, ok := fromContext(ctx) | |
126 | span, ok := FromContext(ctx) | |
127 | 127 | if !ok { |
128 | 128 | span = newSpan(newID(), newID(), 0) |
129 | 129 | } |
147 | 147 | // a child/client span. |
148 | 148 | func ToGRPCRequest(newSpan NewSpanFunc) func(ctx context.Context, md *metadata.MD) context.Context { |
149 | 149 | return func(ctx context.Context, md *metadata.MD) context.Context { |
150 | span, ok := fromContext(ctx) | |
150 | span, ok := FromContext(ctx) | |
151 | 151 | if !ok { |
152 | 152 | span = newSpan(newID(), newID(), 0) |
153 | 153 | } |
242 | 242 | return newSpan(traceID, spanID, parentSpanID) |
243 | 243 | } |
244 | 244 | |
245 | func fromContext(ctx context.Context) (*Span, bool) { | |
245 | // FromContext extracts an existing Zipkin span if it is stored in the provided | |
246 | // context. If you add context.Context as the first parameter in your service | |
247 | // methods you can annotate spans from within business logic. Typical use case | |
248 | // is to AnnotateDuration on interaction with resources like databases. | |
249 | func FromContext(ctx context.Context) (*Span, bool) { | |
246 | 250 | val := ctx.Value(SpanContextKey) |
247 | 251 | if val == nil { |
248 | 252 | return nil, false |