Skip to content

Commit 49473c4

Browse files
feat(boto3): Add propagate_traces option (#8244)
### Description Adds `Boto3Integration(propagate_traces=True)`. Setting it to `False` stops the integration from changing the request params of AWS calls: - Lambda `Invoke` no longer gets trace data in `ClientContext`. - Once #8145 lands: SQS `SendMessage` / `SendMessageBatch` and SNS `Publish` / `PublishBatch` no longer get `sentry-trace` / `baggage` message attributes, and SQS `ReceiveMessage` no longer appends our names to `MessageAttributeNames`. The option gates the shared `get_request_params` hook, so it covers every extension that uses it without per-service changes. Spans are still created as before. This is an escape hatch for setups where the added data breaks calls, e.g. a queue or topic whose `MaximumMessageSize` is set below the 256 KiB the #8145 size check assumes, or consumers that reject unknown attributes. The name matches `CeleryIntegration(propagate_traces=...)`. HTTP `sentry-trace` / `baggage` headers on AWS requests are not affected. They're also added by the stdlib integration and are already controlled by `trace_propagation_targets`. Independent of #8145. The SQS/SNS test cases pass trivially until #8145 is merged, and guard the option after that. Whichever PR merges second needs a small conflict fix at the end of `test_trace_propagation.py`, since both append tests there. <!-- junior-request-attribution:start --> via @sentrivana. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:slack%3AC0BTREZTS2Z%3A1794401901.814229 --> -- [View Session](https://junior-prod.sentry.dev/conversations/slack%3AC0BTREZTS2Z%3A1794401901.814229) [[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0BTREZTS2Z%3A1794401901.814229/?project=4510944073809921) <!-- junior-session-footer:end --> --------- Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com> Co-authored-by: Ivana Kellyer <ivana.kellyer@sentry.io>
1 parent 36870b8 commit 49473c4

3 files changed

Lines changed: 79 additions & 4 deletions

File tree

‎sentry_sdk/integrations/boto3/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class Boto3Integration(Integration):
1414
identifier = IDENTIFIER
1515
origin = ORIGIN
1616

17+
def __init__(self, propagate_traces: bool = True) -> None:
18+
# add trace data to request params, e.g. SQS/SNS message attributes or
19+
# Lambda `ClientContext`, so the receiving side can continue the trace.
20+
self.propagate_traces = propagate_traces
21+
1722
@staticmethod
1823
def setup_once() -> None:
1924
version = parse_version(BOTOCORE_VERSION)

‎sentry_sdk/integrations/boto3/_client.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from contextlib import contextmanager
2-
from typing import TYPE_CHECKING
2+
from typing import TYPE_CHECKING, cast
33

44
import sentry_sdk
55
from sentry_sdk.integrations import DidNotEnable
@@ -34,6 +34,7 @@
3434
from typing import Any, Dict, Iterator, Optional
3535

3636
from sentry_sdk._types import Attributes
37+
from sentry_sdk.integrations.boto3 import Boto3Integration
3738
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
3839

3940
try:
@@ -93,8 +94,11 @@ def sentry_patched_make_api_call(
9394
https://github.com/boto/botocore/blob/358f8eec8c76201bb1a7a35644abcbc9036de7ed/botocore/client.py
9495
https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/#rpc-client-span
9596
"""
96-
client = sentry_sdk.get_client()
97-
if client.get_integration(IDENTIFIER) is None:
97+
integration = cast(
98+
"Optional[Boto3Integration]",
99+
sentry_sdk.get_client().get_integration(IDENTIFIER),
100+
)
101+
if integration is None:
98102
return orig_make_api_call(self, operation_name, api_params)
99103

100104
span: "Optional[Span]" = None
@@ -125,7 +129,7 @@ def sentry_patched_make_api_call(
125129
service_ext = _StepFunctionsExtension()
126130

127131
span = _start_client_span(ctx, service_ext)
128-
if service_ext is not None:
132+
if service_ext is not None and integration.propagate_traces:
129133
# override the request params for e.g. trace propagation.
130134
api_params = service_ext.get_request_params(ctx, span) or api_params
131135

‎tests/integrations/boto3/test_trace_propagation.py‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import copy
12
from http.server import BaseHTTPRequestHandler, HTTPServer
23
from threading import Thread
34
from urllib.parse import parse_qs, urlparse
45

56
import boto3
7+
import pytest
68
from botocore.auth import SIGNED_HEADERS_BLACKLIST
79
from botocore.config import Config
10+
from botocore.stub import Stubber
811

912
import sentry_sdk
1013
from sentry_sdk.integrations.boto3 import Boto3Integration
@@ -13,6 +16,7 @@
1316
_get_aws_sigv4_signed_headers_from_authorization_header,
1417
_get_aws_sigv4_signed_headers_from_url_query_string,
1518
)
19+
from tests.integrations.boto3.helpers import capture_spans_by_op
1620

1721
# botocore 1.43.115+ never signs `baggage`.
1822
BOTOCORE_SIGNS_BAGGAGE = "baggage" not in SIGNED_HEADERS_BLACKLIST
@@ -207,3 +211,65 @@ def test_presigned_urls_do_not_require_sentry_headers(sentry_init):
207211
# presigned URL query must not contain `sentry-trace` or `baggage`.
208212
assert "sentry-trace" not in url
209213
assert "baggage" not in url
214+
215+
216+
@pytest.mark.parametrize(
217+
"service,method,params,response",
218+
[
219+
pytest.param(
220+
"sqs",
221+
"send_message",
222+
{
223+
"QueueUrl": "https://sqs.eu-north-1.amazonaws.com/123456789012/orders",
224+
"MessageBody": "hi",
225+
},
226+
{"MessageId": "1"},
227+
id="sqs-send",
228+
),
229+
pytest.param(
230+
"sqs",
231+
"receive_message",
232+
{"QueueUrl": "https://sqs.eu-north-1.amazonaws.com/123456789012/orders"},
233+
{},
234+
id="sqs-receive",
235+
),
236+
pytest.param(
237+
"sns",
238+
"publish",
239+
{"TopicArn": "arn:aws:sns:eu-north-1:123456789012:alerts", "Message": "hi"},
240+
{"MessageId": "1"},
241+
id="sns-publish",
242+
),
243+
pytest.param(
244+
"lambda",
245+
"invoke",
246+
{"FunctionName": "billing"},
247+
{"StatusCode": 200},
248+
id="lambda-invoke",
249+
),
250+
],
251+
)
252+
def test_propagate_traces_disabled(
253+
sentry_init, capture_items, service, method, params, response
254+
):
255+
"""`propagate_traces=False` sends the caller's params unchanged."""
256+
sentry_init(
257+
traces_sample_rate=1.0,
258+
integrations=[Boto3Integration(propagate_traces=False)],
259+
)
260+
client = boto3.client( # type: ignore[attr-defined]
261+
service,
262+
region_name="eu-north-1",
263+
aws_access_key_id="-",
264+
aws_secret_access_key="-",
265+
)
266+
267+
with Stubber(client) as stubber:
268+
# the stubber fails the call when the sent params differ from these.
269+
stubber.add_response(method, response, copy.deepcopy(params))
270+
spans = capture_spans_by_op(
271+
lambda: getattr(client, method)(**params), capture_items
272+
)
273+
274+
# the call is still instrumented.
275+
assert sum(map(len, spans.values())) == 1

0 commit comments

Comments
 (0)