Skip to content

Commit 499dbcc

Browse files
committed
add tests
1 parent 0c71f8e commit 499dbcc

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

‎tests/integrations/boto3/test_client.py‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
_get_error_attributes,
1717
_get_response_attributes,
1818
)
19+
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
1920
from sentry_sdk.integrations.stdlib import StdlibIntegration
2021
from tests.integrations.boto3.aws_mock import Body
2122

@@ -301,6 +302,115 @@ def _span_attributes(span, span_streaming):
301302
return span["attributes"] if span_streaming else span["data"]
302303

303304

305+
@pytest.mark.parametrize("span_streaming", [True, False])
306+
def test_service_extension_customizes_client_span(
307+
capture_items,
308+
client_factory,
309+
monkeypatch,
310+
span_streaming,
311+
):
312+
class TestServiceExtension(_ServiceExtension):
313+
def get_span_config(self, ctx):
314+
return ("aws.test", None)
315+
316+
def get_request_attributes(self, ctx):
317+
return {
318+
"aws.test.request": ctx.params["Key"],
319+
SPANDATA.RPC_METHOD: "must-not-override",
320+
}
321+
322+
def get_response_attributes(self, ctx, response):
323+
return {
324+
"aws.test.response": response["ResponseMetadata"]["RequestId"],
325+
SPANDATA.HTTP_STATUS_CODE: 418,
326+
}
327+
328+
extension = TestServiceExtension()
329+
monkeypatch.setattr(
330+
"sentry_sdk.integrations.boto3._client._resolve_service",
331+
lambda service_name: extension,
332+
)
333+
client = client_factory()
334+
api_params = {"Bucket": "bucket", "Key": "foo"}
335+
336+
with Stubber(client) as stubber:
337+
stubber.add_response(
338+
"head_object",
339+
{
340+
"ResponseMetadata": {
341+
"HTTPStatusCode": 200,
342+
"RequestId": "request-id",
343+
}
344+
},
345+
api_params,
346+
)
347+
spans_by_op = _capture_boto3_spans_by_op(
348+
lambda: client.head_object(**api_params),
349+
capture_items,
350+
span_streaming,
351+
)
352+
353+
spans = spans_by_op.get("aws.test", [])
354+
assert len(spans) == 1
355+
attributes = _span_attributes(spans[0], span_streaming)
356+
assert attributes["aws.test.request"] == "foo"
357+
assert attributes["aws.test.response"] == "request-id"
358+
assert attributes[SPANDATA.RPC_METHOD] == "HeadObject"
359+
assert attributes[SPANDATA.HTTP_STATUS_CODE] == 200
360+
361+
362+
@pytest.mark.parametrize("span_streaming", [True, False])
363+
def test_service_extension_enriches_client_error(
364+
capture_items,
365+
client_factory,
366+
monkeypatch,
367+
span_streaming,
368+
):
369+
class TestServiceExtension(_ServiceExtension):
370+
def get_response_attributes(self, ctx, response):
371+
return {
372+
"aws.test.error": response["Error"]["Code"],
373+
SPANDATA.ERROR_TYPE: "must-not-override",
374+
SPANDATA.HTTP_STATUS_CODE: 418,
375+
}
376+
377+
monkeypatch.setattr(
378+
"sentry_sdk.integrations.boto3._client._resolve_service",
379+
lambda service_name: TestServiceExtension(),
380+
)
381+
client = client_factory()
382+
error = ClientError(
383+
{
384+
"Error": {"Code": "AccessDeniedException"},
385+
"ResponseMetadata": {"HTTPStatusCode": 403},
386+
},
387+
"HeadObject",
388+
)
389+
390+
def raise_client_error(**kwargs):
391+
raise error
392+
393+
client.meta.events.register("before-parameter-build", raise_client_error)
394+
395+
def invoke_failing_client_method():
396+
with pytest.raises(ClientError) as exc_info:
397+
client.head_object(Bucket="bucket", Key="foo")
398+
assert exc_info.value is error
399+
400+
spans_by_op = _capture_boto3_spans_by_op(
401+
invoke_failing_client_method,
402+
capture_items,
403+
span_streaming,
404+
)
405+
spans = spans_by_op.get(OP.HTTP_CLIENT, [])
406+
407+
_assert_one_failed_span(spans, span_streaming)
408+
attributes = _span_attributes(spans[0], span_streaming)
409+
assert attributes["aws.test.error"] == "AccessDeniedException"
410+
assert attributes[SPANDATA.ERROR_TYPE] == "AccessDeniedException"
411+
assert attributes[SPANDATA.HTTP_STATUS_CODE] == 403
412+
413+
304414
@pytest.mark.parametrize(
305415
("response", "expected"),
306416
[

0 commit comments

Comments
 (0)