From d95b1ec5f8b746b42f56b98c4d8d94dbdedee59e Mon Sep 17 00:00:00 2001 From: Andrii Zhezhel Date: Mon, 24 Jul 2023 17:12:36 +0200 Subject: [PATCH 1/2] Expose SpanFromContext --- tracing.go | 22 ++-------------------- tracing_test.go | 4 ++-- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/tracing.go b/tracing.go index a6cf743a9..426c4d479 100644 --- a/tracing.go +++ b/tracing.go @@ -956,27 +956,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 } diff --git a/tracing_test.go b/tracing_test.go index d31e51755..5bb0e364b 100644 --- a/tracing_test.go +++ b/tracing_test.go @@ -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") } @@ -586,7 +586,7 @@ func TestSpanFromContext(t *testing.T) { // SpanFromContext(ctx).StartChild(...) === StartSpan(ctx, ...) ctx := NewTestContext(ClientOptions{}) - span := spanFromContext(ctx) + span := SpanFromContext(ctx) _ = span From ba1e9b097a0b1162c0e2304423bf6954f21d580f Mon Sep 17 00:00:00 2001 From: Andrii Zhezhel <21112019+zhezhel@users.noreply.github.com> Date: Wed, 26 Jul 2023 17:03:15 +0200 Subject: [PATCH 2/2] Update tracing.go Co-authored-by: Anton Ovchinnikov --- tracing.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tracing.go b/tracing.go index 426c4d479..38b810c01 100644 --- a/tracing.go +++ b/tracing.go @@ -956,8 +956,8 @@ func TransactionFromContext(ctx context.Context) *Span { return nil } -// SpanFromContext returns the last span stored in the context or a dummy -// non-nil 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