Skip to content

Commit d51882e

Browse files
committed
Testing for sampling_context set by aws lambda integration
1 parent d0687a9 commit d51882e

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,29 @@
33
import sentry_sdk
44
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
55

6+
# 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+
}
12+
13+
614
def trace_sampler(sampling_context):
7-
print("I AM IN TRACES SAMPLER")
8-
return 1
15+
# Store the sampling context for verification
16+
global sampling_context_data
17+
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)
27+
return 1.0 # Always sample
28+
929

1030
sentry_sdk.init(
1131
dsn=os.environ.get("SENTRY_DSN"),
@@ -14,12 +34,16 @@ def trace_sampler(sampling_context):
1434
integrations=[AwsLambdaIntegration()],
1535
)
1636

37+
1738
def handler(event, context):
18-
# This is a simple Lambda function with Sentry SDK embedded
39+
# Return the sampling context data for verification
1940
return {
2041
"statusCode": 200,
21-
"body": json.dumps({
22-
"message": "Hello from Lambda with embedded Sentry SDK!",
23-
"event": event
24-
})
42+
"body": json.dumps(
43+
{
44+
"message": "Hello from Lambda with embedded Sentry SDK!",
45+
"event": event,
46+
"sampling_context_data": sampling_context_data,
47+
}
48+
),
2549
}

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,19 @@ def test_span_origin(lambda_client, test_environment):
453453

454454

455455
def test_traces_sampler_has_correct_sampling_context(lambda_client, test_environment):
456-
lambda_client.invoke(
456+
"""
457+
Test that aws_event and aws_context are passed in the custom_sampling_context
458+
when using the AWS Lambda integration.
459+
"""
460+
test_payload = {"test_key": "test_value"}
461+
response = lambda_client.invoke(
457462
FunctionName="TracesSampler",
458-
Payload=json.dumps({}),
459-
)
460-
envelopes = test_environment["server"].envelopes
461-
(transaction_event,) = envelopes
462-
assert (
463-
transaction_event["contexts"]["trace"]["origin"] == "auto.function.aws_lambda"
463+
Payload=json.dumps(test_payload),
464464
)
465+
response_payload = json.loads(response["Payload"].read().decode())
466+
sampling_context_data = json.loads(response_payload["body"])[
467+
"sampling_context_data"
468+
]
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"

tests/integrations/aws_lambda/utils.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323

2424

2525
LAMBDA_FUNCTION_DIR = "./tests/integrations/aws_lambda/lambda_functions/"
26-
LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR = "./tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/"
26+
LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR = (
27+
"./tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/"
28+
)
2729
LAMBDA_FUNCTION_TIMEOUT = 10
2830
SAM_PORT = 3001
2931

@@ -57,11 +59,11 @@ def get_project_root():
5759
"""
5860
# Start from the current file's directory
5961
current_dir = os.path.dirname(os.path.abspath(__file__))
60-
62+
6163
# Navigate up to the project root (4 levels up from tests/integrations/aws_lambda/)
6264
# This is equivalent to the multiple dirname() calls
6365
project_root = os.path.abspath(os.path.join(current_dir, "../../../"))
64-
66+
6567
return project_root
6668

6769

@@ -153,35 +155,44 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
153155
]
154156
for lambda_dir in lambda_dirs:
155157
# Copy the Sentry SDK into the function directory
156-
sdk_path = os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir, "sentry_sdk")
158+
sdk_path = os.path.join(
159+
LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir, "sentry_sdk"
160+
)
157161
if not os.path.exists(sdk_path):
158162
# Find the Sentry SDK in the current environment
159163
import sentry_sdk as sdk_module
164+
160165
sdk_source = os.path.dirname(sdk_module.__file__)
161166
shutil.copytree(sdk_source, sdk_path)
162167

163-
# Install the requirements of Sentry SDK into the function directory
164-
requirements_file = os.path.join(get_project_root(), "requirements-aws-lambda-layer.txt")
165-
168+
# Install the requirements of Sentry SDK into the function directory
169+
requirements_file = os.path.join(
170+
get_project_root(), "requirements-aws-lambda-layer.txt"
171+
)
172+
166173
# Install the package using pip
167-
subprocess.check_call([
168-
sys.executable,
169-
"-m",
170-
"pip",
171-
"install",
172-
"--upgrade",
173-
"--target",
174-
os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir),
175-
"-r",
176-
requirements_file,
177-
])
178-
174+
subprocess.check_call(
175+
[
176+
sys.executable,
177+
"-m",
178+
"pip",
179+
"install",
180+
"--upgrade",
181+
"--target",
182+
os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir),
183+
"-r",
184+
requirements_file,
185+
]
186+
)
187+
179188
CfnResource(
180189
self,
181190
lambda_dir,
182191
type="AWS::Serverless::Function",
183192
properties={
184-
"CodeUri": os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir),
193+
"CodeUri": os.path.join(
194+
LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir
195+
),
185196
"Handler": "index.handler",
186197
"Runtime": PYTHON_VERSION,
187198
"Timeout": LAMBDA_FUNCTION_TIMEOUT,

0 commit comments

Comments
 (0)