Skip to content

Commit acb8eae

Browse files
ref(aws-lambda): Use new scopes API (#2882)
--------- Co-authored-by: Daniel Szoke <[email protected]>
1 parent 05a713c commit acb8eae

File tree

4 files changed

+25
-33
lines changed

4 files changed

+25
-33
lines changed

sentry_sdk/integrations/aws_lambda.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
from datetime import datetime, timedelta, timezone
44
from os import environ
55

6+
import sentry_sdk
67
from sentry_sdk.api import continue_trace
78
from sentry_sdk.consts import OP
8-
from sentry_sdk.hub import Hub, _should_send_default_pii
9+
from sentry_sdk.scope import Scope, should_send_default_pii
910
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
1011
from sentry_sdk.utils import (
1112
AnnotatedValue,
@@ -38,18 +39,13 @@ def _wrap_init_error(init_error):
3839
# type: (F) -> F
3940
def sentry_init_error(*args, **kwargs):
4041
# type: (*Any, **Any) -> Any
41-
42-
hub = Hub.current
43-
integration = hub.get_integration(AwsLambdaIntegration)
42+
client = sentry_sdk.get_client()
43+
integration = client.get_integration(AwsLambdaIntegration)
4444
if integration is None:
4545
return init_error(*args, **kwargs)
4646

47-
# If an integration is there, a client has to be there.
48-
client = hub.client # type: Any
49-
5047
with capture_internal_exceptions():
51-
with hub.configure_scope() as scope:
52-
scope.clear_breadcrumbs()
48+
Scope.get_isolation_scope().clear_breadcrumbs()
5349

5450
exc_info = sys.exc_info()
5551
if exc_info and all(exc_info):
@@ -58,7 +54,7 @@ def sentry_init_error(*args, **kwargs):
5854
client_options=client.options,
5955
mechanism={"type": "aws_lambda", "handled": False},
6056
)
61-
hub.capture_event(sentry_event, hint=hint)
57+
sentry_sdk.capture_event(sentry_event, hint=hint)
6258

6359
return init_error(*args, **kwargs)
6460

@@ -93,16 +89,14 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
9389
# this is empty
9490
request_data = {}
9591

96-
hub = Hub.current
97-
integration = hub.get_integration(AwsLambdaIntegration)
92+
client = sentry_sdk.get_client()
93+
integration = client.get_integration(AwsLambdaIntegration)
9894
if integration is None:
9995
return handler(aws_event, aws_context, *args, **kwargs)
10096

101-
# If an integration is there, a client has to be there.
102-
client = hub.client # type: Any
10397
configured_time = aws_context.get_remaining_time_in_millis()
10498

105-
with hub.push_scope() as scope:
99+
with sentry_sdk.isolation_scope() as scope:
106100
timeout_thread = None
107101
with capture_internal_exceptions():
108102
scope.clear_breadcrumbs()
@@ -148,7 +142,7 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
148142
name=aws_context.function_name,
149143
source=TRANSACTION_SOURCE_COMPONENT,
150144
)
151-
with hub.start_transaction(
145+
with sentry_sdk.start_transaction(
152146
transaction,
153147
custom_sampling_context={
154148
"aws_event": aws_event,
@@ -164,7 +158,7 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
164158
client_options=client.options,
165159
mechanism={"type": "aws_lambda", "handled": False},
166160
)
167-
hub.capture_event(sentry_event, hint=hint)
161+
sentry_sdk.capture_event(sentry_event, hint=hint)
168162
reraise(*exc_info)
169163
finally:
170164
if timeout_thread:
@@ -176,12 +170,12 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
176170
def _drain_queue():
177171
# type: () -> None
178172
with capture_internal_exceptions():
179-
hub = Hub.current
180-
integration = hub.get_integration(AwsLambdaIntegration)
173+
client = sentry_sdk.get_client()
174+
integration = client.get_integration(AwsLambdaIntegration)
181175
if integration is not None:
182176
# Flush out the event queue before AWS kills the
183177
# process.
184-
hub.flush()
178+
client.flush()
185179

186180

187181
class AwsLambdaIntegration(Integration):
@@ -358,7 +352,7 @@ def event_processor(sentry_event, hint, start_time=start_time):
358352
if "headers" in aws_event:
359353
request["headers"] = _filter_headers(aws_event["headers"])
360354

361-
if _should_send_default_pii():
355+
if should_send_default_pii():
362356
user_info = sentry_event.setdefault("user", {})
363357

364358
identity = aws_event.get("identity")

sentry_sdk/integrations/boto3.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from functools import partial
22

3-
from sentry_sdk import Hub
3+
import sentry_sdk
44
from sentry_sdk.consts import OP, SPANDATA
55
from sentry_sdk.integrations import Integration, DidNotEnable
66
from sentry_sdk.tracing import Span
@@ -59,13 +59,11 @@ def sentry_patched_init(self, *args, **kwargs):
5959

6060
def _sentry_request_created(service_id, request, operation_name, **kwargs):
6161
# type: (str, AWSRequest, str, **Any) -> None
62-
hub = Hub.current
63-
if hub.get_integration(Boto3Integration) is None:
62+
if sentry_sdk.get_client().get_integration(Boto3Integration) is None:
6463
return
6564

6665
description = "aws.%s.%s" % (service_id, operation_name)
67-
span = hub.start_span(
68-
hub=hub,
66+
span = sentry_sdk.start_span(
6967
op=OP.HTTP_CLIENT,
7068
description=description,
7169
)

tests/integrations/aws_lambda/test_aws.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,9 @@ def test_serverless_no_code_instrumentation(run_lambda_function):
672672
import sentry_sdk
673673
674674
def test_handler(event, context):
675-
current_client = sentry_sdk.Hub.current.client
675+
current_client = sentry_sdk.get_client()
676676
677-
assert current_client is not None
677+
assert current_client.is_active()
678678
679679
assert len(current_client.options['integrations']) == 1
680680
assert isinstance(current_client.options['integrations'][0],

tests/integrations/boto3/test_s3.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import boto3
44
import pytest
55

6-
from sentry_sdk import Hub
6+
import sentry_sdk
77
from sentry_sdk.integrations.boto3 import Boto3Integration
88
from tests.integrations.boto3 import read_fixture
99
from tests.integrations.boto3.aws_mock import MockResponse
@@ -20,7 +20,7 @@ def test_basic(sentry_init, capture_events):
2020
events = capture_events()
2121

2222
s3 = session.resource("s3")
23-
with Hub.current.start_transaction() as transaction, MockResponse(
23+
with sentry_sdk.start_transaction() as transaction, MockResponse(
2424
s3.meta.client, 200, {}, read_fixture("s3_list.xml")
2525
):
2626
bucket = s3.Bucket("bucket")
@@ -43,7 +43,7 @@ def test_streaming(sentry_init, capture_events):
4343
events = capture_events()
4444

4545
s3 = session.resource("s3")
46-
with Hub.current.start_transaction() as transaction, MockResponse(
46+
with sentry_sdk.start_transaction() as transaction, MockResponse(
4747
s3.meta.client, 200, {}, b"hello"
4848
):
4949
obj = s3.Bucket("bucket").Object("foo.pdf")
@@ -79,7 +79,7 @@ def test_streaming_close(sentry_init, capture_events):
7979
events = capture_events()
8080

8181
s3 = session.resource("s3")
82-
with Hub.current.start_transaction() as transaction, MockResponse(
82+
with sentry_sdk.start_transaction() as transaction, MockResponse(
8383
s3.meta.client, 200, {}, b"hello"
8484
):
8585
obj = s3.Bucket("bucket").Object("foo.pdf")
@@ -108,7 +108,7 @@ def test_omit_url_data_if_parsing_fails(sentry_init, capture_events):
108108
"sentry_sdk.integrations.boto3.parse_url",
109109
side_effect=ValueError,
110110
):
111-
with Hub.current.start_transaction() as transaction, MockResponse(
111+
with sentry_sdk.start_transaction() as transaction, MockResponse(
112112
s3.meta.client, 200, {}, read_fixture("s3_list.xml")
113113
):
114114
bucket = s3.Bucket("bucket")

0 commit comments

Comments
 (0)