|
14 | 14 | SENSITIVE_DATA_SUBSTITUTE, |
15 | 15 | capture_internal_exceptions, |
16 | 16 | ensure_integration_enabled, |
| 17 | + nullcontext, |
17 | 18 | parse_url, |
18 | 19 | ) |
19 | 20 |
|
@@ -61,39 +62,45 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": |
61 | 62 |
|
62 | 63 | if is_span_streaming_enabled: |
63 | 64 | if sentry_sdk.traces.get_current_span() is None: |
64 | | - propagate_trace_headers(client, request) |
65 | | - return real_send(self, request, **kwargs) |
| 65 | + span_ctx = nullcontext() |
| 66 | + else: |
| 67 | + span_ctx = sentry_sdk.traces.start_span( |
| 68 | + name="%s %s" |
| 69 | + % ( |
| 70 | + request.method, |
| 71 | + parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, |
| 72 | + ), |
| 73 | + attributes={ |
| 74 | + "sentry.op": OP.HTTP_CLIENT, |
| 75 | + "sentry.origin": Httpx2Integration.origin, |
| 76 | + "http.request.method": request.method, |
| 77 | + }, |
| 78 | + ) |
66 | 79 |
|
67 | 80 | url_attributes = get_url_attributes(client, parsed_url) |
68 | 81 |
|
69 | | - with sentry_sdk.traces.start_span( |
70 | | - name="%s %s" |
71 | | - % ( |
72 | | - request.method, |
73 | | - parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, |
74 | | - ), |
75 | | - attributes={ |
76 | | - "sentry.op": OP.HTTP_CLIENT, |
77 | | - "sentry.origin": Httpx2Integration.origin, |
78 | | - "http.request.method": request.method, |
79 | | - }, |
80 | | - ) as streamed_span: |
81 | | - attributes: "Attributes" = dict(url_attributes) |
82 | | - |
| 82 | + with span_ctx as streamed_span: |
83 | 83 | propagate_trace_headers(client, request) |
84 | 84 |
|
85 | 85 | try: |
86 | 86 | rv = real_send(self, request, **kwargs) |
87 | 87 |
|
88 | | - streamed_span.status = "error" if rv.status_code >= 400 else "ok" |
89 | | - attributes["http.response.status_code"] = rv.status_code |
| 88 | + if streamed_span is not None: |
| 89 | + streamed_span.status = ( |
| 90 | + "error" if rv.status_code >= 400 else "ok" |
| 91 | + ) |
| 92 | + streamed_span.set_attribute( |
| 93 | + "http.response.status_code", rv.status_code |
| 94 | + ) |
90 | 95 | finally: |
91 | | - streamed_span.set_attributes(attributes) |
92 | | - |
93 | | - # Needs to happen within the context manager as we want to attach the |
94 | | - # final data before the span finishes and is sent for ingesting. |
95 | | - with capture_internal_exceptions(): |
96 | | - add_http_request_source(streamed_span) |
| 96 | + if streamed_span is not None: |
| 97 | + streamed_span.set_attributes(url_attributes) |
| 98 | + |
| 99 | + if streamed_span is not None: |
| 100 | + # Needs to happen within the context manager as we want to attach the |
| 101 | + # final data before the span finishes and is sent for ingesting. |
| 102 | + with capture_internal_exceptions(): |
| 103 | + add_http_request_source(streamed_span) |
97 | 104 | else: |
98 | 105 | with sentry_sdk.start_span( |
99 | 106 | op=OP.HTTP_CLIENT, |
@@ -138,7 +145,7 @@ def send(self: "Client", request: "Request", **kwargs: "Any") -> "Response": |
138 | 145 | elif url_attributes: |
139 | 146 | breadcrumb_data.update( |
140 | 147 | { |
141 | | - "url": url_attributes.get("url.full", parsed_url.url), |
| 148 | + "url": url_attributes.get("url.full", ""), |
142 | 149 | SPANDATA.HTTP_QUERY: url_attributes.get("url.query", ""), |
143 | 150 | SPANDATA.HTTP_FRAGMENT: url_attributes.get("url.fragment", ""), |
144 | 151 | } |
@@ -170,39 +177,45 @@ async def send( |
170 | 177 |
|
171 | 178 | if is_span_streaming_enabled: |
172 | 179 | if sentry_sdk.traces.get_current_span() is None: |
173 | | - propagate_trace_headers(client, request) |
174 | | - return await real_send(self, request, **kwargs) |
| 180 | + span_ctx = nullcontext() |
| 181 | + else: |
| 182 | + span_ctx = sentry_sdk.traces.start_span( |
| 183 | + name="%s %s" |
| 184 | + % ( |
| 185 | + request.method, |
| 186 | + parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, |
| 187 | + ), |
| 188 | + attributes={ |
| 189 | + "sentry.op": OP.HTTP_CLIENT, |
| 190 | + "sentry.origin": Httpx2Integration.origin, |
| 191 | + "http.request.method": request.method, |
| 192 | + }, |
| 193 | + ) |
175 | 194 |
|
176 | 195 | url_attributes = get_url_attributes(client, parsed_url) |
177 | 196 |
|
178 | | - with sentry_sdk.traces.start_span( |
179 | | - name="%s %s" |
180 | | - % ( |
181 | | - request.method, |
182 | | - parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE, |
183 | | - ), |
184 | | - attributes={ |
185 | | - "sentry.op": OP.HTTP_CLIENT, |
186 | | - "sentry.origin": Httpx2Integration.origin, |
187 | | - "http.request.method": request.method, |
188 | | - }, |
189 | | - ) as streamed_span: |
190 | | - attributes: "Attributes" = dict(url_attributes) |
191 | | - |
| 197 | + with span_ctx as streamed_span: |
192 | 198 | propagate_trace_headers(client, request) |
193 | 199 |
|
194 | 200 | try: |
195 | 201 | rv = await real_send(self, request, **kwargs) |
196 | 202 |
|
197 | | - streamed_span.status = "error" if rv.status_code >= 400 else "ok" |
198 | | - attributes["http.response.status_code"] = rv.status_code |
| 203 | + if streamed_span is not None: |
| 204 | + streamed_span.status = ( |
| 205 | + "error" if rv.status_code >= 400 else "ok" |
| 206 | + ) |
| 207 | + streamed_span.set_attribute( |
| 208 | + "http.response.status_code", rv.status_code |
| 209 | + ) |
199 | 210 | finally: |
200 | | - streamed_span.set_attributes(attributes) |
201 | | - |
202 | | - # Needs to happen within the context manager as we want to attach the |
203 | | - # final data before the span finishes and is sent for ingesting. |
204 | | - with capture_internal_exceptions(): |
205 | | - add_http_request_source(streamed_span) |
| 211 | + if streamed_span is not None: |
| 212 | + streamed_span.set_attributes(url_attributes) |
| 213 | + |
| 214 | + if streamed_span is not None: |
| 215 | + # Needs to happen within the context manager as we want to attach the |
| 216 | + # final data before the span finishes and is sent for ingesting. |
| 217 | + with capture_internal_exceptions(): |
| 218 | + add_http_request_source(streamed_span) |
206 | 219 | else: |
207 | 220 | with sentry_sdk.start_span( |
208 | 221 | op=OP.HTTP_CLIENT, |
@@ -246,7 +259,7 @@ async def send( |
246 | 259 | elif url_attributes: |
247 | 260 | breadcrumb_data.update( |
248 | 261 | { |
249 | | - "url": url_attributes.get("url.full", parsed_url.url), |
| 262 | + "url": url_attributes.get("url.full", ""), |
250 | 263 | SPANDATA.HTTP_QUERY: url_attributes.get("url.query", ""), |
251 | 264 | SPANDATA.HTTP_FRAGMENT: url_attributes.get("url.fragment", ""), |
252 | 265 | } |
|
0 commit comments