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

Expose SpanFromContext #672

Merged
merged 2 commits into from Jul 27, 2023
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
24 changes: 3 additions & 21 deletions tracing.go
Expand Up @@ -956,27 +956,9 @@ func TransactionFromContext(ctx context.Context) *Span {
return nil
}

// 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 {
// SpanFromContext returns the last span stored in the context, or nil if no span
// is set on the context.
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
Expand Up @@ -389,7 +389,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 @@ -586,7 +586,7 @@ func TestSpanFromContext(t *testing.T) {
// SpanFromContext(ctx).StartChild(...) === StartSpan(ctx, ...)

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

_ = span

Expand Down