/
context.go
30 lines (24 loc) · 906 Bytes
/
context.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 tracing
import "context"
type (
spanContextKey struct{}
traceContextKey struct{}
)
// NewContextWithSpan returns a new context with the given Span added.
func NewContextWithSpan(ctx context.Context, c *Span) context.Context {
return context.WithValue(ctx, spanContextKey{}, c)
}
// SpanFromContext returns the Span associated with ctx or nil if no Span has been assigned.
func SpanFromContext(ctx context.Context) *Span {
c, _ := ctx.Value(spanContextKey{}).(*Span)
return c
}
// NewContextWithTrace returns a new context with the given Trace added.
func NewContextWithTrace(ctx context.Context, t *Trace) context.Context {
return context.WithValue(ctx, traceContextKey{}, t)
}
// TraceFromContext returns the Trace associated with ctx or nil if no Trace has been assigned.
func TraceFromContext(ctx context.Context) *Trace {
c, _ := ctx.Value(traceContextKey{}).(*Trace)
return c
}