Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export spanFromContext #452

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 2 additions & 20 deletions tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,27 +600,9 @@ func TransactionFromContext(ctx context.Context) *Span {
return nil
}

// spanFromContext returns the last span stored in the context or a dummy
// SpanFromContext returns the last span stored in the context or a dummy
// non-nil span.
//
// TODO(tracing): consider exporting this. Without this, users cannot retrieve a
// span from a context since spanContextKey is not exported.
//
// This can be added retroactively, and in the meantime think better whether it
// should return nil (like GetHubFromContext), always non-nil (like
// HubFromContext), or both: two exported functions.
//
// Note the equivalence:
//
// SpanFromContext(ctx).StartChild(...) === StartSpan(ctx, ...)
//
// So we don't aim spanFromContext at creating spans, but mutating existing
// spans that you'd have no access otherwise (because it was created in code you
// do not control, for example SDK auto-instrumentation).
//
// For now we provide TransactionFromContext, which solves the more common case
// of setting tags, etc, on the current transaction.
func spanFromContext(ctx context.Context) *Span {
func SpanFromContext(ctx context.Context) *Span {
if span, ok := ctx.Value(spanContextKey{}).(*Span); ok {
return span
}
Expand Down
4 changes: 2 additions & 2 deletions tracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (c SpanCheck) Check(t *testing.T, span *Span) {
t.Errorf("original context value lost")
}
// Invariant: SpanFromContext(span.Context) == span
if spanFromContext(gotCtx) != span {
if SpanFromContext(gotCtx) != span {
t.Errorf("span not in its context")
}

Expand Down Expand Up @@ -377,7 +377,7 @@ func TestSpanFromContext(t *testing.T) {
// SpanFromContext(ctx).StartChild(...) === StartSpan(ctx, ...)

ctx := NewTestContext(ClientOptions{})
span := spanFromContext(ctx)
span := SpanFromContext(ctx)

_ = span

Expand Down