forked from google/zoekt
/
opentracing.go
49 lines (40 loc) · 1.63 KB
/
opentracing.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package trace
import (
"context"
"github.com/opentracing/opentracing-go"
)
type key int
const (
enableOpenTracingKey key = iota
)
// isOpenTracingEnabled returns true if the enableOpenTracingKey context value is true.
func isOpenTracingEnabled(ctx context.Context) bool {
v, ok := ctx.Value(enableOpenTracingKey).(bool)
if !ok {
return false
}
return v
}
func WithOpenTracingEnabled(ctx context.Context, enableOpenTracing bool) context.Context {
return context.WithValue(ctx, enableOpenTracingKey, enableOpenTracing)
}
// GetOpenTracer returns the tracer to actually use depending on whether isOpenTracingEnabled(ctx)
// returns true or false. If false, this returns the NoopTracer.
func GetOpenTracer(ctx context.Context, tracer opentracing.Tracer) opentracing.Tracer {
if !isOpenTracingEnabled(ctx) {
return opentracing.NoopTracer{}
}
if tracer == nil {
return opentracing.GlobalTracer()
}
return tracer
}
// StartSpanFromContext starts a span using the tracer returned by invoking GetOpenTracer with the
// passed-in tracer.
func StartSpanFromContextWithTracer(ctx context.Context, tracer opentracing.Tracer, operationName string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
return opentracing.StartSpanFromContextWithTracer(ctx, GetOpenTracer(ctx, tracer), operationName, opts...)
}
// StartSpanFromContext starts a span using the tracer returned by GetOpenTracer.
func StartSpanFromContext(ctx context.Context, operationName string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
return StartSpanFromContextWithTracer(ctx, GetOpenTracer(ctx, nil), operationName, opts...)
}