From 46dd1c60f24bb74f4cd849eb1165f0a14fee3b1f Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 27 Apr 2022 13:58:24 +0200 Subject: [PATCH 1/2] feat: :sparkles: Added function to continue from trace string This allows users to easily add traces from sources other than http.Request --- tracing.go | 11 ++++++++++- tracing_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tracing.go b/tracing.go index 125bb3e1a..2fd340132 100644 --- a/tracing.go +++ b/tracing.go @@ -545,9 +545,18 @@ func TransactionName(name string) SpanOption { // ContinueFromRequest returns a span option that updates the span to continue // an existing trace. If it cannot detect an existing trace in the request, the // span will be left unchanged. +// +// ContinueFromRequest is an alias for: +// ContinueFromTrace(r.Header.Get("sentry-trace")) func ContinueFromRequest(r *http.Request) SpanOption { + return ContinueFromTrace(r.Header.Get("sentry-trace")) +} + +// ContinueFromTrace returns a span option that updates the span to continue +// an existing trace. If it cannot detect an existing trace in the request, the +// span will be left unchanged. +func ContinueFromTrace(trace string) SpanOption { return func(s *Span) { - trace := r.Header.Get("sentry-trace") if trace == "" { return } diff --git a/tracing_test.go b/tracing_test.go index 64ca096f9..1769a2563 100644 --- a/tracing_test.go +++ b/tracing_test.go @@ -342,6 +342,32 @@ func TestContinueSpanFromRequest(t *testing.T) { }) } } +func TestContinueSpanFromTrace(t *testing.T) { + traceID := TraceIDFromHex("bc6d53f15eb88f4320054569b8c553d4") + spanID := SpanIDFromHex("b72fa28504b07285") + + for _, sampled := range []Sampled{SampledTrue, SampledFalse, SampledUndefined} { + sampled := sampled + t.Run(sampled.String(), func(t *testing.T) { + var s Span + trace := (&Span{ + TraceID: traceID, + SpanID: spanID, + Sampled: sampled, + }).ToSentryTrace() + ContinueFromTrace(trace)(&s) + if s.TraceID != traceID { + t.Errorf("got %q, want %q", s.TraceID, traceID) + } + if s.ParentSpanID != spanID { + t.Errorf("got %q, want %q", s.ParentSpanID, spanID) + } + if s.Sampled != sampled { + t.Errorf("got %q, want %q", s.Sampled, sampled) + } + }) + } +} func TestSpanFromContext(t *testing.T) { // SpanFromContext always returns a non-nil value, such that you can use From 1a26634bfc26724a62da65debe70f26020266d12 Mon Sep 17 00:00:00 2001 From: karatekaneen <41196840+karatekaneen@users.noreply.github.com> Date: Wed, 27 Apr 2022 15:43:10 +0200 Subject: [PATCH 2/2] Update tracing.go due to feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kamil Ogórek --- tracing.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tracing.go b/tracing.go index 2fd340132..9cabb3554 100644 --- a/tracing.go +++ b/tracing.go @@ -553,8 +553,7 @@ func ContinueFromRequest(r *http.Request) SpanOption { } // ContinueFromTrace returns a span option that updates the span to continue -// an existing trace. If it cannot detect an existing trace in the request, the -// span will be left unchanged. +// an existing TraceID. func ContinueFromTrace(trace string) SpanOption { return func(s *Span) { if trace == "" {