Skip to content

Commit 578e4ef

Browse files
Merge remote-tracking branch 'origin/major/3.0' into junior/aws-lambda-producer-links
# Conflicts: # sentry_sdk/integrations/aws_lambda/_handler.py # tests/integrations/aws_lambda/test_aws_lambda.py
2 parents b43bfc6 + 45f0192 commit 578e4ef

7 files changed

Lines changed: 105 additions & 8 deletions

File tree

‎MIGRATION_GUIDE.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,16 @@ sentry_sdk.init(
332332
"http_headers": {
333333
"request": {
334334
"mode": "denylist",
335-
"terms": ["forwarded", "-ip", "remote-", "via", "-user"]
335+
"terms": ["forwarded", "-ip", "remote-", "via", "-user"],
336336
},
337337
},
338-
"cookies": { "mode": "denylist", "terms": ["forwarded", "-ip", "remote-", "via", "-user"] },
338+
"cookies": {
339+
"mode": "denylist",
340+
"terms": ["forwarded", "-ip", "remote-", "via", "-user"],
341+
},
339342
"url_query_params": {
340-
"mode": "denylist", "terms": ["forwarded", "-ip", "remote-", "via", "-user"],
343+
"mode": "denylist",
344+
"terms": ["forwarded", "-ip", "remote-", "via", "-user"],
341345
},
342346
},
343347
)

‎sentry_sdk/integrations/aiomysql.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from __future__ import annotations
32

43
from typing import Any, Awaitable, Callable, TypeVar

‎sentry_sdk/integrations/aws_lambda/_handler.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,15 @@ def sentry_handler(
255255
with capture_internal_exceptions():
256256
producer_links = _get_producer_links(aws_event)
257257

258-
sentry_sdk.continue_trace(headers)
258+
# a synchronous `Invoke` passes the invoking span in `ClientContext`; it wins over
259+
# event headers, which a forwarded HTTP event keeps from further upstream.
260+
# https://docs.aws.amazon.com/lambda/latest/dg/python-context.html
261+
client_context = getattr(aws_context, "client_context", None)
262+
custom = getattr(client_context, "custom", None)
263+
if custom and SENTRY_TRACE_HEADER_NAME in custom:
264+
sentry_sdk.continue_trace(custom)
265+
else:
266+
sentry_sdk.continue_trace(headers)
259267
Scope.set_custom_sampling_context(sampling_context)
260268
with sentry_sdk.start_span(
261269
name=function_name,

‎sentry_sdk/integrations/pydantic_ai/spans/invoke_agent.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ def invoke_agent_span(
6363
system_texts = []
6464

6565
if agent:
66-
# Check for system_prompt
6766
system_prompts = getattr(agent, "_system_prompts", None) or []
6867
for prompt in system_prompts:
6968
if isinstance(prompt, str):
@@ -101,7 +100,6 @@ def invoke_agent_span(
101100
}
102101
)
103102
elif isinstance(user_prompt, list):
104-
# Handle list of user content
105103
content = []
106104
for item in user_prompt:
107105
if isinstance(item, str):
@@ -138,7 +136,6 @@ def update_invoke_agent_span(
138136
if not span or not result:
139137
return
140138

141-
# Extract output from result
142139
output = getattr(result, "output", None)
143140

144141
# Set response text if prompts are enabled
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
@@ -998,6 +998,39 @@ def test_producer_links(lambda_client, test_environment, aws_event, links):
998998
assert segment_span["links"] == links
999999

10001000

1001+
@pytest.mark.parametrize(
1002+
"headers",
1003+
[
1004+
pytest.param({}, id="client-context"),
1005+
# `ClientContext` wins over event headers.
1006+
pytest.param(
1007+
{"sentry-trace": "0f4c2a5b8d9e4f1a2b3c4d5e6f7a8b9c-1a2b3c4d5e6f7a8b-1"},
1008+
id="client-context-and-event-headers",
1009+
),
1010+
],
1011+
)
1012+
def test_trace_continuation_from_client_context(
1013+
lambda_client, test_environment, headers
1014+
):
1015+
payload = {
1016+
"headers": headers,
1017+
# `ClientContextTrace` passes it to the runtime as the invocation's `ClientContext`.
1018+
"client_context": {
1019+
"custom": {
1020+
"sentry-trace": "471a43a4192642f0b136d5159a501701-6e8f22c393e68f19-1"
1021+
}
1022+
},
1023+
}
1024+
1025+
lambda_client.invoke(FunctionName="ClientContextTrace", Payload=json.dumps(payload))
1026+
1027+
(segment_span,) = [
1028+
s for s in test_environment["server"].span_items if s["is_segment"]
1029+
]
1030+
assert segment_span["trace_id"] == "471a43a4192642f0b136d5159a501701"
1031+
assert segment_span["parent_span_id"] == "6e8f22c393e68f19"
1032+
1033+
10011034
def test_request_attributes(lambda_client, test_environment):
10021035
payload = {
10031036
"headers": {

0 commit comments

Comments
 (0)