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

Commit

Permalink
Restore the ability to reset the current span in context to nil (#231)
Browse files Browse the repository at this point in the history
* Restore the ability to reset current span in context nil

Signed-off-by: Yuri Shkuro <ys@uber.com>

* Fix unrelated test; use latest Go versions

Signed-off-by: Yuri Shkuro <ys@uber.com>
  • Loading branch information
yurishkuro committed Apr 8, 2020
1 parent 0dfafac commit 9b90650
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
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

0 comments on commit 9b90650

Please sign in to comment.