Codebase list golang-github-go-kit-kit / 8cbddfe
Export zipkin.FromContext so it can be used to annotate spans inside your service's business logic methods Bas van Beek 8 years ago
1 changed file(s) with 9 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
6060 func AnnotateServer(newSpan NewSpanFunc, c Collector) endpoint.Middleware {
6161 return func(next endpoint.Endpoint) endpoint.Endpoint {
6262 return func(ctx context.Context, request interface{}) (interface{}, error) {
63 span, ok := fromContext(ctx)
63 span, ok := FromContext(ctx)
6464 if !ok {
6565 span = newSpan(newID(), newID(), 0)
6666 ctx = context.WithValue(ctx, SpanContextKey, span)
8181 return func(next endpoint.Endpoint) endpoint.Endpoint {
8282 return func(ctx context.Context, request interface{}) (interface{}, error) {
8383 var clientSpan *Span
84 parentSpan, ok := fromContext(ctx)
84 parentSpan, ok := FromContext(ctx)
8585 if ok {
8686 clientSpan = newSpan(parentSpan.TraceID(), newID(), parentSpan.SpanID())
8787 } else {
123123 // a child/client span.
124124 func ToRequest(newSpan NewSpanFunc) func(ctx context.Context, r *http.Request) context.Context {
125125 return func(ctx context.Context, r *http.Request) context.Context {
126 span, ok := fromContext(ctx)
126 span, ok := FromContext(ctx)
127127 if !ok {
128128 span = newSpan(newID(), newID(), 0)
129129 }
147147 // a child/client span.
148148 func ToGRPCRequest(newSpan NewSpanFunc) func(ctx context.Context, md *metadata.MD) context.Context {
149149 return func(ctx context.Context, md *metadata.MD) context.Context {
150 span, ok := fromContext(ctx)
150 span, ok := FromContext(ctx)
151151 if !ok {
152152 span = newSpan(newID(), newID(), 0)
153153 }
242242 return newSpan(traceID, spanID, parentSpanID)
243243 }
244244
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) {
246250 val := ctx.Value(SpanContextKey)
247251 if val == nil {
248252 return nil, false