-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(otel): add option to continue from otel
- Loading branch information
Showing
4 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package sentryotel | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/getsentry/sentry-go" | ||
sentryhttp "github.com/getsentry/sentry-go/http" | ||
"go.opentelemetry.io/otel" | ||
otelSdkTrace "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
func emptyContextWithSentryAndTracing(t *testing.T) (context.Context, <-chan *sentry.Event) { | ||
t.Helper() | ||
|
||
events := make(chan *sentry.Event, 1) | ||
|
||
client, err := sentry.NewClient(sentry.ClientOptions{ | ||
Debug: true, | ||
Dsn: "https://[email protected]/123", | ||
Environment: "testing", | ||
Release: "1.2.3", | ||
EnableTracing: true, | ||
BeforeSendTransaction: func(event *sentry.Event, _ *sentry.EventHint) *sentry.Event { | ||
events <- event | ||
return event | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatalf("failed to create sentry client: %v", err) | ||
} | ||
|
||
hub := sentry.NewHub(client, sentry.NewScope()) | ||
return sentry.SetHubOnContext(context.Background(), hub), events | ||
} | ||
|
||
func TestRespectOtelSampling(t *testing.T) { | ||
spanProcessor := NewSentrySpanProcessor() | ||
|
||
simulateOtelAndSentry := func(ctx context.Context) (root, inner trace.Span) { | ||
handler := sentryhttp.New(sentryhttp.Options{ | ||
SpanOptions: []sentry.SpanOption{ContinueFromOtel()}, | ||
}).Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, inner = otel.Tracer("").Start(ctx, "test-inner-span") | ||
})) | ||
|
||
tracer := otel.Tracer("") | ||
// simulate an otel middleware creating the root span before sentry | ||
ctx, root = tracer.Start(ctx, "test-root-span") | ||
|
||
handler.ServeHTTP( | ||
httptest.NewRecorder(), | ||
httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx), | ||
) | ||
|
||
return root, inner | ||
} | ||
|
||
t.Run("always sample", func(t *testing.T) { | ||
sentrySpanMap.Clear() | ||
|
||
tp := otelSdkTrace.NewTracerProvider( | ||
otelSdkTrace.WithSpanProcessor(spanProcessor), | ||
otelSdkTrace.WithSampler(otelSdkTrace.AlwaysSample()), | ||
) | ||
otel.SetTracerProvider(tp) | ||
|
||
ctx, events := emptyContextWithSentryAndTracing(t) | ||
|
||
root, inner := simulateOtelAndSentry(ctx) | ||
|
||
if root.SpanContext().TraceID() != inner.SpanContext().TraceID() { | ||
t.Errorf("otel root span and inner span should have the same trace id") | ||
} | ||
|
||
if len(events) != 1 { | ||
t.Errorf("got unexpected number of events sent to sentry: %d != 1", len(events)) | ||
} | ||
|
||
for _, span := range []trace.Span{root, inner} { | ||
if !span.SpanContext().IsSampled() { | ||
t.Errorf("otel span should be sampled") | ||
} | ||
|
||
spanID := span.SpanContext().SpanID() | ||
|
||
sentrySpan, ok := sentrySpanMap.Get(spanID) | ||
if !ok { | ||
t.Fatalf("sentry span could not be found from otel span %d", spanID) | ||
} | ||
|
||
if sentrySpan.Sampled != sentry.SampledTrue { | ||
t.Errorf("sentry span should be sampled, not %v", sentrySpan.Sampled) | ||
} | ||
} | ||
|
||
}) | ||
|
||
t.Run("never sample", func(t *testing.T) { | ||
sentrySpanMap.Clear() | ||
|
||
tp := otelSdkTrace.NewTracerProvider( | ||
otelSdkTrace.WithSpanProcessor(spanProcessor), | ||
otelSdkTrace.WithSampler(otelSdkTrace.NeverSample()), | ||
) | ||
otel.SetTracerProvider(tp) | ||
|
||
ctx, events := emptyContextWithSentryAndTracing(t) | ||
|
||
if len(events) != 0 { | ||
t.Fatalf("sentry span should not have been sent to sentry") | ||
} | ||
|
||
root, inner := simulateOtelAndSentry(ctx) | ||
|
||
for _, span := range []trace.Span{root, inner} { | ||
if span.SpanContext().IsSampled() { | ||
t.Errorf("otel span should not be sampled") | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package sentryotel | ||
|
||
import ( | ||
"github.com/getsentry/sentry-go" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
// ContinueFromOtel is a [sentry.SpanOption] that can be used with [sentryhttp.New] to ensure sentry uses any | ||
// existing OpenTelemetry trace in the context as the parent of the sentry span. | ||
// | ||
// NOTE: be sure to start the OpenTelemetry span before the sentry one by, e.g. keeping any OpenTelemetry middlewares | ||
// higher in the call chain. | ||
func ContinueFromOtel() sentry.SpanOption { | ||
return func(currentSpan *sentry.Span) { | ||
otelTrace := trace.SpanFromContext(currentSpan.Context()) | ||
if otelTrace == nil { | ||
return | ||
} | ||
transaction, ok := sentrySpanMap.Get(otelTrace.SpanContext().SpanID()) | ||
if !ok { | ||
return | ||
} | ||
currentSpan.ParentSpanID = transaction.SpanID | ||
// setting this directly because currentSpan.parent is not exported and this currently short-circuits | ||
// span.sample() in the right place. | ||
currentSpan.Sampled = transaction.Sampled | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters