fork of https://github.com/sourcegraph/zoekt
1package trace
2
3import (
4 "context"
5
6 "github.com/opentracing/opentracing-go"
7)
8
9type key int
10
11const (
12 enableOpenTracingKey key = iota
13)
14
15// isOpenTracingEnabled returns true if the enableOpenTracingKey context value is true.
16func isOpenTracingEnabled(ctx context.Context) bool {
17 v, ok := ctx.Value(enableOpenTracingKey).(bool)
18 if !ok {
19 return false
20 }
21 return v
22}
23
24func WithOpenTracingEnabled(ctx context.Context, enableOpenTracing bool) context.Context {
25 return context.WithValue(ctx, enableOpenTracingKey, enableOpenTracing)
26}
27
28// GetOpenTracer returns the tracer to actually use depending on whether isOpenTracingEnabled(ctx)
29// returns true or false. If false, this returns the NoopTracer.
30func GetOpenTracer(ctx context.Context, tracer opentracing.Tracer) opentracing.Tracer {
31 if !isOpenTracingEnabled(ctx) {
32 return opentracing.NoopTracer{}
33 }
34 if tracer == nil {
35 return opentracing.GlobalTracer()
36 }
37 return tracer
38}