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

fix(otel): Overwrite "trace" context in Sentry error #580

Merged
merged 9 commits into from Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 11 additions & 9 deletions otel/event_processor.go
Expand Up @@ -15,6 +15,10 @@ func linkTraceContextToErrorEvent(event *sentry.Event, hint *sentry.EventHint) *
if hint == nil || hint.Context == nil {
return event
}
// TODO: what we want here is to compare with the (unexported) sentry.transactionType
if event.Type == "transaction" {
return event
}
otelSpanContext := trace.SpanContextFromContext(hint.Context)
var sentrySpan *sentry.Span
if otelSpanContext.IsValid() {
Expand All @@ -24,15 +28,13 @@ func linkTraceContextToErrorEvent(event *sentry.Event, hint *sentry.EventHint) *
return event
}

traceContext := event.Contexts["trace"]
if len(traceContext) > 0 {
// trace context is already set, not touching it
return event
}
event.Contexts["trace"] = map[string]interface{}{
"trace_id": sentrySpan.TraceID.String(),
"span_id": sentrySpan.SpanID.String(),
"parent_span_id": sentrySpan.ParentSpanID.String(),
traceContext, found := event.Contexts["trace"]
if !found {
event.Contexts["trace"] = make(map[string]interface{})
traceContext = event.Contexts["trace"]
}
traceContext["trace_id"] = sentrySpan.TraceID.String()
traceContext["span_id"] = sentrySpan.SpanID.String()
traceContext["parent_span_id"] = sentrySpan.ParentSpanID.String()
return event
}
101 changes: 45 additions & 56 deletions otel/event_processor_test.go
Expand Up @@ -4,65 +4,54 @@ package sentryotel

import (
"errors"
"fmt"
"testing"

"github.com/getsentry/sentry-go"
)

func TestLinkTraceContextToErrorEventWithEmptyTraceContext(t *testing.T) {
_, _, tracer := setupSpanProcessorTest()
ctx, otelSpan := tracer.Start(emptyContextWithSentry(), "spanName")
sentrySpan, _ := sentrySpanMap.Get(otelSpan.SpanContext().SpanID())

hub := sentry.GetHubFromContext(ctx)
client := hub.Client()
client.CaptureException(
errors.New("new sentry exception"),
&sentry.EventHint{Context: ctx},
nil,
)

transport := client.Transport.(*TransportMock)
events := transport.Events()
assertEqual(t, len(events), 1)
err := events[0]
exception := err.Exception[0]
assertEqual(t, exception.Type, "*errors.errorString")
assertEqual(t, exception.Value, "new sentry exception")
assertEqual(t, err.Type, "")
assertEqual(t,
err.Contexts["trace"],
map[string]interface{}{
"trace_id": sentrySpan.TraceID.String(),
"span_id": sentrySpan.SpanID.String(),
"parent_span_id": sentrySpan.ParentSpanID.String(),
},
)
}

func TestLinkTraceContextToErrorEventDoesNotTouchExistingTraceContext(t *testing.T) {
_, _, tracer := setupSpanProcessorTest()
ctx, _ := tracer.Start(emptyContextWithSentry(), "spanName")

hub := sentry.GetHubFromContext(ctx)
hub.Scope().SetContext("trace", map[string]interface{}{"trace_id": "123"})
client := hub.Client()
client.CaptureException(
errors.New("new sentry exception with existing trace context"),
&sentry.EventHint{Context: ctx},
hub.Scope(),
)

transport := client.Transport.(*TransportMock)
events := transport.Events()
assertEqual(t, len(events), 1)
err := events[0]
exception := err.Exception[0]
assertEqual(t, exception.Type, "*errors.errorString")
assertEqual(t, exception.Value, "new sentry exception with existing trace context")
assertEqual(t, err.Type, "")
assertEqual(t,
err.Contexts["trace"],
map[string]interface{}{"trace_id": "123"},
)
func TestLinkTraceContextToErrorEventSetsContext(t *testing.T) {

withExistingContextOptions := []bool{false, true}

for _, withExistingContext := range withExistingContextOptions {
withExistingContext := withExistingContext
name := fmt.Sprintf("withExistingContext_%v", withExistingContext)

t.Run(name, func(t *testing.T) {
_, _, tracer := setupSpanProcessorTest()
ctx, otelSpan := tracer.Start(emptyContextWithSentry(), "spanName")
sentrySpan, _ := sentrySpanMap.Get(otelSpan.SpanContext().SpanID())

hub := sentry.GetHubFromContext(ctx)
client, scope := hub.Client(), hub.Scope()

if withExistingContext {
// The existing "trace" context should be ovewritten by the event processor
scope.SetContext("trace", map[string]interface{}{"trace_id": "123"})
}
client.CaptureException(
errors.New("new sentry exception with existing trace context"),
&sentry.EventHint{Context: ctx},
hub.Scope(),
)

transport := client.Transport.(*TransportMock)
events := transport.Events()
assertEqual(t, len(events), 1)
err := events[0]
exception := err.Exception[0]
assertEqual(t, exception.Type, "*errors.errorString")
assertEqual(t, exception.Value, "new sentry exception with existing trace context")
assertEqual(t, err.Type, "")
assertEqual(t,
err.Contexts["trace"],
map[string]interface{}{
"trace_id": sentrySpan.TraceID.String(),
"span_id": sentrySpan.SpanID.String(),
"parent_span_id": sentrySpan.ParentSpanID.String(),
},
)
})
}
}