Skip to content

Commit

Permalink
feat: ✨ Added function to continue from trace string (#434)
Browse files Browse the repository at this point in the history
* feat: ✨ Added function to continue from trace string

This allows users to easily add traces from sources other than http.Request

* Update tracing.go due to feedback

Co-authored-by: Kamil Ogórek <kamil.ogorek@gmail.com>

Co-authored-by: Robin <robin.bauhn@minso.se>
Co-authored-by: Kamil Ogórek <kamil.ogorek@gmail.com>
  • Loading branch information
3 people committed Apr 27, 2022
1 parent b7a0655 commit af11326
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tracing.go
Expand Up @@ -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
}
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

0 comments on commit af11326

Please sign in to comment.