4
4
import os
5
5
import random
6
6
import sys
7
- from typing import Any , Callable , Dict , Union
7
+ from typing import Any , Callable , Dict , Optional , Union
8
+
9
+ import jmespath
8
10
9
11
from ..shared import constants
10
12
from ..shared .functions import resolve_env_var_choice , resolve_truthy_env_var_choice
@@ -36,6 +38,27 @@ def _is_cold_start() -> bool:
36
38
return cold_start
37
39
38
40
41
+ def _get_correlation_id_path (path : Union [str , int ]) -> Optional [str ]:
42
+ """
43
+
44
+ Parameters
45
+ ----------
46
+ path :
47
+ JMESPath
48
+ Returns
49
+ -------
50
+ str, optional
51
+ Returns the JMESPath
52
+ """
53
+ if path is None :
54
+ return None
55
+
56
+ # NOTE: We could have a enum of standard event types
57
+ # like "requestContext.requestId" or API Gateway Proxy Events
58
+
59
+ return path
60
+
61
+
39
62
# PyCharm does not support autocomplete via getattr
40
63
# so we need to return to subclassing removed in #97
41
64
# All methods/properties continue to be proxied to inner logger
@@ -204,7 +227,12 @@ def _configure_sampling(self):
204
227
f"Please review POWERTOOLS_LOGGER_SAMPLE_RATE environment variable."
205
228
)
206
229
207
- def inject_lambda_context (self , lambda_handler : Callable [[Dict , Any ], Any ] = None , log_event : bool = None ):
230
+ def inject_lambda_context (
231
+ self ,
232
+ lambda_handler : Callable [[Dict , Any ], Any ] = None ,
233
+ log_event : bool = None ,
234
+ correlation_id_path : Union [str , int ] = None ,
235
+ ):
208
236
"""Decorator to capture Lambda contextual info and inject into logger
209
237
210
238
Parameters
@@ -213,6 +241,8 @@ def inject_lambda_context(self, lambda_handler: Callable[[Dict, Any], Any] = Non
213
241
Method to inject the lambda context
214
242
log_event : bool, optional
215
243
Instructs logger to log Lambda Event, by default False
244
+ correlation_id_path: str, optional
245
+ Can be a JMESPath or one of the standard ones
216
246
217
247
Environment variables
218
248
---------------------
@@ -251,18 +281,24 @@ def handler(event, context):
251
281
# Return a partial function with args filled
252
282
if lambda_handler is None :
253
283
logger .debug ("Decorator called with parameters" )
254
- return functools .partial (self .inject_lambda_context , log_event = log_event )
284
+ return functools .partial (
285
+ self .inject_lambda_context , log_event = log_event , correlation_id_path = correlation_id_path
286
+ )
255
287
256
288
log_event = resolve_truthy_env_var_choice (
257
289
choice = log_event , env = os .getenv (constants .LOGGER_LOG_EVENT_ENV , "false" )
258
290
)
291
+ correlation_id_path = _get_correlation_id_path (correlation_id_path )
259
292
260
293
@functools .wraps (lambda_handler )
261
294
def decorate (event , context ):
262
295
lambda_context = build_lambda_context_model (context )
263
296
cold_start = _is_cold_start ()
264
297
self .structure_logs (append = True , cold_start = cold_start , ** lambda_context .__dict__ )
265
298
299
+ if correlation_id_path :
300
+ self .structure_logs (append = True , correlation_id = jmespath .search (correlation_id_path , event ))
301
+
266
302
if log_event :
267
303
logger .debug ("Event received" )
268
304
self .info (event )
0 commit comments