Skip to content

Commit e64b7b9

Browse files
committed
stuff
1 parent 8cbdfa8 commit e64b7b9

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import json
2+
import os
3+
import sentry_sdk
4+
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
5+
6+
def trace_sampler(sampling_context):
7+
print("I AM IN TRACES SAMPLER")
8+
return 1
9+
10+
sentry_sdk.init(
11+
dsn=os.environ.get("SENTRY_DSN"),
12+
traces_sample_rate=1.0,
13+
traces_sampler=trace_sampler,
14+
integrations=[AwsLambdaIntegration()],
15+
)
16+
17+
def handler(event, context):
18+
# This is a simple Lambda function with Sentry SDK embedded
19+
return {
20+
"statusCode": 200,
21+
"body": json.dumps({
22+
"message": "Hello from Lambda with embedded Sentry SDK!",
23+
"event": event
24+
})
25+
}

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,21 @@ def test_span_origin(lambda_client, test_environment):
450450
assert (
451451
transaction_event["contexts"]["trace"]["origin"] == "auto.function.aws_lambda"
452452
)
453+
454+
455+
def test_traces_sampler_has_correct_sampling_context(lambda_client, test_environment):
456+
print("111")
457+
bla = lambda_client.invoke(
458+
FunctionName="TracesSampler",
459+
Payload=json.dumps({}),
460+
)
461+
print(bla)
462+
print("222")
463+
envelopes = test_environment["server"].envelopes
464+
print(envelopes)
465+
print("333")
466+
(transaction_event,) = envelopes
467+
print("444")
468+
assert (
469+
transaction_event["contexts"]["trace"]["origin"] == "auto.function.aws_lambda"
470+
)

tests/integrations/aws_lambda/utils.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import gzip
22
import json
33
import os
4+
import shutil
5+
import subprocess
46
import requests
57
import sys
68
import time
79
import threading
810
import socket
911
import platform
12+
import tempfile
1013

1114
from aws_cdk import (
1215
CfnResource,
@@ -20,6 +23,7 @@
2023

2124

2225
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/"
2327
LAMBDA_FUNCTION_TIMEOUT = 10
2428
SAM_PORT = 3001
2529

@@ -47,6 +51,20 @@ def get_host_ip():
4751
return host
4852

4953

54+
def get_project_root():
55+
"""
56+
Returns the absolute path to the project root directory.
57+
"""
58+
# Start from the current file's directory
59+
current_dir = os.path.dirname(os.path.abspath(__file__))
60+
61+
# Navigate up to the project root (4 levels up from tests/integrations/aws_lambda/)
62+
# This is equivalent to the multiple dirname() calls
63+
project_root = os.path.abspath(os.path.join(current_dir, "../../../"))
64+
65+
return project_root
66+
67+
5068
class LocalLambdaStack(Stack):
5169
"""
5270
Uses the AWS CDK to create a local SAM stack containing Lambda functions.
@@ -124,6 +142,65 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
124142
)
125143
)
126144

145+
print(
146+
"[LocalLambdaStack] Add all Lambda functions defined in "
147+
"/tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/ to the SAM stack"
148+
)
149+
lambda_dirs = [
150+
d
151+
for d in os.listdir(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR)
152+
if os.path.isdir(os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, d))
153+
]
154+
for lambda_dir in lambda_dirs:
155+
# Copy the Sentry SDK into the function directory
156+
import ipdb; ipdb.set_trace()
157+
sdk_path = os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir, "sentry_sdk")
158+
if not os.path.exists(sdk_path):
159+
# Find the Sentry SDK in the current environment
160+
import sentry_sdk as sdk_module
161+
sdk_source = os.path.dirname(sdk_module.__file__)
162+
shutil.copytree(sdk_source, sdk_path)
163+
164+
# Install the requirements of Sentry SDK into the function directory
165+
requirements_file = os.path.join(get_project_root(), "requirements-aws-lambda-layer.txt")
166+
167+
# Install the package using pip
168+
subprocess.check_call([
169+
sys.executable,
170+
"-m",
171+
"pip",
172+
"install",
173+
"--target",
174+
os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir),
175+
"-r",
176+
requirements_file,
177+
])
178+
179+
180+
CfnResource(
181+
self,
182+
lambda_dir,
183+
type="AWS::Serverless::Function",
184+
properties={
185+
"CodeUri": os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir),
186+
"Handler": "sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler",
187+
"Runtime": PYTHON_VERSION,
188+
"Timeout": LAMBDA_FUNCTION_TIMEOUT,
189+
"Environment": {
190+
"Variables": {
191+
"SENTRY_DSN": dsn,
192+
}
193+
},
194+
},
195+
)
196+
print(
197+
"[LocalLambdaStack] - Created Lambda function: %s (%s)"
198+
% (
199+
lambda_dir,
200+
os.path.join(LAMBDA_FUNCTION_DIR, lambda_dir),
201+
)
202+
)
203+
127204
@classmethod
128205
def wait_for_stack(cls, timeout=30, port=SAM_PORT):
129206
"""

0 commit comments

Comments
 (0)