From dde4d360660838f3c2e0ced8205bc8f7a8d312d9 Mon Sep 17 00:00:00 2001 From: Andrii Zhezhel <21112019+zhezhel@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:52:17 +0200 Subject: [PATCH] feat: Expose SpanFromContext (#672) Co-authored-by: Andrii Zhezhel Co-authored-by: Anton Ovchinnikov --- tracing.go | 24 +++--------------------- tracing_test.go | 4 ++-- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/tracing.go b/tracing.go index a6cf743a9..38b810c01 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 -// 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 } 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