forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
63 lines (52 loc) · 1.91 KB
/
context.go
File metadata and controls
63 lines (52 loc) · 1.91 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package trace
import (
"context"
"github.com/sourcegraph/log"
oteltrace "go.opentelemetry.io/otel/trace"
"github.com/sourcegraph/sourcegraph/internal/trace/policy"
)
type traceContextKey string
const traceKey = traceContextKey("trace")
// contextWithTrace returns a new context.Context that holds a reference to trace's
// SpanContext. External callers should likely use CopyContext, as this properly propagates all
// tracing context from one context to another.
func contextWithTrace(ctx context.Context, tr *Trace) context.Context {
ctx = oteltrace.ContextWithSpan(ctx, tr.oteltraceSpan)
ctx = context.WithValue(ctx, traceKey, tr)
return ctx
}
// TraceFromContext returns the Trace previously associated with ctx, or
// nil if no such Trace could be found.
func TraceFromContext(ctx context.Context) *Trace {
tr, _ := ctx.Value(traceKey).(*Trace)
return tr
}
// CopyContext copies the tracing-related context items from one context to another and returns that
// context.
func CopyContext(ctx context.Context, from context.Context) context.Context {
if tr := TraceFromContext(from); tr != nil {
ctx = contextWithTrace(ctx, tr)
}
if shouldTrace := policy.ShouldTrace(from); shouldTrace {
ctx = policy.WithShouldTrace(ctx, shouldTrace)
}
return ctx
}
// ID returns a trace ID, if any, found in the given context. If you need both trace and
// span ID, use trace.Context.
func ID(ctx context.Context) string {
return Context(ctx).TraceID
}
// Context retrieves the full trace context, if any, from context - this includes
// both TraceID and SpanID.
func Context(ctx context.Context) log.TraceContext {
// get the OpenTelemetry span, which is always present via the OpenTracing bridge
if otelSpan := oteltrace.SpanContextFromContext(ctx); otelSpan.IsValid() {
return log.TraceContext{
TraceID: otelSpan.TraceID().String(),
SpanID: otelSpan.SpanID().String(),
}
}
// no span found
return log.TraceContext{}
}