Skip to content

Commit 45f0192

Browse files
sentry-junior[bot]pabloDeputterdingsdax
authored
feat(aws-lambda): Continue the trace from Invoke ClientContext (#8221)
### Description With #8117 (merged), a synchronous boto3 `Invoke` carries `sentry-trace` and `baggage` in `ClientContext.custom`. This PR adds the receiving side: the Lambda integration continues the trace from `context.client_context.custom`. `ClientContext` wins over event headers, because a forwarded HTTP event can still carry headers from further upstream. `sam local` drops `ClientContext`, so the new `ClientContextTrace` test function hands the event's `client_context` to the runtime. That way the container tests can cover it. This is Pablo's code from `pablo/aws-lambda-client-context-trace`. The one change: `client_context` and `custom` are read with `getattr`, so a context object without them falls back to event headers instead of failing. It's the first of three stacked PRs for the stretch Lambda work: this PR → trigger detection (#8222) → `queue.process` spans (#8223). #### Issues * resolves: PY-3009 (receiving side; #8117 does the injection) * Part of #8071 <!-- junior-request-attribution:start --> via @dingsdax. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:slack%3AC0C9X9V5NMT%3A1793904671.980379 --> -- [View Session](https://junior-prod.sentry.dev/conversations/slack%3AC0C9X9V5NMT%3A1793904671.980379) [[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0C9X9V5NMT%3A1793904671.980379/?project=4510944073809921) <!-- junior-session-footer:end --> --------- Co-authored-by: Pablo Deputter <pablo.deputter@sentry.io> Co-authored-by: Pablo Deputter <71842639+pabloDeputter@users.noreply.github.com> Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: Johannes Daxböck <114897+dingsdax@users.noreply.github.com>
1 parent a521dc4 commit 45f0192

4 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎sentry_sdk/integrations/aws_lambda/_handler.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ORIGIN,
2121
)
2222
from sentry_sdk.scope import Scope
23-
from sentry_sdk.traces import SegmentNameSource
23+
from sentry_sdk.traces import SENTRY_TRACE_HEADER_NAME, SegmentNameSource
2424
from sentry_sdk.utils import (
2525
TimeoutThread,
2626
capture_internal_exceptions,
@@ -187,7 +187,15 @@ def sentry_handler(
187187
}
188188
)
189189

190-
sentry_sdk.continue_trace(headers)
190+
# a synchronous `Invoke` passes the invoking span in `ClientContext`; it wins over
191+
# event headers, which a forwarded HTTP event keeps from further upstream.
192+
# https://docs.aws.amazon.com/lambda/latest/dg/python-context.html
193+
client_context = getattr(aws_context, "client_context", None)
194+
custom = getattr(client_context, "custom", None)
195+
if custom and SENTRY_TRACE_HEADER_NAME in custom:
196+
sentry_sdk.continue_trace(custom)
197+
else:
198+
sentry_sdk.continue_trace(headers)
191199
Scope.set_custom_sampling_context(sampling_context)
192200
with sentry_sdk.start_span(
193201
name=function_name,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Need to add some ignore rules in this directory, because the unit tests will add the Sentry SDK and its dependencies
2+
# into this directory to create a Lambda function package that contains everything needed to instrument a Lambda function using Sentry.
3+
4+
# Ignore everything
5+
*
6+
7+
# But not index.py
8+
!index.py
9+
10+
# And not .gitignore itself
11+
!.gitignore
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import json
2+
import os
3+
4+
import sentry_sdk
5+
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
6+
from sentry_sdk.integrations.aws_lambda._runtime import get_lambda_bootstrap
7+
8+
sentry_sdk.init(
9+
dsn=os.environ.get("SENTRY_DSN"),
10+
traces_sample_rate=1.0,
11+
integrations=[AwsLambdaIntegration()],
12+
)
13+
14+
# `sam local` drops `ClientContext`; pass the event's `client_context` to the runtime
15+
# instead, like lambda does for a synchronous `Invoke`.
16+
bootstrap = get_lambda_bootstrap()
17+
handle_event_request = bootstrap.handle_event_request
18+
19+
20+
def handle_event_request_with_client_context(
21+
lambda_runtime_client,
22+
request_handler,
23+
invoke_id,
24+
event_body,
25+
content_type,
26+
client_context_json,
27+
*args,
28+
):
29+
client_context_json = json.dumps(json.loads(event_body)["client_context"])
30+
return handle_event_request(
31+
lambda_runtime_client,
32+
request_handler,
33+
invoke_id,
34+
event_body,
35+
content_type,
36+
client_context_json,
37+
*args,
38+
)
39+
40+
41+
bootstrap.handle_event_request = handle_event_request_with_client_context
42+
43+
44+
def handler(event, context):
45+
return {"event": event}

‎tests/integrations/aws_lambda/test_aws_lambda.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,39 @@ def test_trace_continuation(lambda_client, test_environment):
870870
assert SPANDATA.FAAS_INVOCATION_ID in attrs
871871

872872

873+
@pytest.mark.parametrize(
874+
"headers",
875+
[
876+
pytest.param({}, id="client-context"),
877+
# `ClientContext` wins over event headers.
878+
pytest.param(
879+
{"sentry-trace": "0f4c2a5b8d9e4f1a2b3c4d5e6f7a8b9c-1a2b3c4d5e6f7a8b-1"},
880+
id="client-context-and-event-headers",
881+
),
882+
],
883+
)
884+
def test_trace_continuation_from_client_context(
885+
lambda_client, test_environment, headers
886+
):
887+
payload = {
888+
"headers": headers,
889+
# `ClientContextTrace` passes it to the runtime as the invocation's `ClientContext`.
890+
"client_context": {
891+
"custom": {
892+
"sentry-trace": "471a43a4192642f0b136d5159a501701-6e8f22c393e68f19-1"
893+
}
894+
},
895+
}
896+
897+
lambda_client.invoke(FunctionName="ClientContextTrace", Payload=json.dumps(payload))
898+
899+
(segment_span,) = [
900+
s for s in test_environment["server"].span_items if s["is_segment"]
901+
]
902+
assert segment_span["trace_id"] == "471a43a4192642f0b136d5159a501701"
903+
assert segment_span["parent_span_id"] == "6e8f22c393e68f19"
904+
905+
873906
def test_request_attributes(lambda_client, test_environment):
874907
payload = {
875908
"headers": {

0 commit comments

Comments
 (0)