Skip to content

Commit 787cd71

Browse files
committed
add tests
1 parent 142431c commit 787cd71

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
@@ -17,6 +17,7 @@
1717
_get_response_attributes,
1818
_instrument_streaming_body,
1919
)
20+
from sentry_sdk.integrations.boto3._services.base import _ServiceExtension
2021
from sentry_sdk.integrations.boto3.consts import ORIGIN
2122
from sentry_sdk.integrations.stdlib import StdlibIntegration
2223
from sentry_sdk.traces import StreamedSpan
@@ -390,6 +391,115 @@ def _span_attributes(span, span_streaming):
390391
return span["attributes"] if span_streaming else span["data"]
391392

392393

394+
@pytest.mark.parametrize("span_streaming", [True, False])
395+
def test_service_extension_customizes_client_span(
396+
capture_items,
397+
client_factory,
398+
monkeypatch,
399+
span_streaming,
400+
):
401+
class TestServiceExtension(_ServiceExtension):
402+
def get_span_config(self, ctx):
403+
return ("aws.test", None)
404+
405+
def get_request_attributes(self, ctx):
406+
return {
407+
"aws.test.request": ctx.params["Key"],
408+
SPANDATA.RPC_METHOD: "must-not-override",
409+
}
410+
411+
def get_response_attributes(self, ctx, response):
412+
return {
413+
"aws.test.response": response["ResponseMetadata"]["RequestId"],
414+
SPANDATA.HTTP_STATUS_CODE: 418,
415+
}
416+
417+
extension = TestServiceExtension()
418+
monkeypatch.setattr(
419+
"sentry_sdk.integrations.boto3._client._resolve_service",
420+
lambda service_name: extension,
421+
)
422+
client = client_factory()
423+
api_params = {"Bucket": "bucket", "Key": "foo"}
424+
425+
with Stubber(client) as stubber:
426+
stubber.add_response(
427+
"head_object",
428+
{
429+
"ResponseMetadata": {
430+
"HTTPStatusCode": 200,
431+
"RequestId": "request-id",
432+
}
433+
},
434+
api_params,
435+
)
436+
spans_by_op = _capture_boto3_spans_by_op(
437+
lambda: client.head_object(**api_params),
438+
capture_items,
439+
span_streaming,
440+
)
441+
442+
spans = spans_by_op.get("aws.test", [])
443+
assert len(spans) == 1
444+
attributes = _span_attributes(spans[0], span_streaming)
445+
assert attributes["aws.test.request"] == "foo"
446+
assert attributes["aws.test.response"] == "request-id"
447+
assert attributes[SPANDATA.RPC_METHOD] == "HeadObject"
448+
assert attributes[SPANDATA.HTTP_STATUS_CODE] == 200
449+
450+
451+
@pytest.mark.parametrize("span_streaming", [True, False])
452+
def test_service_extension_enriches_client_error(
453+
capture_items,
454+
client_factory,
455+
monkeypatch,
456+
span_streaming,
457+
):
458+
class TestServiceExtension(_ServiceExtension):
459+
def get_response_attributes(self, ctx, response):
460+
return {
461+
"aws.test.error": response["Error"]["Code"],
462+
SPANDATA.ERROR_TYPE: "must-not-override",
463+
SPANDATA.HTTP_STATUS_CODE: 418,
464+
}
465+
466+
monkeypatch.setattr(
467+
"sentry_sdk.integrations.boto3._client._resolve_service",
468+
lambda service_name: TestServiceExtension(),
469+
)
470+
client = client_factory()
471+
error = ClientError(
472+
{
473+
"Error": {"Code": "AccessDeniedException"},
474+
"ResponseMetadata": {"HTTPStatusCode": 403},
475+
},
476+
"HeadObject",
477+
)
478+
479+
def raise_client_error(**kwargs):
480+
raise error
481+
482+
client.meta.events.register("before-parameter-build", raise_client_error)
483+
484+
def invoke_failing_client_method():
485+
with pytest.raises(ClientError) as exc_info:
486+
client.head_object(Bucket="bucket", Key="foo")
487+
assert exc_info.value is error
488+
489+
spans_by_op = _capture_boto3_spans_by_op(
490+
invoke_failing_client_method,
491+
capture_items,
492+
span_streaming,
493+
)
494+
spans = spans_by_op.get(OP.HTTP_CLIENT, [])
495+
496+
_assert_one_failed_span(spans, span_streaming)
497+
attributes = _span_attributes(spans[0], span_streaming)
498+
assert attributes["aws.test.error"] == "AccessDeniedException"
499+
assert attributes[SPANDATA.ERROR_TYPE] == "AccessDeniedException"
500+
assert attributes[SPANDATA.HTTP_STATUS_CODE] == 403
501+
502+
393503
@pytest.mark.parametrize(
394504
("response", "expected"),
395505
[

0 commit comments

Comments
 (0)