Skip to content

Commit 939a3f8

Browse files
committed
use context managers
1 parent 52be6a4 commit 939a3f8

2 files changed

Lines changed: 14 additions & 17 deletions

File tree

‎sentry_sdk/integrations/boto3/_client.py‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
from sentry_sdk.integrations.boto3._context import AwsCallContext
77
from sentry_sdk.integrations.boto3._instrumentation import (
88
_finish_active_http_child_span,
9-
_finish_client_span,
9+
_instrument_streaming_body,
1010
_sentry_before_sign,
1111
_sentry_request_created,
1212
_start_client_span,
1313
)
14-
from sentry_sdk.utils import capture_internal_exceptions
14+
from sentry_sdk.utils import capture_internal_exceptions, nullcontext
1515

1616
if TYPE_CHECKING:
1717
from typing import Any, Optional, Union
@@ -58,22 +58,21 @@ def sentry_patched_make_api_call(
5858

5959
with capture_internal_exceptions():
6060
span = _start_client_span(ctx)
61-
if span is not None:
62-
span.__enter__()
6361

64-
try:
65-
parsed = orig_make_api_call(self, operation_name, api_params)
66-
except BaseException as exc:
67-
if span is not None:
68-
with capture_internal_exceptions():
69-
_finish_active_http_child_span(span)
70-
span.__exit__(type(exc), exc, exc.__traceback__)
71-
raise
62+
span_ctx = span or nullcontext()
63+
with span_ctx as span:
64+
try:
65+
parsed = orig_make_api_call(self, operation_name, api_params)
66+
finally:
67+
# stdlib HTTP span must leave stack before boto3 parent ctx
68+
# manager restores the previous span.
69+
if span is not None:
70+
with capture_internal_exceptions():
71+
_finish_active_http_child_span(span)
7272

7373
if span is not None:
7474
with capture_internal_exceptions():
75-
_finish_active_http_child_span(span)
76-
_finish_client_span(span, parsed)
75+
_instrument_streaming_body(span, parsed)
7776
return parsed
7877

7978
BaseClient.__init__ = sentry_patched_init # type: ignore

‎sentry_sdk/integrations/boto3/_instrumentation.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,10 @@ def _finish_active_http_child_span(
165165
http_span.end()
166166

167167

168-
def _finish_client_span(
168+
def _instrument_streaming_body(
169169
span: "Union[Span, StreamedSpan]",
170170
parsed: "Dict[str, Any]",
171171
) -> None:
172-
span.__exit__(None, None, None)
173-
174172
if isinstance(span, NoOpStreamedSpan):
175173
return
176174

0 commit comments

Comments
 (0)