diff --git a/tracing.go b/tracing.go index 125bb3e1a..9cabb3554 100644 --- a/tracing.go +++ b/tracing.go @@ -545,9 +545,17 @@ 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 TraceID. +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