Skip to content
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

Span attrs instead of AWS custom sampling context #3814

Merged
merged 7 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 38 additions & 4 deletions sentry_sdk/integrations/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
20 changes: 7 additions & 13 deletions tests/integrations/aws_lambda/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
)
)
Expand All @@ -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
Expand Down
Loading