1717from scripts .build_aws_lambda_layer import build_packaged_zip , DIST_PATH
1818
1919
20- PYTHON_VERSION = f"python{ sys .version_info .major } .{ sys .version_info .minor } "
21- SAM_PORT = 3001
22- LAMBDA_FUNCTION_TIMEOUT = 10
2320LAMBDA_FUNCTION_DIR = "./tests/integrations/aws_lambda/lambda_functions/"
21+ LAMBDA_FUNCTION_TIMEOUT = 10
22+ SAM_PORT = 3001
23+
24+ PYTHON_VERSION = f"python{ sys .version_info .major } .{ sys .version_info .minor } "
2425
2526
26- class DummyLambdaStack (Stack ):
27+ class LocalLambdaStack (Stack ):
2728 def __init__ (self , scope : Construct , construct_id : str , ** kwargs ) -> None :
2829 super ().__init__ (scope , construct_id , ** kwargs )
29-
30- print (f"CREATING STACK: { self } " )
30+ print ("[LocalLambdaStack] Creating local SAM Lambda Stack: %s" % self )
3131
3232 # Override the template synthesis
3333 self .template_options .template_format_version = "2010-09-09"
3434 self .template_options .transforms = ["AWS::Serverless-2016-10-31" ]
3535
36- print ("- Create Sentry Lambda layer package" )
36+ print ("[LocalLambdaStack] Create Sentry Lambda layer package" )
3737 filename = "sentry-sdk-lambda-layer.zip"
3838 build_packaged_zip (
3939 make_dist = True ,
4040 out_zip_filename = filename ,
4141 )
4242
43- print ("- Add Sentry Lambda layer containing the Sentry SDK to the SAM stack" )
43+ print (
44+ "[LocalLambdaStack] Add Sentry Lambda layer containing the Sentry SDK to the SAM stack"
45+ )
4446 self .sentry_layer = CfnResource (
4547 self ,
4648 "SentryPythonServerlessSDK" ,
@@ -53,9 +55,12 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
5355 },
5456 )
5557
56- print ("- Add all Lambda functions defined in /tests/integrations/aws_lambda/lambda_functions/ to the SAM stack" )
58+ print (
59+ "[LocalLambdaStack] Add all Lambda functions defined in /tests/integrations/aws_lambda/lambda_functions/ to the SAM stack"
60+ )
5761 lambda_dirs = [
58- d for d in os .listdir (LAMBDA_FUNCTION_DIR )
62+ d
63+ for d in os .listdir (LAMBDA_FUNCTION_DIR )
5964 if os .path .isdir (os .path .join (LAMBDA_FUNCTION_DIR , d ))
6065 ]
6166 for lambda_dir in lambda_dirs :
@@ -68,7 +73,9 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
6873 "Handler" : "sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler" ,
6974 "Runtime" : PYTHON_VERSION ,
7075 "Timeout" : LAMBDA_FUNCTION_TIMEOUT ,
71- "Layers" : [{"Ref" : self .sentry_layer .logical_id }], # Add layer containing the Sentry SDK to function.
76+ "Layers" : [
77+ {"Ref" : self .sentry_layer .logical_id }
78+ ], # Add layer containing the Sentry SDK to function.
7279 "Environment" : {
7380 "Variables" : {
7481 "SENTRY_DSN" :
"http://[email protected] :9999/0" ,
@@ -78,8 +85,13 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
7885 },
7986 },
8087 )
81- print (f" - Created Lambda function: { lambda_dir } ({ os .path .join (LAMBDA_FUNCTION_DIR , lambda_dir )} )" )
82-
88+ print (
89+ "[LocalLambdaStack] - Created Lambda function: %s (%s)"
90+ % (
91+ lambda_dir ,
92+ os .path .join (LAMBDA_FUNCTION_DIR , lambda_dir ),
93+ )
94+ )
8395
8496 @classmethod
8597 def wait_for_stack (cls , timeout = 30 , port = SAM_PORT ):
@@ -90,7 +102,8 @@ def wait_for_stack(cls, timeout=30, port=SAM_PORT):
90102 while True :
91103 if time .time () - start_time > timeout :
92104 raise TimeoutError (
93- "SAM failed to start within {} seconds. (Maybe Docker is not running?)" .format (timeout )
105+ "SAM failed to start within %s seconds. (Maybe Docker is not running?)"
106+ % timeout
94107 )
95108
96109 try :
@@ -104,18 +117,24 @@ def wait_for_stack(cls, timeout=30, port=SAM_PORT):
104117 continue
105118
106119
107- class SentryTestServer :
108- def __init__ (self , port = 9999 ):
120+ class SentryServerForTesting :
121+ """
122+ A simple Sentry.io style server that accepts envelopes and stores them in a list.
123+ """
124+
125+ def __init__ (self , port = 9999 , log_level = "warning" ):
109126 self .envelopes = []
110127 self .port = port
128+ self .log_level = log_level
111129 self .app = FastAPI ()
112130
113131 @self .app .post ("/api/0/envelope/" )
114132 async def envelope (request : Request ):
133+ print ("[SentryServerForTesting] Received envelope" )
115134 try :
116135 raw_body = await request .body ()
117136 except :
118- return {"status" : "no body" }
137+ return {"status" : "no body received " }
119138
120139 try :
121140 body = gzip .decompress (raw_body ).decode ("utf-8" )
@@ -143,13 +162,13 @@ async def envelope(request: Request):
143162 return {"status" : "ok" }
144163
145164 def run_server (self ):
146- uvicorn .run (self .app , host = "0.0.0.0" , port = self .port , log_level = "warning" )
165+ uvicorn .run (self .app , host = "0.0.0.0" , port = self .port , log_level = self . log_level )
147166
148167 def start (self ):
149- print ("[SENTRY SERVER ] Starting server" )
168+ print ("[SentryServerForTesting ] Starting server" )
150169 server_thread = threading .Thread (target = self .run_server , daemon = True )
151170 server_thread .start ()
152171
153172 def clear_envelopes (self ):
154- print ("[SENTRY SERVER ] Clearing envelopes" )
173+ print ("[SentryServerForTesting ] Clearing envelopes" )
155174 self .envelopes = []
0 commit comments