-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext.go
More file actions
25 lines (21 loc) · 702 Bytes
/
context.go
File metadata and controls
25 lines (21 loc) · 702 Bytes
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
package tracer
import "context"
type key struct{}
// Fetch tries to get the tracer from a context or returns safe nil.
//
// tracer.Fetch(context.Background()).Start().Stop() // won't panic
//
func Fetch(ctx context.Context) *Trace {
trace, _ := ctx.Value(key{}).(*Trace)
return trace
}
// Inject returns a new context with injected into it the tracer.
//
// func (server *server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// req = req.WithContext(tracer.Inject(req.Context(), make([]*trace.Call, 0, 10)))
// server.routing.Handle(rw, req)
// }
//
func Inject(ctx context.Context, stack []*Call) context.Context {
return context.WithValue(ctx, key{}, &Trace{stack: stack})
}