Skip to content

Commit

Permalink
feat: Expose SpanFromContext (#672)
Browse files Browse the repository at this point in the history

Co-authored-by: Andrii Zhezhel <andrii.zhezhel@inflect.com>
Co-authored-by: Anton Ovchinnikov <anton@tonyo.info>
  • Loading branch information
3 people committed Jul 27, 2023
1 parent b551438 commit dde4d36
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
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

0 comments on commit dde4d36

Please sign in to comment.