Skip to content

Commit 48eae1c

Browse files
ref(boto3): Guard SQS ARN and batch-count reads separately
Botocore validates params after our hook, so a bad Entries or ARN value now only loses that attribute, not every SQS request attribute. Move the batch error.type code to _utils so SNS can share it, and link the SQS docs for BatchResultErrorEntry. Co-Authored-By: Johannes Daxböck <114897+dingsdax@users.noreply.github.com>
1 parent f6c8099 commit 48eae1c

2 files changed

Lines changed: 34 additions & 14 deletions

File tree

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
from sentry_sdk.consts import OP, SPANDATA
66
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
7-
from sentry_sdk.integrations.boto3._utils import _get_aws_arn_attributes
7+
from sentry_sdk.integrations.boto3._utils import (
8+
_get_aws_arn_attributes,
9+
_get_batch_error_type,
10+
)
811
from sentry_sdk.utils import capture_internal_exceptions
912

1013
if TYPE_CHECKING:
@@ -45,9 +48,14 @@ def get_span_name(
4548
)
4649

4750
def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
48-
attributes = _get_aws_arn_attributes(
49-
(ctx.params.get("DestinationArn"), ctx.params.get("SourceArn")), ("sqs",)
50-
)
51+
attributes: "Attributes" = {}
52+
with capture_internal_exceptions():
53+
attributes.update(
54+
_get_aws_arn_attributes(
55+
(ctx.params.get("DestinationArn"), ctx.params.get("SourceArn")),
56+
("sqs",),
57+
)
58+
)
5159

5260
queue_url = ctx.params.get("QueueUrl")
5361
if queue_url is not None:
@@ -86,9 +94,10 @@ def get_request_attributes(self, ctx: "AwsCallContext") -> "Attributes":
8694
if operation_type == "send":
8795
attributes[SPANDATA.SENTRY_KIND] = "producer"
8896
if "Entries" in ctx.params:
89-
attributes[SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT] = len(
90-
ctx.params["Entries"]
91-
)
97+
with capture_internal_exceptions():
98+
attributes[SPANDATA.MESSAGING_BATCH_MESSAGE_COUNT] = len(
99+
ctx.params["Entries"]
100+
)
92101
return attributes
93102

94103
def get_response_attributes(
@@ -109,11 +118,7 @@ def get_response_attributes(
109118
if len(messages) == 1:
110119
attributes[SPANDATA.MESSAGING_MESSAGE_ID] = messages[0]["MessageId"]
111120

112-
# failed entries have a `200 OK` response; use their code when they share one.
113-
# https://docs.aws.amazon.com/sns/latest/api/API_BatchResultErrorEntry.html
114-
error_codes = {entry["Code"] for entry in response.get("Failed", ())}
115-
if len(error_codes) == 1:
116-
attributes[SPANDATA.ERROR_TYPE] = error_codes.pop()
117-
elif error_codes:
118-
attributes[SPANDATA.ERROR_TYPE] = "aws_batch_partial_failure"
121+
error_type = _get_batch_error_type(response)
122+
if error_type is not None:
123+
attributes[SPANDATA.ERROR_TYPE] = error_type
119124
return attributes

‎sentry_sdk/integrations/boto3/_utils.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,18 @@ def _get_aws_arn_attributes(
3838
return attributes
3939

4040
return {}
41+
42+
43+
def _get_batch_error_type(response: "Any") -> "Optional[str]":
44+
"""
45+
`error.type` for a batch call whose response has `Failed` entries.
46+
Failed entries come back in a `200 OK` response; use their code when they share one.
47+
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_BatchResultErrorEntry.html
48+
https://docs.aws.amazon.com/sns/latest/api/API_BatchResultErrorEntry.html
49+
"""
50+
error_codes = {entry["Code"] for entry in response.get("Failed", ())}
51+
if len(error_codes) == 1:
52+
return error_codes.pop()
53+
if error_codes:
54+
return "aws_batch_partial_failure"
55+
return None

0 commit comments

Comments
 (0)