Skip to content

Commit 8927084

Browse files
fix(boto3): Don't raise on unexpected SQS response shapes
on_success_response() ran outside capture_internal_exceptions(), so a SendMessage or ReceiveMessage response without MessageId (e.g. from a botocore Stubber or an SQS-compatible service) raised a KeyError into the caller after a successful call and left the span open. Co-Authored-By: Ivana Kellyer <131587164+sentrivana@users.noreply.github.com>
1 parent 558f104 commit 8927084

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

‎sentry_sdk/integrations/boto3/_services/sqs.py‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,22 @@ def on_success_response(
107107
) -> None:
108108
attributes: "Attributes" = {}
109109

110-
if ctx.operation_name == "SendMessage":
111-
attributes[SPANDATA.MESSAGING_MESSAGE_ID] = response["MessageId"]
112-
elif ctx.operation_name == "ReceiveMessage":
113-
# `Messages` is left out when no message was received.
114-
messages = response.get("Messages", ())
115-
attributes[SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT] = len(messages)
116-
if len(messages) == 1:
117-
attributes[SPANDATA.MESSAGING_MESSAGE_ID] = messages[0]["MessageId"]
118-
119-
error_type = _get_batch_error_type(response)
120-
if error_type is not None:
121-
span.status = SpanStatus.ERROR
122-
attributes[SPANDATA.ERROR_TYPE] = error_type
110+
# the call already succeeded; an unexpected response shape, e.g. from a
111+
# `Stubber` or an SQS-compatible service, must only lose these attributes.
112+
with capture_internal_exceptions():
113+
if ctx.operation_name == "SendMessage" and "MessageId" in response:
114+
attributes[SPANDATA.MESSAGING_MESSAGE_ID] = response["MessageId"]
115+
elif ctx.operation_name == "ReceiveMessage":
116+
# `Messages` is left out when no message was received.
117+
messages = response.get("Messages", ())
118+
attributes[SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT] = len(messages)
119+
if len(messages) == 1 and "MessageId" in messages[0]:
120+
attributes[SPANDATA.MESSAGING_MESSAGE_ID] = messages[0]["MessageId"]
121+
122+
error_type = _get_batch_error_type(response)
123+
if error_type is not None:
124+
span.status = SpanStatus.ERROR
125+
attributes[SPANDATA.ERROR_TYPE] = error_type
123126

124127
span.set_attributes(attributes)
125128
span.end()

‎tests/integrations/boto3/test_sqs.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,15 @@ def test_request_attributes(
9696
None,
9797
id="single",
9898
),
99+
pytest.param(
100+
"send_message",
101+
{"MessageBody": "hi"},
102+
{},
103+
None,
104+
None,
105+
None,
106+
id="single-without-message-id",
107+
),
99108
pytest.param(
100109
"send_message_batch",
101110
{"Entries": _BATCH_ENTRIES},
@@ -198,6 +207,12 @@ def test_send_message(
198207
"1",
199208
id="single-message",
200209
),
210+
pytest.param(
211+
{"Messages": [{"Body": "hi"}]},
212+
1,
213+
None,
214+
id="single-message-without-message-id",
215+
),
201216
pytest.param(
202217
{
203218
"Messages": [

0 commit comments

Comments
 (0)