9 "github.com/opentracing/opentracing-go"
10 "github.com/opentracing/opentracing-go/ext"
12 "github.com/go-kit/kit/log"
13 kithttp "github.com/go-kit/kit/transport/http"
16 // ContextToHTTP returns an http RequestFunc that injects an OpenTracing Span
17 // found in `ctx` into the http headers. If no such Span can be found, the
18 // RequestFunc is a noop.
19 func ContextToHTTP(tracer opentracing.Tracer, logger log.Logger) kithttp.RequestFunc {
20 return func(ctx context.Context, req *http.Request) context.Context {
21 // Try to find a Span in the Context.
22 if span := opentracing.SpanFromContext(ctx); span != nil {
23 // Add standard OpenTracing tags.
24 ext.HTTPMethod.Set(span, req.Method)
25 ext.HTTPUrl.Set(span, req.URL.String())
26 host, portString, err := net.SplitHostPort(req.URL.Host)
28 ext.PeerHostname.Set(span, host)
29 if port, err := strconv.Atoi(portString); err != nil {
30 ext.PeerPort.Set(span, uint16(port))
33 ext.PeerHostname.Set(span, req.URL.Host)
36 // There's nothing we can do with any errors here.
37 if err = tracer.Inject(
40 opentracing.HTTPHeadersCarrier(req.Header),
42 logger.Log("err", err)
49 // HTTPToContext returns an http RequestFunc that tries to join with an
50 // OpenTracing trace found in `req` and starts a new Span called
51 // `operationName` accordingly. If no trace could be found in `req`, the Span
52 // will be a trace root. The Span is incorporated in the returned Context and
53 // can be retrieved with opentracing.SpanFromContext(ctx).
54 func HTTPToContext(tracer opentracing.Tracer, operationName string, logger log.Logger) kithttp.RequestFunc {
55 return func(ctx context.Context, req *http.Request) context.Context {
56 // Try to join to a trace propagated in `req`.
57 var span opentracing.Span
58 wireContext, err := tracer.Extract(
60 opentracing.HTTPHeadersCarrier(req.Header),
62 if err != nil && err != opentracing.ErrSpanContextNotFound {
63 logger.Log("err", err)
66 span = tracer.StartSpan(operationName, ext.RPCServerOption(wireContext))
67 ext.HTTPMethod.Set(span, req.Method)
68 ext.HTTPUrl.Set(span, req.URL.String())
69 return opentracing.ContextWithSpan(ctx, span)