-
Notifications
You must be signed in to change notification settings - Fork 561
fix(aws): Inject scopes in TimeoutThread exception with AWS lambda #4914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
58535a3
2f0e1f4
826f49b
b4c993e
2ff1ba2
a0b8ffc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1484,17 +1484,37 @@ class TimeoutThread(threading.Thread): | |
waiting_time and raises a custom ServerlessTimeout exception. | ||
""" | ||
|
||
def __init__(self, waiting_time, configured_timeout): | ||
# type: (float, int) -> None | ||
def __init__( | ||
self, waiting_time, configured_timeout, isolation_scope=None, current_scope=None | ||
): | ||
# type: (float, int, Optional[sentry_sdk.Scope], Optional[sentry_sdk.Scope]) -> None | ||
threading.Thread.__init__(self) | ||
self.waiting_time = waiting_time | ||
self.configured_timeout = configured_timeout | ||
|
||
self.isolation_scope = isolation_scope | ||
self.current_scope = current_scope | ||
|
||
self._stop_event = threading.Event() | ||
|
||
def stop(self): | ||
# type: () -> None | ||
self._stop_event.set() | ||
|
||
def _capture_exception(self): | ||
# type: () -> ExcInfo | ||
exc_info = sys.exc_info() | ||
|
||
client = sentry_sdk.get_client() | ||
event, hint = event_from_exception( | ||
exc_info, | ||
client_options=client.options, | ||
mechanism={"type": "threading", "handled": False}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: AWS Lambda Timeout Mechanism InconsistencyThe |
||
) | ||
sentry_sdk.capture_event(event, hint=hint) | ||
|
||
return exc_info | ||
|
||
def run(self): | ||
# type: () -> None | ||
|
||
|
@@ -1510,6 +1530,18 @@ def run(self): | |
integer_configured_timeout = integer_configured_timeout + 1 | ||
|
||
# Raising Exception after timeout duration is reached | ||
if self.isolation_scope is not None and self.current_scope is not None: | ||
with sentry_sdk.scope.use_isolation_scope(self.isolation_scope): | ||
with sentry_sdk.scope.use_scope(self.current_scope): | ||
try: | ||
raise ServerlessTimeoutWarning( | ||
"WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds.".format( | ||
integer_configured_timeout | ||
) | ||
) | ||
except Exception: | ||
reraise(*self._capture_exception()) | ||
|
||
raise ServerlessTimeoutWarning( | ||
"WARNING : Function is expected to get timed out. Configured timeout duration = {} seconds.".format( | ||
integer_configured_timeout | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Need to add some ignore rules in this directory, because the unit tests will add the Sentry SDK and its dependencies | ||
# into this directory to create a Lambda function package that contains everything needed to instrument a Lambda function using Sentry. | ||
|
||
# Ignore everything | ||
* | ||
|
||
# But not index.py | ||
!index.py | ||
|
||
# And not .gitignore itself | ||
!.gitignore |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import os | ||
import time | ||
|
||
import sentry_sdk | ||
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration | ||
|
||
sentry_sdk.init( | ||
dsn=os.environ.get("SENTRY_DSN"), | ||
traces_sample_rate=1.0, | ||
integrations=[AwsLambdaIntegration(timeout_warning=True)], | ||
) | ||
|
||
|
||
def handler(event, context): | ||
sentry_sdk.set_tag("custom_tag", "custom_value") | ||
time.sleep(15) | ||
return { | ||
"event": event, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the mechanism be changed?
Keeping
type=threading
would keep it consistent with previous behaviour.Changing this could help us distinguish AWS lambda timeout exceptions with exceptions caught by the threading integration that are unrelated to AWS lambda timeouts.