From eff3909eaf3dc69f836e0a6428c1a589257c211e Mon Sep 17 00:00:00 2001 From: Jarrad Whitaker <155397737+jwhitaker-gridcog@users.noreply.github.com> Date: Tue, 31 Dec 2024 11:24:43 +1100 Subject: [PATCH] Allow OTEL instrumenter to work even with no DSN. Context: we are writing tests for our Sentry integration itself, setting up a client like ```py events = [] c = sentry_sdk.Client(dsn=None, instrumenter='otel', transport=events.append) ``` , and verifying that events contains spans from OpenTelemetry such as ```py with tracer.start_as_current_span('fn'): raise Exception('boo') assert events = [...] ``` This currently doesn't work because the Sentry OTEL integration has some hacks poking into the client and hard-exiting if DSN is None. I conjecture this can be removed - the check-DSN-to-avoid-sentry-otel-loops logic still works even without the DSN check in on_start(). --- .../integrations/opentelemetry/span_processor.py | 8 -------- .../opentelemetry/test_span_processor.py | 13 +++++++++++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/span_processor.py b/sentry_sdk/integrations/opentelemetry/span_processor.py index e00562a509..04978844e0 100644 --- a/sentry_sdk/integrations/opentelemetry/span_processor.py +++ b/sentry_sdk/integrations/opentelemetry/span_processor.py @@ -113,14 +113,6 @@ def on_start(self, otel_span, parent_context=None): # type: (OTelSpan, Optional[context_api.Context]) -> None client = get_client() - if not client.dsn: - return - - try: - _ = Dsn(client.dsn) - except Exception: - return - if client.options["instrumenter"] != INSTRUMENTER.OTEL: return diff --git a/tests/integrations/opentelemetry/test_span_processor.py b/tests/integrations/opentelemetry/test_span_processor.py index ec5cf6af23..801f7c4463 100644 --- a/tests/integrations/opentelemetry/test_span_processor.py +++ b/tests/integrations/opentelemetry/test_span_processor.py @@ -287,7 +287,16 @@ def test_update_span_with_otel_data_db_query(): ) -def test_on_start_transaction(): +@pytest.mark.parametrize( + ["dsn"], + [ + pytest.param( + "https://1234567890abcdef@o123456.ingest.sentry.io/123456", id="with a DSN" + ), + pytest.param(None, id="with no DSN"), + ], +) +def test_on_start_transaction(dsn): otel_span = MagicMock() otel_span.name = "Sample OTel Span" otel_span.start_time = time.time_ns() @@ -306,7 +315,7 @@ def test_on_start_transaction(): fake_client = MagicMock() fake_client.options = {"instrumenter": "otel"} - fake_client.dsn = "https://1234567890abcdef@o123456.ingest.sentry.io/123456" + fake_client.dsn = dsn sentry_sdk.get_global_scope().set_client(fake_client) with mock.patch(