Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 660
feat(sanic): Support span streaming#6319
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
| @@ -346,6 +346,7 @@ def __init__( | ||
| expected_status: int, | ||
| expected_transaction_name: "Optional[str]", | ||
| expected_source: "Optional[str]" = None, | ||
| streaming_compatible: bool = True, | ||
| ) -> None: | ||
| """ | ||
| expected_transaction_name of None indicates we expect to not receive a transaction | ||
| @@ -355,11 +356,13 @@ def __init__( | ||
| self.expected_status = expected_status | ||
| self.expected_transaction_name = expected_transaction_name | ||
| self.expected_source = expected_source | ||
| self.streaming_compatible = streaming_compatible | ||
| @pytest.mark.skipif( | ||
| not PERFORMANCE_SUPPORTED, reason="Performance not supported on this Sanic version" | ||
| ) | ||
| @pytest.mark.parametrize("span_streaming", [True, False]) | ||
| @pytest.mark.parametrize( | ||
| "test_config", | ||
| [ | ||
| @@ -371,6 +374,14 @@ def __init__( | ||
| expected_transaction_name="hi", | ||
| expected_source=TransactionSource.COMPONENT, | ||
| ), | ||
| TransactionTestConfig( | ||
| # Transaction for successful page load with query string | ||
| integration_args=(), | ||
| url="/message?foo=bar", | ||
| expected_status=200, | ||
| expected_transaction_name="hi", | ||
| expected_source=TransactionSource.COMPONENT, | ||
| ), | ||
| TransactionTestConfig( | ||
| # Transaction still recorded when we have an internal server error | ||
| integration_args=(), | ||
| @@ -385,6 +396,7 @@ def __init__( | ||
| url="/404", | ||
| expected_status=404, | ||
| expected_transaction_name=None, | ||
| streaming_compatible=False, | ||
| ), | ||
| TransactionTestConfig( | ||
| # With no ignored HTTP statuses, we should get transactions for 404 errors | ||
| @@ -400,6 +412,7 @@ def __init__( | ||
| url="/message", | ||
| expected_status=200, | ||
| expected_transaction_name=None, | ||
| streaming_compatible=False, | ||
| ), | ||
| ], | ||
| ) | ||
| @@ -408,57 +421,123 @@ def test_transactions( | ||
| sentry_init: "Any", | ||
| app: "Any", | ||
| capture_events: "Any", | ||
| capture_items: "Any", | ||
| span_streaming: bool, | ||
| ) -> None: | ||
| if span_streaming and not test_config.streaming_compatible: | ||
| pytest.skip("unsampled_statuses is not supported in span streaming mode") | ||
| # Init the SanicIntegration with the desired arguments | ||
| sentry_init( | ||
| integrations=[SanicIntegration(*test_config.integration_args)], | ||
| traces_sample_rate=1.0, | ||
| _experiments={"trace_lifecycle": "stream" if span_streaming else "static"}, | ||
| ) | ||
| events = capture_events() | ||
| if span_streaming: | ||
| items = capture_items("span") | ||
| else: | ||
| events = capture_events() | ||
| # Make request to the desired URL | ||
| c = get_client(app) | ||
| with c as client: | ||
| _, response = client.get(test_config.url) | ||
| assert response.status == test_config.expected_status | ||
| # Extract the transaction events by inspecting the event types. We should at most have 1 transaction event. | ||
| transaction_events = [ | ||
| e for e in events if "type" in e and e["type"] == "transaction" | ||
| ] | ||
| assert len(transaction_events) <= 1 | ||
| # Get the only transaction event, or set to None if there are no transaction events. | ||
| (transaction_event, *_) = [*transaction_events, None] | ||
| # We should have no transaction event if and only if we expect no transactions | ||
| assert (transaction_event is None) == ( | ||
| test_config.expected_transaction_name is None | ||
| ) | ||
| sentry_sdk.flush() | ||
| if span_streaming: | ||
| segments = [ | ||
| i.payload | ||
| for i in items | ||
| if i.payload["attributes"].get("sentry.origin") == "auto.http.sanic" | ||
| and i.payload["is_segment"] | ||
| ] | ||
| assert len(segments) <= 1 | ||
| (segment, *_) = [*segments, None] | ||
| assert (segment is None) == (test_config.expected_transaction_name is None) | ||
| if segment is not None: | ||
| assert segment["name"] == test_config.expected_transaction_name | ||
| assert ( | ||
| segment["attributes"]["sentry.span.source"] | ||
| == test_config.expected_source | ||
| ) | ||
| # If a transaction was expected, ensure it is correct | ||
| assert ( | ||
| transaction_event is None | ||
| or transaction_event["transaction"] == test_config.expected_transaction_name | ||
| ) | ||
| assert ( | ||
| transaction_event is None | ||
| or transaction_event["transaction_info"]["source"] | ||
| == test_config.expected_source | ||
| ) | ||
| attrs = segment["attributes"] | ||
| assert attrs["http.request.method"] == "GET" | ||
| assert attrs["url.full"].endswith(test_config.url) | ||
| if "?" in test_config.url: | ||
| assert attrs["http.query"] == test_config.url.split("?", 1)[1] | ||
| assert attrs["network.protocol.name"] == "http" | ||
sentry-warden[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| header_keys = { | ||
| key[len("http.request.header.") :] | ||
| for key in attrs | ||
| if key.startswith("http.request.header.") | ||
| } | ||
| assert header_keys >= {"accept", "accept-encoding", "host", "user-agent"} | ||
| assert attrs["http.response.status_code"] == test_config.expected_status | ||
| assert segment["status"] == ( | ||
| "error" if test_config.expected_status >= 400 else "ok" | ||
| ) | ||
| else: | ||
| # Extract the transaction events by inspecting the event types. We should at most have 1 transaction event. | ||
| transaction_events = [ | ||
| e for e in events if "type" in e and e["type"] == "transaction" | ||
| ] | ||
| assert len(transaction_events) <= 1 | ||
| # Get the only transaction event, or set to None if there are no transaction events. | ||
| (transaction_event, *_) = [*transaction_events, None] | ||
| # We should have no transaction event if and only if we expect no transactions | ||
| assert (transaction_event is None) == ( | ||
| test_config.expected_transaction_name is None | ||
| ) | ||
| # If a transaction was expected, ensure it is correct | ||
| assert ( | ||
| transaction_event is None | ||
| or transaction_event["transaction"] == test_config.expected_transaction_name | ||
| ) | ||
| assert ( | ||
| transaction_event is None | ||
| or transaction_event["transaction_info"]["source"] | ||
| == test_config.expected_source | ||
| ) | ||
| @pytest.mark.skipif( | ||
| not PERFORMANCE_SUPPORTED, reason="Performance not supported on this Sanic version" | ||
| ) | ||
| def test_span_origin(sentry_init, app, capture_events): | ||
| sentry_init(integrations=[SanicIntegration()], traces_sample_rate=1.0) | ||
| events = capture_events() | ||
| @pytest.mark.parametrize("span_streaming", [True, False]) | ||
| def test_span_origin(sentry_init, app, capture_events, capture_items, span_streaming): | ||
| sentry_init( | ||
| integrations=[SanicIntegration()], | ||
| traces_sample_rate=1.0, | ||
| _experiments={"trace_lifecycle": "stream" if span_streaming else "static"}, | ||
| ) | ||
| if span_streaming: | ||
| items = capture_items("span") | ||
| else: | ||
| events = capture_events() | ||
| c = get_client(app) | ||
| with c as client: | ||
| client.get("/message?foo=bar") | ||
| (_, event) = events | ||
| sentry_sdk.flush() | ||
| assert event["contexts"]["trace"]["origin"] == "auto.http.sanic" | ||
| if span_streaming: | ||
| (segment,) = [ | ||
| i.payload | ||
| for i in items | ||
| if i.payload["attributes"].get("sentry.origin") == "auto.http.sanic" | ||
| ] | ||
| assert segment["attributes"]["sentry.origin"] == "auto.http.sanic" | ||
| else: | ||
| (_, event) = events | ||
| assert event["contexts"]["trace"]["origin"] == "auto.http.sanic" | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.