From 04ea85a755afd823a154b605217269f8934d0ea2 Mon Sep 17 00:00:00 2001 From: Oliver Powell Date: Thu, 7 Sep 2023 05:39:32 -0300 Subject: [PATCH] fix(otel): prevent panic in shutdown (#711) Shutdown panics when passed a background context, or any context which doesn't contain a hub. Returning the current hub if no hub is found on the context prevents this. --- otel/span_processor.go | 4 ++++ otel/span_processor_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/otel/span_processor.go b/otel/span_processor.go index 8b64d9dd0..d32e2b55b 100644 --- a/otel/span_processor.go +++ b/otel/span_processor.go @@ -103,6 +103,10 @@ func (ssp *sentrySpanProcessor) ForceFlush(ctx context.Context) error { func flushSpanProcessor(ctx context.Context) error { hub := sentry.GetHubFromContext(ctx) + if hub == nil { + hub = sentry.CurrentHub() + } + // TODO(michi) should we make this configurable? defer hub.Flush(2 * time.Second) return nil diff --git a/otel/span_processor_test.go b/otel/span_processor_test.go index 4361442c0..3154b4e54 100644 --- a/otel/span_processor_test.go +++ b/otel/span_processor_test.go @@ -72,7 +72,7 @@ func TestNewSentrySpanProcessor(t *testing.T) { func TestSpanProcessorShutdown(t *testing.T) { spanProcessor, _, tracer := setupSpanProcessorTest() - ctx := emptyContextWithSentry() + ctx := context.Background() tracer.Start(emptyContextWithSentry(), "spanName") assertEqual(t, sentrySpanMap.Len(), 1)