Skip to content

Commit 73b5bf7

Browse files
Merge remote-tracking branch 'origin/major/3.0' into pablo/boto3-add-sqs/sns-propagation
Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com> # Conflicts: # tests/integrations/boto3/test_trace_propagation.py
2 parents 0966c93 + 49473c4 commit 73b5bf7

3 files changed

Lines changed: 75 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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,65 @@ def test_message_attributes_round_trip(sentry_init, capture_items, service):
403403
assert link["trace_id"] == publish_span["trace_id"]
404404
assert link["span_id"] == publish_span["span_id"]
405405
assert link["sampled"] is True
406+
407+
408+
@pytest.mark.parametrize(
409+
"service,method,params,response",
410+
[
411+
pytest.param(
412+
"sqs",
413+
"send_message",
414+
{
415+
"QueueUrl": "https://sqs.eu-north-1.amazonaws.com/123456789012/orders",
416+
"MessageBody": "hi",
417+
},
418+
{"MessageId": "1"},
419+
id="sqs-send",
420+
),
421+
pytest.param(
422+
"sqs",
423+
"receive_message",
424+
{"QueueUrl": "https://sqs.eu-north-1.amazonaws.com/123456789012/orders"},
425+
{},
426+
id="sqs-receive",
427+
),
428+
pytest.param(
429+
"sns",
430+
"publish",
431+
{"TopicArn": "arn:aws:sns:eu-north-1:123456789012:alerts", "Message": "hi"},
432+
{"MessageId": "1"},
433+
id="sns-publish",
434+
),
435+
pytest.param(
436+
"lambda",
437+
"invoke",
438+
{"FunctionName": "billing"},
439+
{"StatusCode": 200},
440+
id="lambda-invoke",
441+
),
442+
],
443+
)
444+
def test_propagate_traces_disabled(
445+
sentry_init, capture_items, service, method, params, response
446+
):
447+
"""`propagate_traces=False` sends the caller's params unchanged."""
448+
sentry_init(
449+
traces_sample_rate=1.0,
450+
integrations=[Boto3Integration(propagate_traces=False)],
451+
)
452+
client = boto3.client( # type: ignore[attr-defined]
453+
service,
454+
region_name="eu-north-1",
455+
aws_access_key_id="-",
456+
aws_secret_access_key="-",
457+
)
458+
459+
with Stubber(client) as stubber:
460+
# the stubber fails the call when the sent params differ from these.
461+
stubber.add_response(method, response, copy.deepcopy(params))
462+
spans = capture_spans_by_op(
463+
lambda: getattr(client, method)(**params), capture_items
464+
)
465+
466+
# the call is still instrumented.
467+
assert sum(map(len, spans.values())) == 1

0 commit comments

Comments
 (0)