|
3 | 3 |
|
4 | 4 | from sentry_sdk.api import continue_trace |
5 | 5 | from sentry_sdk._compat import reraise |
6 | | -from sentry_sdk.consts import OP |
| 6 | +from sentry_sdk.consts import OP, SPANDATA |
7 | 7 | from sentry_sdk.hub import Hub |
8 | 8 | from sentry_sdk.integrations import Integration, DidNotEnable |
9 | 9 | from sentry_sdk.integrations.logging import ignore_logger |
|
13 | 13 | request_body_within_bounds, |
14 | 14 | ) |
15 | 15 | from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE |
| 16 | +from sentry_sdk.tracing_utils import should_propagate_trace |
16 | 17 | from sentry_sdk.utils import ( |
17 | 18 | capture_internal_exceptions, |
18 | 19 | event_from_exception, |
19 | 20 | logger, |
| 21 | + parse_url, |
20 | 22 | parse_version, |
21 | 23 | transaction_from_function, |
22 | 24 | HAS_REAL_CONTEXTVARS, |
|
38 | 40 | if TYPE_CHECKING: |
39 | 41 | from aiohttp.web_request import Request |
40 | 42 | from aiohttp.abc import AbstractMatchInfo |
41 | | - from aiohttp import TraceRequestStartParams |
42 | | - from aiohttp import TraceRequestEndParams |
| 43 | + from aiohttp import TraceRequestStartParams, TraceRequestEndParams |
43 | 44 | from types import SimpleNamespace |
44 | 45 | from typing import Any |
45 | 46 | from typing import Dict |
|
55 | 56 | TRANSACTION_STYLE_VALUES = ("handler_name", "method_and_path_pattern") |
56 | 57 |
|
57 | 58 |
|
58 | | -def create_trace_config(): |
59 | | - # type: () -> TraceConfig |
60 | | - async def on_request_start(session, trace_config_ctx, params): |
61 | | - # type: (ClientSession, SimpleNamespace, TraceRequestStartParams) -> None |
62 | | - hub = Hub.current |
63 | | - if hub.get_integration(AioHttpIntegration) is None: |
64 | | - return |
65 | | - |
66 | | - method = params.method.upper() |
67 | | - request_url = str(params.url) |
68 | | - |
69 | | - span = hub.start_span( |
70 | | - op=OP.HTTP_CLIENT, description="%s %s" % (method, request_url) |
71 | | - ) |
72 | | - span.set_data("method", method) |
73 | | - span.set_data("url", request_url) |
74 | | - |
75 | | - for key, value in hub.iter_trace_propagation_headers(span): |
76 | | - logger.debug( |
77 | | - "[Tracing] Adding `{key}` header {value} to outgoing request to {url}.".format( |
78 | | - key=key, value=value, url=params.url |
79 | | - ) |
80 | | - ) |
81 | | - params.headers[key] = value |
82 | | - |
83 | | - trace_config_ctx.span = span |
84 | | - |
85 | | - async def on_request_end(session, trace_config_ctx, params): |
86 | | - # type: (ClientSession, SimpleNamespace, TraceRequestEndParams) -> None |
87 | | - if trace_config_ctx.span is None: |
88 | | - return |
89 | | - |
90 | | - span = trace_config_ctx.span |
91 | | - span.set_data("status_code", int(params.response.status)) |
92 | | - span.set_http_status(int(params.response.status)) |
93 | | - span.set_data("reason", params.response.reason) |
94 | | - span.finish() |
95 | | - |
96 | | - trace_config = TraceConfig() |
97 | | - |
98 | | - trace_config.on_request_start.append(on_request_start) |
99 | | - trace_config.on_request_end.append(on_request_end) |
100 | | - |
101 | | - return trace_config |
102 | | - |
103 | | - |
104 | 59 | class AioHttpIntegration(Integration): |
105 | 60 | identifier = "aiohttp" |
106 | 61 |
|
@@ -233,6 +188,57 @@ def init(*args, **kwargs): |
233 | 188 | ClientSession.__init__ = init |
234 | 189 |
|
235 | 190 |
|
| 191 | +def create_trace_config(): |
| 192 | + # type: () -> TraceConfig |
| 193 | + async def on_request_start(session, trace_config_ctx, params): |
| 194 | + # type: (ClientSession, SimpleNamespace, TraceRequestStartParams) -> None |
| 195 | + hub = Hub.current |
| 196 | + if hub.get_integration(AioHttpIntegration) is None: |
| 197 | + return |
| 198 | + |
| 199 | + method = params.method.upper() |
| 200 | + |
| 201 | + parsed_url = None |
| 202 | + with capture_internal_exceptions(): |
| 203 | + parsed_url = parse_url(str(params.url), sanitize=False) |
| 204 | + |
| 205 | + span = hub.start_span( |
| 206 | + op=OP.HTTP_CLIENT, description="%s %s" % (method, parsed_url) |
| 207 | + ) |
| 208 | + span.set_data(SPANDATA.HTTP_METHOD, method) |
| 209 | + span.set_data("url", parsed_url) |
| 210 | + span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) |
| 211 | + span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) |
| 212 | + |
| 213 | + if should_propagate_trace(hub, parsed_url): |
| 214 | + for key, value in hub.iter_trace_propagation_headers(span): |
| 215 | + logger.debug( |
| 216 | + "[Tracing] Adding `{key}` header {value} to outgoing request to {url}.".format( |
| 217 | + key=key, value=value, url=params.url |
| 218 | + ) |
| 219 | + ) |
| 220 | + params.headers[key] = value |
| 221 | + |
| 222 | + trace_config_ctx.span = span |
| 223 | + |
| 224 | + async def on_request_end(session, trace_config_ctx, params): |
| 225 | + # type: (ClientSession, SimpleNamespace, TraceRequestEndParams) -> None |
| 226 | + if trace_config_ctx.span is None: |
| 227 | + return |
| 228 | + |
| 229 | + span = trace_config_ctx.span |
| 230 | + span.set_http_status(int(params.response.status)) |
| 231 | + span.set_data("reason", params.response.reason) |
| 232 | + span.finish() |
| 233 | + |
| 234 | + trace_config = TraceConfig() |
| 235 | + |
| 236 | + trace_config.on_request_start.append(on_request_start) |
| 237 | + trace_config.on_request_end.append(on_request_end) |
| 238 | + |
| 239 | + return trace_config |
| 240 | + |
| 241 | + |
236 | 242 | def _make_request_processor(weak_request): |
237 | 243 | # type: (Callable[[], Request]) -> EventProcessor |
238 | 244 | def aiohttp_processor( |
|
0 commit comments