Skip to content

Commit 7bb1a4a

Browse files
committed
Full working example with layer and sentry server collecting envelopes
1 parent 751f1fe commit 7bb1a4a

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
from somelayer import layer_function
2+
3+
14
def handler(event, context):
2-
return {"message": f"Hello, {event['name']}!"}
5+
message = f"Hello, {event['name']}! ({layer_function()})"
6+
return {"message": message}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import urllib.request
2+
import urllib.parse
3+
import json
4+
5+
6+
def layer_function():
7+
data = json.dumps({"event": "test from layer"}).encode("utf-8")
8+
req = urllib.request.Request(
9+
"http://host.docker.internal:9999/api/0/envelope/",
10+
data=data,
11+
headers={"Content-Type": "application/json"},
12+
)
13+
with urllib.request.urlopen(req) as response:
14+
return "This is from the Layer"

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
import subprocess
66
import time
7+
import threading
78
import yaml
89
from aws_cdk import (
910
App,
@@ -26,6 +27,25 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
2627
self.template_options.template_format_version = "2010-09-09"
2728
self.template_options.transforms = ["AWS::Serverless-2016-10-31"]
2829

30+
# Create Sentry Lambda Layer
31+
layer = CfnResource(
32+
self,
33+
"SentryPythonServerlessSDK",
34+
type="AWS::Serverless::LayerVersion",
35+
properties={
36+
"ContentUri": "./tests/integrations/aws_lambda/lambda_layer",
37+
"CompatibleRuntimes": [
38+
"python3.7",
39+
"python3.8",
40+
"python3.9",
41+
"python3.10",
42+
"python3.11",
43+
"python3.12",
44+
"python3.13",
45+
],
46+
},
47+
)
48+
2949
# Add the function using SAM format
3050
CfnResource(
3151
self,
@@ -35,6 +55,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
3555
"CodeUri": "./tests/integrations/aws_lambda/lambda_functions/hello_world",
3656
"Handler": "index.handler",
3757
"Runtime": "python3.12",
58+
"Layers": [{"Ref": layer.logical_id}],
3859
},
3960
)
4061

@@ -59,11 +80,55 @@ def wait_for_sam(timeout=30, port=SAM_PORT):
5980
continue
6081

6182

83+
class SentryTestServer:
84+
def __init__(self, port=9999):
85+
self.envelopes = []
86+
self.port = port
87+
88+
from fastapi import FastAPI, Request
89+
90+
self.app = FastAPI()
91+
92+
@self.app.get("/")
93+
async def root():
94+
return {
95+
"message": "Sentry Test Server. Use DSN http://123@localhost:9999/0 in your SDK."
96+
}
97+
98+
@self.app.post("/api/0/envelope/")
99+
async def envelope(request: Request):
100+
self.envelopes.append(await request.json())
101+
return {"status": "ok"}
102+
103+
def run_server(self):
104+
import uvicorn
105+
106+
uvicorn.run(self.app, host="0.0.0.0", port=self.port)
107+
108+
def start(self):
109+
server_thread = threading.Thread(target=self.run_server, daemon=True)
110+
server_thread.start()
111+
112+
def clear_envelopes(self):
113+
self.envelopes = []
114+
115+
116+
@pytest.fixture(scope="session")
117+
def sentry_test_server():
118+
server = SentryTestServer()
119+
server.start()
120+
121+
time.sleep(1) # Give it a moment to start up
122+
123+
yield server
124+
125+
62126
@pytest.fixture(scope="session")
63127
def sam_stack():
64128
"""
65129
Create and deploy the SAM stack once for all tests
66130
"""
131+
# Create the SAM stack
67132
app = App()
68133
stack = DummyLambdaStack(app, "DummyLambdaStack", env={"region": SAM_REGION})
69134

@@ -92,6 +157,7 @@ def sam_stack():
92157
# Wait for SAM to be ready
93158
wait_for_sam()
94159
yield stack
160+
95161
finally:
96162
process.terminate()
97163
process.wait(timeout=5) # Give it time to shut down gracefully
@@ -115,13 +181,25 @@ def lambda_client():
115181
)
116182

117183

118-
def test_basic(lambda_client, sam_stack):
119-
region = boto3.Session().region_name
120-
print("Region: ", region)
184+
def test_basic(lambda_client, sam_stack, sentry_test_server):
185+
sentry_test_server.clear_envelopes()
186+
187+
response = lambda_client.invoke(
188+
FunctionName="BasicTestFunction", Payload=json.dumps({"name": "Anton"})
189+
)
190+
result = json.loads(response["Payload"].read().decode())
191+
192+
print(sentry_test_server.envelopes)
193+
assert result == {"message": "Hello, Anton!"}
194+
195+
196+
def test_basic_2(lambda_client, sam_stack, sentry_test_server):
197+
sentry_test_server.clear_envelopes()
121198

122199
response = lambda_client.invoke(
123200
FunctionName="BasicTestFunction", Payload=json.dumps({"name": "Anton"})
124201
)
125202
result = json.loads(response["Payload"].read().decode())
126203

204+
print(sentry_test_server.envelopes)
127205
assert result == {"message": "Hello, Anton!"}

0 commit comments

Comments
 (0)