Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Added function to continue from trace string #434

Merged
merged 2 commits into from Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion tracing.go
Expand Up @@ -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.
karatekaneen marked this conversation as resolved.
Show resolved Hide resolved
func ContinueFromTrace(trace string) SpanOption {
return func(s *Span) {
trace := r.Header.Get("sentry-trace")
if trace == "" {
return
}
Expand Down
26 changes: 26 additions & 0 deletions tracing_test.go
Expand Up @@ -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
Expand Down