-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
51 lines (44 loc) · 1.2 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package pinpoint
import (
"context"
"net/http"
)
const contextKey = "pinpoint.spanTracer"
// NewContext returns a new Context that contains the given Tracer.
func NewContext(ctx context.Context, tracer Tracer) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, contextKey, tracer)
}
// FromContext returns the Tracer from the context. If not present, NoopTracer is returned.
func FromContext(ctx context.Context) Tracer {
if ctx == nil {
return NoopTracer()
}
if v := ctx.Value(contextKey); v != nil {
tracer, ok := v.(Tracer)
if !ok {
return NoopTracer()
}
return tracer
} else {
return NoopTracer()
}
}
// RequestWithTracerContext returns the request that has a Context carrying the given Tracer.
func RequestWithTracerContext(req *http.Request, tracer Tracer) *http.Request {
if req != nil {
ctx := NewContext(req.Context(), tracer)
return req.WithContext(ctx)
} else {
return req
}
}
// TracerFromRequestContext returns the Tracer from the request's context. If not present, NoopTracer is returned.
func TracerFromRequestContext(req *http.Request) Tracer {
if req != nil {
return FromContext(req.Context())
}
return NoopTracer()
}