Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Restore the ability to reset current span in context nil #231

Merged
merged 2 commits into from Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions gocontext.go
Expand Up @@ -7,10 +7,12 @@ type contextKey struct{}
var activeSpanKey = contextKey{}

// ContextWithSpan returns a new `context.Context` that holds a reference to
// `span`'s SpanContext.
// the span. If span is nil, a new context without an active span is returned.
func ContextWithSpan(ctx context.Context, span Span) context.Context {
if tracerWithHook, ok := span.Tracer().(TracerContextWithSpanExtension); ok {
ctx = tracerWithHook.ContextWithSpanHook(ctx, span)
if span != nil {
if tracerWithHook, ok := span.Tracer().(TracerContextWithSpanExtension); ok {
ctx = tracerWithHook.ContextWithSpanHook(ctx, span)
}
}
return context.WithValue(ctx, activeSpanKey, span)
}
Expand Down
5 changes: 5 additions & 0 deletions gocontext_test.go
Expand Up @@ -27,6 +27,11 @@ func TestContextWithSpan(t *testing.T) {
if span != span2 {
t.Errorf("Not the same span returned from context, expected=%+v, actual=%+v", span, span2)
}

ctx = ContextWithSpan(ctx, nil)
if s := SpanFromContext(ctx); s != nil {
t.Errorf("Not able to reset span in context, expected=nil, actual=%+v", s)
}
}

type noopExtTracer struct {
Expand Down