diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md index 88a51e8608..558188ae1b 100644 --- a/MIGRATION_GUIDE.md +++ b/MIGRATION_GUIDE.md @@ -93,6 +93,18 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh Note that `rq.job.args`, `rq.job.kwargs`, and `rq.job.func` are serialized and not the actual objects on the job. +- If you're using the AWS Lambda integration, the `sampling_context` argument of `traces_sampler` doesn't contain the `aws_event` and `aws_context` objects anymore. Instead, the following, if available, is accessible: + + | AWS property | Sampling context key(s) | + | ------------------------------------------- | ----------------------- | + | `aws_event["httpMethod"]` | `http.request.method` | + | `aws_event["queryStringParameters"]` | `url.query` | + | `aws_event["path"]` | `url.path` | + | full URL | `url.full` | + | `aws_event["headers"]["X-Forwarded-Proto"]` | `network.protocol.name` | + | `aws_event["headers"]["Host"]` | `server.address` | + | `aws_context["function_name"]` | `faas.name` | + ### Removed - Spans no longer have a `description`. Use `name` instead. diff --git a/sentry_sdk/integrations/aws_lambda.py b/sentry_sdk/integrations/aws_lambda.py index 8579fcb6c5..656d71ec8e 100644 --- a/sentry_sdk/integrations/aws_lambda.py +++ b/sentry_sdk/integrations/aws_lambda.py @@ -39,6 +39,17 @@ MILLIS_TO_SECONDS = 1000.0 +EVENT_TO_ATTRIBUTES = { + "httpMethod": "http.request.method", + "queryStringParameters": "url.query", + "path": "url.path", +} + +CONTEXT_TO_ATTRIBUTES = { + "function_name": "faas.name", +} + + def _wrap_init_error(init_error): # type: (F) -> F @ensure_integration_enabled(AwsLambdaIntegration, init_error) @@ -151,10 +162,7 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs): name=aws_context.function_name, source=TRANSACTION_SOURCE_COMPONENT, origin=AwsLambdaIntegration.origin, - custom_sampling_context={ - "aws_event": aws_event, - "aws_context": aws_context, - }, + attributes=_prepopulate_attributes(aws_event, aws_context), ): try: return handler(aws_event, aws_context, *args, **kwargs) @@ -457,3 +465,29 @@ def _event_from_error_json(error_json): } # type: Event return event + + +def _prepopulate_attributes(aws_event, aws_context): + attributes = {} + + for prop, attr in EVENT_TO_ATTRIBUTES.items(): + if aws_event.get(prop) is not None: + attributes[attr] = aws_event[prop] + + for prop, attr in CONTEXT_TO_ATTRIBUTES.items(): + if getattr(aws_context, prop, None) is not None: + attributes[attr] = getattr(aws_context, prop) + + url = _get_url(aws_event, aws_context) + if url: + if aws_event.get("queryStringParameters"): + url += f"?{aws_event['queryStringParameters']}" + attributes["url.full"] = url + + headers = aws_event.get("headers") or {} + if headers.get("X-Forwarded-Proto"): + attributes["network.protocol.name"] = headers["X-Forwarded-Proto"] + if headers.get("Host"): + attributes["server.address"] = headers["Host"] + + return attributes diff --git a/tests/integrations/aws_lambda/test_aws.py b/tests/integrations/aws_lambda/test_aws.py index e229812336..c1235ae0a0 100644 --- a/tests/integrations/aws_lambda/test_aws.py +++ b/tests/integrations/aws_lambda/test_aws.py @@ -619,18 +619,12 @@ def test_handler(event, context): traces_sampler.assert_any_call( DictionaryContaining( { - "aws_event": DictionaryContaining({ - "httpMethod": "GET", - "path": "/sit/stay/rollover", - "headers": {"Host": "x.io", "X-Forwarded-Proto": "http"}, - }), - "aws_context": ObjectDescribedBy( - type=get_lambda_bootstrap().LambdaContext, - attrs={ - 'function_name': StringContaining("test_"), - 'function_version': '$LATEST', - } - ) + "http.request.method": "GET", + "url.path": "/sit/stay/rollover", + "url.query": "repeat=again", + "url.full": "http://x.io/sit/stay/rollover?repeat=twice", + "network.protocol.name": "http", + "server.address": "x.io", } ) ) @@ -649,7 +643,7 @@ def test_handler(event, context): ) """ ), - b'{"httpMethod": "GET", "path": "/sit/stay/rollover", "headers": {"Host": "x.io", "X-Forwarded-Proto": "http"}}', + b'{"httpMethod": "GET", "path": "/sit/stay/rollover", "query_string": {"repeat": "again"}, "headers": {"Host": "x.io", "X-Forwarded-Proto": "http"}}', ) assert response["Payload"]["AssertionError raised"] is False