Skip to content

Commit a858cdb

Browse files
committed
More tests
1 parent e815a6e commit a858cdb

File tree

6 files changed

+130
-1
lines changed

6 files changed

+130
-1
lines changed
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import sentry_sdk
3+
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
4+
5+
6+
sentry_sdk.init(
7+
dsn=os.environ.get("SENTRY_DSN"),
8+
traces_sample_rate=None, # this is the default, just added for clarity
9+
integrations=[AwsLambdaIntegration()],
10+
)
11+
12+
13+
def handler(event, context):
14+
raise Exception("Oh!")
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
import sentry_sdk
3+
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
4+
5+
6+
sentry_sdk.init(
7+
dsn=os.environ.get("SENTRY_DSN"),
8+
traces_sample_rate=1.0,
9+
integrations=[AwsLambdaIntegration()],
10+
)
11+
12+
13+
def handler(event, context):
14+
raise Exception("Oh!")

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,3 +469,82 @@ def test_traces_sampler_has_correct_sampling_context(lambda_client, test_environ
469469
assert sampling_context_data.get("aws_event_present") is True
470470
assert sampling_context_data.get("aws_context_present") is True
471471
assert sampling_context_data.get("event_data", {}).get("test_key") == "test_value"
472+
473+
474+
@pytest.mark.parametrize(
475+
"lambda_function_name",
476+
["RaiseErrorPerformanceEnabled", "RaiseErrorPerformanceDisabled"],
477+
)
478+
def test_error_has_new_trace_context(
479+
lambda_client, test_environment, lambda_function_name
480+
):
481+
lambda_client.invoke(
482+
FunctionName=lambda_function_name,
483+
Payload=json.dumps({}),
484+
)
485+
envelopes = test_environment["server"].envelopes
486+
487+
if lambda_function_name == "RaiseErrorPerformanceEnabled":
488+
(error_event, transaction_event) = envelopes
489+
else:
490+
(error_event,) = envelopes
491+
transaction_event = None
492+
493+
assert "trace" in error_event["contexts"]
494+
assert "trace_id" in error_event["contexts"]["trace"]
495+
496+
if transaction_event:
497+
assert "trace" in transaction_event["contexts"]
498+
assert "trace_id" in transaction_event["contexts"]["trace"]
499+
assert (
500+
error_event["contexts"]["trace"]["trace_id"]
501+
== transaction_event["contexts"]["trace"]["trace_id"]
502+
)
503+
504+
505+
@pytest.mark.parametrize(
506+
"lambda_function_name",
507+
["RaiseErrorPerformanceEnabled", "RaiseErrorPerformanceDisabled"],
508+
)
509+
def test_error_has_existing_trace_context(
510+
lambda_client, test_environment, lambda_function_name
511+
):
512+
trace_id = "471a43a4192642f0b136d5159a501701"
513+
parent_span_id = "6e8f22c393e68f19"
514+
parent_sampled = 1
515+
sentry_trace_header = "{}-{}-{}".format(trace_id, parent_span_id, parent_sampled)
516+
517+
# We simulate here AWS Api Gateway's behavior of passing HTTP headers
518+
# as the `headers` dict in the event passed to the Lambda function.
519+
payload = {
520+
"headers": {
521+
"sentry-trace": sentry_trace_header,
522+
}
523+
}
524+
525+
lambda_client.invoke(
526+
FunctionName=lambda_function_name,
527+
Payload=json.dumps(payload),
528+
)
529+
envelopes = test_environment["server"].envelopes
530+
531+
if lambda_function_name == "RaiseErrorPerformanceEnabled":
532+
(error_event, transaction_event) = envelopes
533+
else:
534+
(error_event,) = envelopes
535+
transaction_event = None
536+
537+
assert "trace" in error_event["contexts"]
538+
assert "trace_id" in error_event["contexts"]["trace"]
539+
assert (
540+
error_event["contexts"]["trace"]["trace_id"]
541+
== "471a43a4192642f0b136d5159a501701"
542+
)
543+
544+
if transaction_event:
545+
assert "trace" in transaction_event["contexts"]
546+
assert "trace_id" in transaction_event["contexts"]["trace"]
547+
assert (
548+
transaction_event["contexts"]["trace"]["trace_id"]
549+
== "471a43a4192642f0b136d5159a501701"
550+
)

tests/integrations/aws_lambda/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
211211
)
212212

213213
@classmethod
214-
def wait_for_stack(cls, timeout=30, port=SAM_PORT):
214+
def wait_for_stack(cls, timeout=60, port=SAM_PORT):
215215
"""
216216
Wait for SAM to be ready, with timeout.
217217
"""

0 commit comments

Comments
 (0)