Skip to content

Commit 0a2d878

Browse files
authored
Fix AWS Lambda tests (#4199)
With the refactoring of the AWS Lambda test suite in `master`. We need to update the tests in `potel-base` to succeed again. This also fixes a check in the `stdlib` integration. With local AWS Lambda we have a DSN that includes a port. This did not work and this PR makes this work. (This will allow support for self hosted Sentry running on a specific port too)
1 parent a2ce2bb commit 0a2d878

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

sentry_sdk/integrations/stdlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def putrequest(self, method, url, *args, **kwargs):
7373

7474
client = sentry_sdk.get_client()
7575
if client.get_integration(StdlibIntegration) is None or is_sentry_url(
76-
client, host
76+
client, f"{host}:{port}"
7777
):
7878
return real_putrequest(self, method, url, *args, **kwargs)
7979

tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/TracesSampler/index.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,14 @@
44
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
55

66
# Global variables to store sampling context for verification
7-
sampling_context_data = {
8-
"aws_event_present": False,
9-
"aws_context_present": False,
10-
"event_data": None,
11-
}
7+
sampling_context_data = None
128

139

1410
def trace_sampler(sampling_context):
1511
# Store the sampling context for verification
1612
global sampling_context_data
13+
sampling_context_data = sampling_context
1714

18-
# Check if aws_event and aws_context are in the sampling_context
19-
if "aws_event" in sampling_context:
20-
sampling_context_data["aws_event_present"] = True
21-
sampling_context_data["event_data"] = sampling_context["aws_event"]
22-
23-
if "aws_context" in sampling_context:
24-
sampling_context_data["aws_context_present"] = True
25-
26-
print("Sampling context data:", sampling_context_data)
2715
return 1.0 # Always sample
2816

2917

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_environment():
6767

6868
try:
6969
# Wait for SAM to be ready
70-
LocalLambdaStack.wait_for_stack()
70+
LocalLambdaStack.wait_for_stack(log_file=debug_log_file)
7171

7272
def before_test():
7373
server.clear_envelopes()
@@ -137,12 +137,12 @@ def test_basic_no_exception(lambda_client, test_environment):
137137
}
138138
assert transaction_event["contexts"]["trace"] == {
139139
"op": "function.aws",
140-
"description": mock.ANY,
141140
"span_id": mock.ANY,
142141
"parent_span_id": mock.ANY,
143142
"trace_id": mock.ANY,
144143
"origin": "auto.function.aws_lambda",
145144
"data": mock.ANY,
145+
"status": "ok",
146146
}
147147

148148

@@ -178,7 +178,6 @@ def test_basic_exception(lambda_client, test_environment):
178178
}
179179
assert error_event["contexts"]["trace"] == {
180180
"op": "function.aws",
181-
"description": mock.ANY,
182181
"span_id": mock.ANY,
183182
"parent_span_id": mock.ANY,
184183
"trace_id": mock.ANY,
@@ -314,9 +313,7 @@ def test_non_dict_event(
314313
"headers": {"Host": "x1.io", "X-Forwarded-Proto": "https"},
315314
"method": "GET",
316315
"url": "https://x1.io/1",
317-
"query_string": {
318-
"done": "f",
319-
},
316+
"query_string": "done=f",
320317
}
321318
else:
322319
request_data = {"url": "awslambda:///BasicException"}
@@ -343,7 +340,8 @@ def test_request_data(lambda_client, test_environment):
343340
"X-Forwarded-Proto": "https"
344341
},
345342
"queryStringParameters": {
346-
"bonkers": "true"
343+
"bonkers": "true",
344+
"wild": "false"
347345
},
348346
"pathParameters": null,
349347
"stageVariables": null,
@@ -373,7 +371,7 @@ def test_request_data(lambda_client, test_environment):
373371
"X-Forwarded-Proto": "https",
374372
},
375373
"method": "GET",
376-
"query_string": {"bonkers": "true"},
374+
"query_string": "bonkers=true&wild=false",
377375
"url": "https://iwsz2c7uwi.execute-api.us-east-1.amazonaws.com/asd",
378376
}
379377

@@ -457,7 +455,19 @@ def test_traces_sampler_has_correct_sampling_context(lambda_client, test_environ
457455
Test that aws_event and aws_context are passed in the custom_sampling_context
458456
when using the AWS Lambda integration.
459457
"""
460-
test_payload = {"test_key": "test_value"}
458+
test_payload = {
459+
"test_key": "test_value",
460+
"httpMethod": "GET",
461+
"queryStringParameters": {
462+
"test_query_param": "test_query_value",
463+
},
464+
"path": "/test",
465+
"headers": {
466+
"X-Forwarded-Proto": "https",
467+
"Host": "example.com",
468+
"X-Bla": "blabla",
469+
},
470+
}
461471
response = lambda_client.invoke(
462472
FunctionName="TracesSampler",
463473
Payload=json.dumps(test_payload),
@@ -466,9 +476,28 @@ def test_traces_sampler_has_correct_sampling_context(lambda_client, test_environ
466476
sampling_context_data = json.loads(response_payload["body"])[
467477
"sampling_context_data"
468478
]
469-
assert sampling_context_data.get("aws_event_present") is True
470-
assert sampling_context_data.get("aws_context_present") is True
471-
assert sampling_context_data.get("event_data", {}).get("test_key") == "test_value"
479+
480+
assert sampling_context_data == {
481+
"transaction_context": {
482+
"name": "TracesSampler",
483+
"op": "function.aws",
484+
"source": "component",
485+
},
486+
"http.request.method": "GET",
487+
"url.query": "test_query_param=test_query_value",
488+
"url.path": "/test",
489+
"url.full": "https://example.com/test?test_query_param=test_query_value",
490+
"network.protocol.name": "https",
491+
"server.address": "example.com",
492+
"faas.name": "TracesSampler",
493+
"http.request.header.x-forwarded-proto": "https",
494+
"http.request.header.host": "example.com",
495+
"http.request.header.x-bla": "blabla",
496+
"sentry.op": "function.aws",
497+
"sentry.source": "component",
498+
"parent_sampled": None,
499+
"cloud.provider": "aws",
500+
}
472501

473502

474503
@pytest.mark.parametrize(

tests/integrations/aws_lambda/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,16 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
211211
)
212212

213213
@classmethod
214-
def wait_for_stack(cls, timeout=60, port=SAM_PORT):
214+
def wait_for_stack(cls, timeout=60, port=SAM_PORT, log_file=None):
215215
"""
216216
Wait for SAM to be ready, with timeout.
217217
"""
218218
start_time = time.time()
219219
while True:
220220
if time.time() - start_time > timeout:
221221
raise TimeoutError(
222-
"AWS SAM failed to start within %s seconds. (Maybe Docker is not running?)"
223-
% timeout
222+
"AWS SAM failed to start within %s seconds. (Maybe Docker is not running, or new docker images could not be built in time?) Check the log for more details: %s"
223+
% (timeout, log_file)
224224
)
225225

226226
try:

0 commit comments

Comments
 (0)