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 all commits
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
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ext/field_test.go
Expand Up @@ -37,7 +37,7 @@ func TestLogError(t *testing.T) {
ValueString: "error",
},
{
Key: "error",
Key: "error.object",
ValueKind: reflect.String,
ValueString: err.Error(),
},
Expand Down
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