aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Symonds <dsymonds@golang.org>2015-06-10 14:37:36 -0700
committerDavid Symonds <dsymonds@golang.org>2015-06-22 16:16:26 +1000
commit38f3db3860bb1812858610029b0d8188517d8b74 (patch)
treee26a9d0ba75f94fc5490be94835c7a4662a01bb7
parent10576091dc82c9c109dddfb5ed77bdbbc87a9af8 (diff)
downloadnet-38f3db3860bb1812858610029b0d8188517d8b74.tar.gz
trace: Add NewContext and FromContext functions.
These will be the standard way for inserting a Trace into a Context, and retrieving it later. NewContext will typically only be used by top-level context creators (e.g. the gRPC package). Change-Id: Ifb6fc81f46a71bb965196f664714d3042a9aea18
-rw-r--r--trace/trace.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/trace/trace.go b/trace/trace.go
index b469f68..0bd8d1e 100644
--- a/trace/trace.go
+++ b/trace/trace.go
@@ -36,6 +36,7 @@ import (
"sync/atomic"
"time"
+ "golang.org/x/net/context"
"golang.org/x/net/internal/timeseries"
)
@@ -207,6 +208,22 @@ func lookupBucket(fam string, b int) *traceBucket {
return f.Buckets[b]
}
+type contextKeyT string
+
+var contextKey = contextKeyT("golang.org/x/net/trace.Trace")
+
+// NewContext returns a copy of the parent context
+// and associates it with a Trace.
+func NewContext(ctx context.Context, tr Trace) context.Context {
+ return context.WithValue(ctx, contextKey, tr)
+}
+
+// FromContext returns the Trace bound to the context, if any.
+func FromContext(ctx context.Context) (tr Trace, ok bool) {
+ tr, ok = ctx.Value(contextKey).(Trace)
+ return
+}
+
// Trace represents an active request.
type Trace interface {
// LazyLog adds x to the event log. It will be evaluated each time the