From 9b906502e23c426fb1483e9783e7f46b06d19940 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Wed, 8 Apr 2020 15:25:05 -0400 Subject: [PATCH] Restore the ability to reset the current span in context to nil (#231) * Restore the ability to reset current span in context nil Signed-off-by: Yuri Shkuro * Fix unrelated test; use latest Go versions Signed-off-by: Yuri Shkuro --- .travis.yml | 4 ++-- ext/field_test.go | 2 +- gocontext.go | 8 +++++--- gocontext_test.go | 5 +++++ 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8d5b75e..b950e42 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,8 +2,8 @@ language: go matrix: include: - - go: "1.11.x" - - go: "1.12.x" + - go: "1.13.x" + - go: "1.14.x" - go: "tip" env: - LINT=true diff --git a/ext/field_test.go b/ext/field_test.go index 8947840..d4f633e 100644 --- a/ext/field_test.go +++ b/ext/field_test.go @@ -37,7 +37,7 @@ func TestLogError(t *testing.T) { ValueString: "error", }, { - Key: "error", + Key: "error.object", ValueKind: reflect.String, ValueString: err.Error(), }, diff --git a/gocontext.go b/gocontext.go index 8865e75..1831bc9 100644 --- a/gocontext.go +++ b/gocontext.go @@ -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) } diff --git a/gocontext_test.go b/gocontext_test.go index a5b1aed..c6bbad4 100644 --- a/gocontext_test.go +++ b/gocontext_test.go @@ -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 {