-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
endpoint.go
30 lines (25 loc) · 935 Bytes
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package zipkin
import (
"context"
"github.com/openzipkin/zipkin-go"
"github.com/openzipkin/zipkin-go/model"
"github.com/go-kit/kit/endpoint"
)
// TraceEndpoint returns an Endpoint middleware, tracing a Go kit endpoint.
// This endpoint tracer should be used in combination with a Go kit Transport
// tracing middleware or custom before and after transport functions as
// propagation of SpanContext is not provided in this middleware.
func TraceEndpoint(tracer *zipkin.Tracer, name string) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
var sc model.SpanContext
if parentSpan := zipkin.SpanFromContext(ctx); parentSpan != nil {
sc = parentSpan.Context()
}
sp := tracer.StartSpan(name, zipkin.Parent(sc))
defer sp.Finish()
ctx = zipkin.NewContext(ctx, sp)
return next(ctx, request)
}
}
}