Skip to content

Commit 33b121a

Browse files
chore(boto3): Remove transaction-based tracing
1 parent 36c1aa3 commit 33b121a

4 files changed

Lines changed: 224 additions & 489 deletions

File tree

sentry_sdk/integrations/boto3.py

Lines changed: 39 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
from sentry_sdk.integrations import DidNotEnable, Integration, _check_minimum_version
77
from sentry_sdk.scope import should_send_default_pii
88
from sentry_sdk.traces import StreamedSpan
9-
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, Span
9+
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME
1010
from sentry_sdk.tracing_utils import (
1111
add_http_breadcrumb,
1212
add_sentry_baggage_to_headers,
13-
has_span_streaming_enabled,
1413
should_propagate_trace,
1514
)
1615
from sentry_sdk.utils import (
@@ -20,7 +19,7 @@
2019
)
2120

2221
if TYPE_CHECKING:
23-
from typing import Any, Dict, Optional, Type, Union
22+
from typing import Any, Dict, Optional, Type
2423

2524
from botocore.model import ServiceId
2625

@@ -78,11 +77,30 @@ def _sentry_request_created(
7877

7978
breadcrumb: "dict[str, Any]" = {}
8079

81-
is_span_streaming_enabled = has_span_streaming_enabled(client.options)
82-
span: "Union[Span, StreamedSpan, None]" = None
83-
if is_span_streaming_enabled:
80+
span: "Optional[StreamedSpan]" = None
81+
if parsed_url and should_send_default_pii():
82+
breadcrumb.update(
83+
{
84+
SPANDATA.URL_FULL: parsed_url.url,
85+
SPANDATA.URL_QUERY: parsed_url.query,
86+
SPANDATA.URL_FRAGMENT: parsed_url.fragment,
87+
}
88+
)
89+
90+
if request.method is not None:
91+
breadcrumb[SPANDATA.HTTP_REQUEST_METHOD] = request.method
92+
93+
if sentry_sdk.traces.get_current_span() is not None:
94+
span = sentry_sdk.traces.start_span(
95+
name=description,
96+
attributes={
97+
"sentry.op": OP.HTTP_CLIENT,
98+
"sentry.origin": Boto3Integration.origin,
99+
SPANDATA.RPC_METHOD: f"{service_id}/{operation_name}",
100+
},
101+
)
84102
if parsed_url and should_send_default_pii():
85-
breadcrumb.update(
103+
span.set_attributes(
86104
{
87105
SPANDATA.URL_FULL: parsed_url.url,
88106
SPANDATA.URL_QUERY: parsed_url.query,
@@ -91,54 +109,7 @@ def _sentry_request_created(
91109
)
92110

93111
if request.method is not None:
94-
breadcrumb[SPANDATA.HTTP_REQUEST_METHOD] = request.method
95-
96-
if sentry_sdk.traces.get_current_span() is not None:
97-
span = sentry_sdk.traces.start_span(
98-
name=description,
99-
attributes={
100-
"sentry.op": OP.HTTP_CLIENT,
101-
"sentry.origin": Boto3Integration.origin,
102-
SPANDATA.RPC_METHOD: f"{service_id}/{operation_name}",
103-
},
104-
)
105-
if parsed_url and should_send_default_pii():
106-
span.set_attributes(
107-
{
108-
SPANDATA.URL_FULL: parsed_url.url,
109-
SPANDATA.URL_QUERY: parsed_url.query,
110-
SPANDATA.URL_FRAGMENT: parsed_url.fragment,
111-
}
112-
)
113-
114-
if request.method is not None:
115-
span.set_attribute(SPANDATA.HTTP_REQUEST_METHOD, request.method)
116-
else:
117-
span = sentry_sdk.start_span(
118-
op=OP.HTTP_CLIENT,
119-
name=description,
120-
origin=Boto3Integration.origin,
121-
)
122-
123-
if parsed_url:
124-
span.set_data("aws.request.url", parsed_url.url)
125-
span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query)
126-
span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment)
127-
breadcrumb.update(
128-
{
129-
"aws.request.url": parsed_url.url,
130-
SPANDATA.HTTP_QUERY: parsed_url.query,
131-
SPANDATA.HTTP_FRAGMENT: parsed_url.fragment,
132-
}
133-
)
134-
135-
if request.method is not None:
136-
span.set_data(SPANDATA.HTTP_METHOD, request.method)
137-
breadcrumb[SPANDATA.HTTP_METHOD] = request.method
138-
139-
# We do it in order for subsequent http calls/retries be
140-
# attached to this span.
141-
span.__enter__()
112+
span.set_attribute(SPANDATA.HTTP_REQUEST_METHOD, request.method)
142113

143114
add_http_breadcrumb(None, breadcrumb)
144115

@@ -205,7 +176,7 @@ def _replace_header(request: "AWSRequest", key: str, value: str) -> None:
205176
def _sentry_after_call(
206177
context: "Dict[str, Any]", parsed: "Dict[str, Any]", **kwargs: "Any"
207178
) -> None:
208-
span: "Optional[Union[Span, StreamedSpan]]" = context.pop("_sentrysdk_span", None)
179+
span: "Optional[StreamedSpan]" = context.pop("_sentrysdk_span", None)
209180

210181
# Span could be absent if the integration is disabled.
211182
if span is None:
@@ -217,22 +188,14 @@ def _sentry_after_call(
217188
if not isinstance(body, StreamingBody):
218189
return
219190

220-
streaming_span: "Union[Span, StreamedSpan]"
221-
if isinstance(span, StreamedSpan):
222-
streaming_span = sentry_sdk.traces.start_span(
223-
name=span.name,
224-
parent_span=span,
225-
attributes={
226-
"sentry.op": OP.HTTP_CLIENT_STREAM,
227-
"sentry.origin": Boto3Integration.origin,
228-
},
229-
)
230-
else:
231-
streaming_span = span.start_child(
232-
op=OP.HTTP_CLIENT_STREAM,
233-
name=span.description,
234-
origin=Boto3Integration.origin,
235-
)
191+
streaming_span = sentry_sdk.traces.start_span(
192+
name=span.name,
193+
parent_span=span,
194+
attributes={
195+
"sentry.op": OP.HTTP_CLIENT_STREAM,
196+
"sentry.origin": Boto3Integration.origin,
197+
},
198+
)
236199

237200
orig_read = body.read
238201
orig_close = body.close
@@ -243,25 +206,16 @@ def sentry_streaming_body_read(*args: "Any", **kwargs: "Any") -> bytes:
243206
if ret:
244207
return ret
245208

246-
if isinstance(streaming_span, StreamedSpan):
247-
streaming_span.end()
248-
else:
249-
streaming_span.finish()
209+
streaming_span.end()
250210
return ret
251211
except Exception:
252-
if isinstance(streaming_span, StreamedSpan):
253-
streaming_span.end()
254-
else:
255-
streaming_span.finish()
212+
streaming_span.end()
256213
raise
257214

258215
body.read = sentry_streaming_body_read # type: ignore
259216

260217
def sentry_streaming_body_close(*args: "Any", **kwargs: "Any") -> None:
261-
if isinstance(streaming_span, StreamedSpan):
262-
streaming_span.end()
263-
else:
264-
streaming_span.finish()
218+
streaming_span.end()
265219
orig_close(*args, **kwargs)
266220

267221
body.close = sentry_streaming_body_close # type: ignore
@@ -270,7 +224,7 @@ def sentry_streaming_body_close(*args: "Any", **kwargs: "Any") -> None:
270224
def _sentry_after_call_error(
271225
context: "Dict[str, Any]", exception: "Type[BaseException]", **kwargs: "Any"
272226
) -> None:
273-
span: "Optional[Union[Span, StreamedSpan]]" = context.pop("_sentrysdk_span", None)
227+
span: "Optional[StreamedSpan]" = context.pop("_sentrysdk_span", None)
274228

275229
# Span could be absent if the integration is disabled.
276230
if span is None:

tests/integrations/boto3/test_aws_http_connection.py

Lines changed: 37 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,20 @@ def _request(server, headers, path="/"):
4545
connection.close()
4646

4747

48-
@pytest.mark.parametrize("span_streaming", [False, True])
4948
def test_aws_http_connection_adds_missing_unsigned_propagation_headers(
50-
sentry_init, local_http_server, span_streaming
49+
sentry_init,
50+
local_http_server,
5151
):
5252
"""Add missing unsigned `sentry-trace` and `baggage`."""
5353
sentry_init(
5454
traces_sample_rate=1.0,
55-
trace_lifecycle="stream" if span_streaming else "static",
55+
trace_lifecycle="stream",
5656
default_integrations=False,
5757
integrations=[StdlibIntegration()],
5858
)
5959
server, requests = local_http_server
60-
61-
if span_streaming:
62-
with sentry_sdk.traces.start_span(name="test"): # type: ignore[attr-defined]
63-
_request(server, [])
64-
else:
65-
with sentry_sdk.start_transaction(name="test", sampled=True):
66-
_request(server, [])
60+
with sentry_sdk.traces.start_span(name="test"): # type: ignore[attr-defined]
61+
_request(server, [])
6762

6863
headers: HTTPMessage = requests[0]
6964

@@ -78,37 +73,26 @@ def test_aws_http_connection_adds_missing_unsigned_propagation_headers(
7873
assert len(sentry_trace_headers) == 1
7974

8075

81-
@pytest.mark.parametrize("span_streaming", [False, True])
8276
def test_aws_http_connection_appends_baggage_but_preserves_sentry_trace(
83-
sentry_init, local_http_server, span_streaming
77+
sentry_init,
78+
local_http_server,
8479
):
8580
"""Append unsigned `baggage`; leave existing `sentry-trace` as-is."""
8681
sentry_init(
8782
traces_sample_rate=1.0,
88-
trace_lifecycle="stream" if span_streaming else "static",
83+
trace_lifecycle="stream",
8984
default_integrations=False,
9085
integrations=[StdlibIntegration()],
9186
)
9287
server, requests = local_http_server
93-
94-
if span_streaming:
95-
with sentry_sdk.traces.start_span(name="test"): # type: ignore[attr-defined]
96-
_request(
97-
server,
98-
[
99-
("baggage", "vendor=value"),
100-
("sentry-trace", "existing-trace"),
101-
],
102-
)
103-
else:
104-
with sentry_sdk.start_transaction(name="test", sampled=True):
105-
_request(
106-
server,
107-
[
108-
("baggage", "vendor=value"),
109-
("sentry-trace", "existing-trace"),
110-
],
111-
)
88+
with sentry_sdk.traces.start_span(name="test"): # type: ignore[attr-defined]
89+
_request(
90+
server,
91+
[
92+
("baggage", "vendor=value"),
93+
("sentry-trace", "existing-trace"),
94+
],
95+
)
11296

11397
headers: HTTPMessage = requests[0]
11498

@@ -123,14 +107,14 @@ def test_aws_http_connection_appends_baggage_but_preserves_sentry_trace(
123107
assert headers.get_all("sentry-trace") == ["existing-trace"]
124108

125109

126-
@pytest.mark.parametrize("span_streaming", [False, True])
127110
def test_aws_http_connection_preserves_signed_propagation_headers(
128-
sentry_init, local_http_server, span_streaming
111+
sentry_init,
112+
local_http_server,
129113
):
130114
"""Leave signed `sentry-trace` and `baggage` as-is."""
131115
sentry_init(
132116
traces_sample_rate=1.0,
133-
trace_lifecycle="stream" if span_streaming else "static",
117+
trace_lifecycle="stream",
134118
default_integrations=False,
135119
integrations=[StdlibIntegration()],
136120
)
@@ -143,27 +127,15 @@ def test_aws_http_connection_preserves_signed_propagation_headers(
143127
"SignedHeaders=baggage;host;sentry-trace, "
144128
"Signature=sixtyseven"
145129
)
146-
147-
if span_streaming:
148-
with sentry_sdk.traces.start_span(name="test"): # type: ignore[attr-defined]
149-
_request(
150-
server,
151-
[
152-
("baggage", "vendor=value"),
153-
("sentry-trace", "existing-trace"),
154-
("Authorization", authorization),
155-
],
156-
)
157-
else:
158-
with sentry_sdk.start_transaction(name="test", sampled=True):
159-
_request(
160-
server,
161-
[
162-
("baggage", "vendor=value"),
163-
("sentry-trace", "existing-trace"),
164-
("Authorization", authorization),
165-
],
166-
)
130+
with sentry_sdk.traces.start_span(name="test"): # type: ignore[attr-defined]
131+
_request(
132+
server,
133+
[
134+
("baggage", "vendor=value"),
135+
("sentry-trace", "existing-trace"),
136+
("Authorization", authorization),
137+
],
138+
)
167139

168140
headers: HTTPMessage = requests[0]
169141

@@ -180,14 +152,14 @@ def test_aws_http_connection_preserves_signed_propagation_headers(
180152
}
181153

182154

183-
@pytest.mark.parametrize("span_streaming", [False, True])
184155
def test_aws_http_connection_preserves_query_signed_baggage(
185-
sentry_init, local_http_server, span_streaming
156+
sentry_init,
157+
local_http_server,
186158
):
187159
"""Leave query-signed `baggage` as-is; add unsigned `sentry-trace`."""
188160
sentry_init(
189161
traces_sample_rate=1.0,
190-
trace_lifecycle="stream" if span_streaming else "static",
162+
trace_lifecycle="stream",
191163
default_integrations=False,
192164
integrations=[StdlibIntegration()],
193165
)
@@ -202,21 +174,12 @@ def test_aws_http_connection_preserves_query_signed_baggage(
202174
"&X-Amz-SignedHeaders=baggage%3Bhost"
203175
"&X-Amz-Signature=sixtyseven"
204176
)
205-
206-
if span_streaming:
207-
with sentry_sdk.traces.start_span(name="test"): # type: ignore[attr-defined]
208-
_request(
209-
server,
210-
[("baggage", "vendor=value")],
211-
path=path,
212-
)
213-
else:
214-
with sentry_sdk.start_transaction(name="test", sampled=True):
215-
_request(
216-
server,
217-
[("baggage", "vendor=value")],
218-
path=path,
219-
)
177+
with sentry_sdk.traces.start_span(name="test"): # type: ignore[attr-defined]
178+
_request(
179+
server,
180+
[("baggage", "vendor=value")],
181+
path=path,
182+
)
220183

221184
headers: HTTPMessage = requests[0]
222185
# query-signed `baggage`: leave as-is.

0 commit comments

Comments
 (0)