-
Notifications
You must be signed in to change notification settings - Fork 647
ref(aiohttp): Move crumbs to integration #7135
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
base: master
Are you sure you want to change the base?
Changes from all commits
07a3ea3
2f91234
c3aea86
15d2e2f
53ab7f0
0ea13df
2f54487
8e6087f
91fe6a4
5858c87
906838b
4e093aa
040f223
7472af9
8ace993
f492ba4
8e3adaf
2a172c1
1239d84
9adbede
abdc32b
d03072b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ | |
| TransactionSource, | ||
| ) | ||
| from sentry_sdk.tracing_utils import ( | ||
| add_http_breadcrumb, | ||
| add_http_request_source, | ||
| has_span_streaming_enabled, | ||
| should_propagate_trace, | ||
|
|
@@ -388,57 +389,64 @@ async def on_request_start( | |
| with capture_internal_exceptions(): | ||
| parsed_url = parse_url(str(params.url), sanitize=False) | ||
|
|
||
| breadcrumb = {} | ||
|
|
||
| span_name = "%s %s" % ( | ||
| method, | ||
| parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, | ||
| ) | ||
|
|
||
| span: "Union[Span, StreamedSpan, None]" | ||
| span: "Union[Span, StreamedSpan, None]" = None | ||
| if has_span_streaming_enabled(client.options): | ||
| if sentry_sdk.traces.get_current_span() is None: | ||
| span = None | ||
| else: | ||
| attributes: "Attributes" = { | ||
| "sentry.op": OP.HTTP_CLIENT, | ||
| "sentry.origin": AioHttpIntegration.origin, | ||
| "http.request.method": method, | ||
| } | ||
| if parsed_url is not None: | ||
| if has_data_collection_enabled(client.options): | ||
| url_full = parsed_url.url | ||
| attributes["url.path"] = params.url.path | ||
|
|
||
| if parsed_url.query: | ||
| filtered_query = ( | ||
| _apply_data_collection_filtering_to_query_string( | ||
| query_string=parsed_url.query, | ||
| behaviour=client.options["data_collection"][ | ||
| "url_query_params" | ||
| ], | ||
| ) | ||
| attributes: "Attributes" = { | ||
| "sentry.op": OP.HTTP_CLIENT, | ||
| "sentry.origin": AioHttpIntegration.origin, | ||
| "http.request.method": method, | ||
| } | ||
| if parsed_url is not None: | ||
| if has_data_collection_enabled(client.options): | ||
| url_full = parsed_url.url | ||
| attributes["url.path"] = params.url.path | ||
|
|
||
| if parsed_url.query: | ||
| filtered_query = ( | ||
| _apply_data_collection_filtering_to_query_string( | ||
| query_string=parsed_url.query, | ||
| behaviour=client.options["data_collection"][ | ||
| "url_query_params" | ||
| ], | ||
| ) | ||
| if filtered_query: | ||
| attributes["url.query"] = filtered_query | ||
| url_full += "?" + filtered_query | ||
|
|
||
| if parsed_url.fragment: | ||
| attributes["url.fragment"] = parsed_url.fragment | ||
| url_full += "#" + parsed_url.fragment | ||
|
|
||
| attributes["url.full"] = url_full | ||
| elif should_send_default_pii(): | ||
| url_full = parsed_url.url | ||
| attributes["url.path"] = params.url.path | ||
|
|
||
| if parsed_url.query: | ||
| url_full += "?" + parsed_url.query | ||
| attributes["url.query"] = parsed_url.query | ||
| if parsed_url.fragment: | ||
| url_full += "#" + parsed_url.fragment | ||
| attributes["url.fragment"] = parsed_url.fragment | ||
|
|
||
| attributes["url.full"] = url_full | ||
|
|
||
| ) | ||
| if filtered_query: | ||
| attributes["url.query"] = filtered_query | ||
| url_full += "?" + filtered_query | ||
| breadcrumb[SPANDATA.HTTP_QUERY] = filtered_query | ||
|
|
||
| if parsed_url.fragment: | ||
| attributes["url.fragment"] = parsed_url.fragment | ||
| url_full += "#" + parsed_url.fragment | ||
| breadcrumb[SPANDATA.HTTP_FRAGMENT] = parsed_url.fragment | ||
|
|
||
| attributes["url.full"] = url_full | ||
| breadcrumb["url"] = url_full | ||
|
|
||
| elif should_send_default_pii(): | ||
| url_full = parsed_url.url | ||
| attributes["url.path"] = params.url.path | ||
|
|
||
| if parsed_url.query: | ||
| url_full += "?" + parsed_url.query | ||
| attributes["url.query"] = parsed_url.query | ||
| breadcrumb[SPANDATA.HTTP_QUERY] = parsed_url.query | ||
| if parsed_url.fragment: | ||
| url_full += "#" + parsed_url.fragment | ||
| attributes["url.fragment"] = parsed_url.fragment | ||
| breadcrumb[SPANDATA.HTTP_FRAGMENT] = parsed_url.fragment | ||
|
|
||
| attributes["url.full"] = url_full | ||
| breadcrumb["url"] = url_full | ||
|
|
||
| if sentry_sdk.traces.get_current_span() is not None: | ||
| span = sentry_sdk.traces.start_span( | ||
| name=span_name, attributes=attributes | ||
| ) | ||
|
|
@@ -453,6 +461,13 @@ async def on_request_start( | |
| legacy_span.set_data("url", parsed_url.url) | ||
| legacy_span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) | ||
| legacy_span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) | ||
| breadcrumb.update( | ||
| { | ||
| SPANDATA.HTTP_QUERY: parsed_url.query, | ||
| SPANDATA.HTTP_FRAGMENT: parsed_url.fragment, | ||
| "url": parsed_url.url, | ||
| } | ||
| ) | ||
| span = legacy_span | ||
|
|
||
| if should_propagate_trace(client, str(params.url)): | ||
|
|
@@ -475,18 +490,35 @@ async def on_request_start( | |
| else: | ||
| params.headers[key] = value | ||
|
|
||
| trace_config_ctx.span = span | ||
| trace_config_ctx._sentry_span = span | ||
| trace_config_ctx._sentry_breadcrumb = breadcrumb | ||
|
|
||
| async def on_request_end( | ||
| session: "ClientSession", | ||
| trace_config_ctx: "SimpleNamespace", | ||
| params: "TraceRequestEndParams", | ||
| ) -> None: | ||
| if trace_config_ctx.span is None: | ||
| status = int(params.response.status) | ||
|
|
||
| breadcrumb = trace_config_ctx._sentry_breadcrumb | ||
|
sentrivana marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Accessing Suggested FixIn Prompt for AI Agent |
||
| if breadcrumb is not None: | ||
| breadcrumb.update( | ||
| { | ||
| SPANDATA.HTTP_METHOD: params.method.upper(), | ||
| SPANDATA.HTTP_STATUS_CODE: status, | ||
| "reason": params.response.reason, | ||
| } | ||
| ) | ||
|
|
||
| add_http_breadcrumb( | ||
| status, | ||
| breadcrumb, | ||
| ) | ||
|
|
||
| if trace_config_ctx._sentry_span is None: | ||
| return | ||
|
|
||
| span = trace_config_ctx.span | ||
| status = int(params.response.status) | ||
| span = trace_config_ctx._sentry_span | ||
|
|
||
| if isinstance(span, StreamedSpan): | ||
| span.set_attribute("http.response.status_code", status) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -210,10 +210,26 @@ def record_sql_queries( | |||||||
| yield span | ||||||||
|
|
||||||||
|
|
||||||||
| def add_http_breadcrumb(status_code, data): | ||||||||
| # type: (Optional[int], dict[str, Any]) -> None | ||||||||
|
Comment on lines
+213
to
+214
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there something preventing this type definition from living inline like the following?
Suggested change
Or is it in a comment as a matter of personal preference? |
||||||||
| level = None | ||||||||
| if status_code: | ||||||||
| if 500 <= status_code <= 599: | ||||||||
| level = "error" | ||||||||
| elif 400 <= status_code <= 499: | ||||||||
| level = "warning" | ||||||||
|
|
||||||||
| kwargs: "dict[str, Any]" = {"type": "http", "category": "httplib", "data": data} | ||||||||
| if level: | ||||||||
| kwargs["level"] = level | ||||||||
|
|
||||||||
| sentry_sdk.add_breadcrumb(**kwargs) | ||||||||
|
|
||||||||
|
|
||||||||
| def maybe_create_breadcrumbs_from_span( | ||||||||
| scope: "sentry_sdk.Scope", span: "sentry_sdk.tracing.Span" | ||||||||
| ) -> None: | ||||||||
| if span.op == OP.HTTP_CLIENT: | ||||||||
| if span.op == OP.HTTP_CLIENT and span.origin not in ("auto.http.aiohttp",): | ||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just here to make sure we're not creating breadcrumbs the old way in transaction-based tracing anymore. Once all HTTP client integrations have been migrated, the whole function will go away |
||||||||
| level = None | ||||||||
| status_code = span._data.get(SPANDATA.HTTP_STATUS_CODE) | ||||||||
| if status_code: | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This had to be moved after the PII redaction, because even if we don't want to create a span, we do want to create a breadcrumb, and we need to apply the same PII redacting logic to breadcrumbs.