-
Notifications
You must be signed in to change notification settings - Fork 569
feat(otlp): Optionally capture exceptions from otel's Span.record_exception api #5235
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -24,6 +24,25 @@ | |
| original_propagator = get_global_textmap() | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def mock_otlp_ingest(): | ||
| responses.start() | ||
| responses.add( | ||
| responses.POST, | ||
| url="https://bla.ingest.sentry.io/api/12312012/integration/otlp/v1/traces/", | ||
| status=200, | ||
| ) | ||
|
|
||
| yield | ||
|
|
||
| tracer_provider = get_tracer_provider() | ||
| if isinstance(tracer_provider, TracerProvider): | ||
| tracer_provider.force_flush() | ||
|
|
||
| responses.stop() | ||
| responses.reset() | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reset_otlp(uninstall_integration): | ||
| trace._TRACER_PROVIDER_SET_ONCE = Once() | ||
|
|
@@ -127,14 +146,7 @@ def test_does_not_set_propagator_if_disabled(sentry_init): | |
| assert propagator is original_propagator | ||
|
|
||
|
|
||
| @responses.activate | ||
| def test_otel_propagation_context(sentry_init): | ||
| responses.add( | ||
| responses.POST, | ||
| url="https://bla.ingest.sentry.io/api/12312012/integration/otlp/v1/traces/", | ||
| status=200, | ||
| ) | ||
|
|
||
| sentry_init( | ||
| dsn="https://[email protected]/12312012", | ||
| integrations=[OTLPIntegration()], | ||
|
|
@@ -145,9 +157,6 @@ def test_otel_propagation_context(sentry_init): | |
| with tracer.start_as_current_span("bar") as span: | ||
| external_propagation_context = get_external_propagation_context() | ||
|
|
||
| # Force flush to ensure spans are exported while mock is active | ||
| get_tracer_provider().force_flush() | ||
|
|
||
| assert external_propagation_context is not None | ||
| (trace_id, span_id) = external_propagation_context | ||
| assert trace_id == format_trace_id(root_span.get_span_context().trace_id) | ||
|
|
@@ -222,3 +231,74 @@ def test_propagator_inject_continue_trace(sentry_init): | |
| assert carrier["baggage"] == incoming_headers["baggage"] | ||
|
|
||
| detach(token) | ||
|
|
||
|
|
||
| def test_capture_exceptions_enabled(sentry_init, capture_events): | ||
| sentry_init( | ||
| dsn="https://[email protected]/12312012", | ||
| integrations=[OTLPIntegration(capture_exceptions=True)], | ||
| ) | ||
|
|
||
| events = capture_events() | ||
|
|
||
| tracer = trace.get_tracer(__name__) | ||
| with tracer.start_as_current_span("test_span") as span: | ||
| try: | ||
| raise ValueError("Test exception") | ||
| except ValueError as e: | ||
| span.record_exception(e) | ||
|
|
||
| (event,) = events | ||
| assert event["exception"]["values"][0]["type"] == "ValueError" | ||
| assert event["exception"]["values"][0]["value"] == "Test exception" | ||
| assert event["exception"]["values"][0]["mechanism"]["type"] == "otlp" | ||
| assert event["exception"]["values"][0]["mechanism"]["handled"] is False | ||
|
|
||
| trace_context = event["contexts"]["trace"] | ||
| assert trace_context["trace_id"] == format_trace_id( | ||
| span.get_span_context().trace_id | ||
| ) | ||
| assert trace_context["span_id"] == format_span_id(span.get_span_context().span_id) | ||
|
|
||
|
|
||
| def test_capture_exceptions_disabled(sentry_init, capture_events): | ||
| sentry_init( | ||
| dsn="https://[email protected]/12312012", | ||
| integrations=[OTLPIntegration(capture_exceptions=False)], | ||
| ) | ||
|
|
||
| events = capture_events() | ||
|
|
||
| tracer = trace.get_tracer(__name__) | ||
| with tracer.start_as_current_span("test_span") as span: | ||
| try: | ||
| raise ValueError("Test exception") | ||
| except ValueError as e: | ||
| span.record_exception(e) | ||
|
|
||
| assert len(events) == 0 | ||
|
|
||
|
|
||
| def test_capture_exceptions_preserves_otel_behavior(sentry_init, capture_events): | ||
| sentry_init( | ||
| dsn="https://[email protected]/12312012", | ||
| integrations=[OTLPIntegration(capture_exceptions=True)], | ||
| ) | ||
|
|
||
| events = capture_events() | ||
|
|
||
| tracer = trace.get_tracer(__name__) | ||
| with tracer.start_as_current_span("test_span") as span: | ||
| try: | ||
| raise ValueError("Test exception") | ||
| except ValueError as e: | ||
| span.record_exception(e, attributes={"foo": "bar"}) | ||
|
|
||
| # Verify the span recorded the exception (OpenTelemetry behavior) | ||
| # The span should have events with the exception information | ||
| (otel_event,) = span._events | ||
| assert otel_event.name == "exception" | ||
| assert otel_event.attributes["foo"] == "bar" | ||
|
|
||
| # verify sentry also captured it | ||
| assert len(events) == 1 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.