diff --git a/sentry_sdk/integrations/aws_lambda.py b/sentry_sdk/integrations/aws_lambda.py index 4990fd6e6a..85d1a6c28c 100644 --- a/sentry_sdk/integrations/aws_lambda.py +++ b/sentry_sdk/integrations/aws_lambda.py @@ -138,6 +138,8 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs): timeout_thread = TimeoutThread( waiting_time, configured_time / MILLIS_TO_SECONDS, + isolation_scope=scope, + current_scope=sentry_sdk.get_current_scope(), ) # Starting the thread to raise timeout warning exception diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index cd825b29e2..3496178228 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -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}, + ) + 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 diff --git a/tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/TimeoutErrorScopeModified/.gitignore b/tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/TimeoutErrorScopeModified/.gitignore new file mode 100644 index 0000000000..1c56884372 --- /dev/null +++ b/tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/TimeoutErrorScopeModified/.gitignore @@ -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 diff --git a/tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/TimeoutErrorScopeModified/index.py b/tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/TimeoutErrorScopeModified/index.py new file mode 100644 index 0000000000..109245b90d --- /dev/null +++ b/tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/TimeoutErrorScopeModified/index.py @@ -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, + } diff --git a/tests/integrations/aws_lambda/test_aws_lambda.py b/tests/integrations/aws_lambda/test_aws_lambda.py index 85da7e0b14..664220464c 100644 --- a/tests/integrations/aws_lambda/test_aws_lambda.py +++ b/tests/integrations/aws_lambda/test_aws_lambda.py @@ -223,6 +223,31 @@ def test_timeout_error(lambda_client, test_environment): assert exception["mechanism"]["type"] == "threading" +def test_timeout_error_scope_modified(lambda_client, test_environment): + lambda_client.invoke( + FunctionName="TimeoutErrorScopeModified", + Payload=json.dumps({}), + ) + envelopes = test_environment["server"].envelopes + + (error_event,) = envelopes + + assert error_event["level"] == "error" + assert ( + error_event["extra"]["lambda"]["function_name"] == "TimeoutErrorScopeModified" + ) + + (exception,) = error_event["exception"]["values"] + assert not exception["mechanism"]["handled"] + assert exception["type"] == "ServerlessTimeoutWarning" + assert exception["value"].startswith( + "WARNING : Function is expected to get timed out. Configured timeout duration =" + ) + assert exception["mechanism"]["type"] == "threading" + + assert error_event["tags"]["custom_tag"] == "custom_value" + + @pytest.mark.parametrize( "aws_event, has_request_data, batch_size", [