Skip to content

Commit 5e822de

Browse files
authored
Span attrs instead of AWS custom sampling context (#3814)
1 parent a9b4d47 commit 5e822de

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

MIGRATION_GUIDE.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
9393

9494
Note that `rq.job.args`, `rq.job.kwargs`, and `rq.job.func` are serialized and not the actual objects on the job.
9595

96+
- 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:
97+
98+
| AWS property | Sampling context key(s) |
99+
| ------------------------------------------- | ----------------------- |
100+
| `aws_event["httpMethod"]` | `http.request.method` |
101+
| `aws_event["queryStringParameters"]` | `url.query` |
102+
| `aws_event["path"]` | `url.path` |
103+
| full URL | `url.full` |
104+
| `aws_event["headers"]["X-Forwarded-Proto"]` | `network.protocol.name` |
105+
| `aws_event["headers"]["Host"]` | `server.address` |
106+
| `aws_context["function_name"]` | `faas.name` |
107+
96108
### Removed
97109

98110
- Spans no longer have a `description`. Use `name` instead.

sentry_sdk/integrations/aws_lambda.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@
3939
MILLIS_TO_SECONDS = 1000.0
4040

4141

42+
EVENT_TO_ATTRIBUTES = {
43+
"httpMethod": "http.request.method",
44+
"queryStringParameters": "url.query",
45+
"path": "url.path",
46+
}
47+
48+
CONTEXT_TO_ATTRIBUTES = {
49+
"function_name": "faas.name",
50+
}
51+
52+
4253
def _wrap_init_error(init_error):
4354
# type: (F) -> F
4455
@ensure_integration_enabled(AwsLambdaIntegration, init_error)
@@ -151,10 +162,7 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
151162
name=aws_context.function_name,
152163
source=TRANSACTION_SOURCE_COMPONENT,
153164
origin=AwsLambdaIntegration.origin,
154-
custom_sampling_context={
155-
"aws_event": aws_event,
156-
"aws_context": aws_context,
157-
},
165+
attributes=_prepopulate_attributes(aws_event, aws_context),
158166
):
159167
try:
160168
return handler(aws_event, aws_context, *args, **kwargs)
@@ -457,3 +465,29 @@ def _event_from_error_json(error_json):
457465
} # type: Event
458466

459467
return event
468+
469+
470+
def _prepopulate_attributes(aws_event, aws_context):
471+
attributes = {}
472+
473+
for prop, attr in EVENT_TO_ATTRIBUTES.items():
474+
if aws_event.get(prop) is not None:
475+
attributes[attr] = aws_event[prop]
476+
477+
for prop, attr in CONTEXT_TO_ATTRIBUTES.items():
478+
if getattr(aws_context, prop, None) is not None:
479+
attributes[attr] = getattr(aws_context, prop)
480+
481+
url = _get_url(aws_event, aws_context)
482+
if url:
483+
if aws_event.get("queryStringParameters"):
484+
url += f"?{aws_event['queryStringParameters']}"
485+
attributes["url.full"] = url
486+
487+
headers = aws_event.get("headers") or {}
488+
if headers.get("X-Forwarded-Proto"):
489+
attributes["network.protocol.name"] = headers["X-Forwarded-Proto"]
490+
if headers.get("Host"):
491+
attributes["server.address"] = headers["Host"]
492+
493+
return attributes

tests/integrations/aws_lambda/test_aws.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -619,18 +619,12 @@ def test_handler(event, context):
619619
traces_sampler.assert_any_call(
620620
DictionaryContaining(
621621
{
622-
"aws_event": DictionaryContaining({
623-
"httpMethod": "GET",
624-
"path": "/sit/stay/rollover",
625-
"headers": {"Host": "x.io", "X-Forwarded-Proto": "http"},
626-
}),
627-
"aws_context": ObjectDescribedBy(
628-
type=get_lambda_bootstrap().LambdaContext,
629-
attrs={
630-
'function_name': StringContaining("test_"),
631-
'function_version': '$LATEST',
632-
}
633-
)
622+
"http.request.method": "GET",
623+
"url.path": "/sit/stay/rollover",
624+
"url.query": "repeat=again",
625+
"url.full": "http://x.io/sit/stay/rollover?repeat=twice",
626+
"network.protocol.name": "http",
627+
"server.address": "x.io",
634628
}
635629
)
636630
)
@@ -649,7 +643,7 @@ def test_handler(event, context):
649643
)
650644
"""
651645
),
652-
b'{"httpMethod": "GET", "path": "/sit/stay/rollover", "headers": {"Host": "x.io", "X-Forwarded-Proto": "http"}}',
646+
b'{"httpMethod": "GET", "path": "/sit/stay/rollover", "query_string": {"repeat": "again"}, "headers": {"Host": "x.io", "X-Forwarded-Proto": "http"}}',
653647
)
654648

655649
assert response["Payload"]["AssertionError raised"] is False

0 commit comments

Comments
 (0)