@@ -53,37 +53,45 @@ class _LambdaDecorator(object):
53
53
and extracts/injects trace context.
54
54
"""
55
55
56
- _force_new = False
56
+ _force_wrap = False
57
57
58
58
def __new__ (cls , func ):
59
59
"""
60
60
If the decorator is accidentally applied to the same function multiple times,
61
- only the first one takes effect .
61
+ wrap only once .
62
62
63
- If _force_new , always return a real decorator, useful for unit tests.
63
+ If _force_wrap , always return a real decorator, useful for unit tests.
64
64
"""
65
- if cls ._force_new or not getattr (func , "_dd_wrapped" , False ):
66
- wrapped = super (_LambdaDecorator , cls ).__new__ (cls )
67
- wrapped ._dd_wrapped = True
68
- return wrapped
69
- else :
70
- return _NoopDecorator (func )
65
+ try :
66
+ if cls ._force_wrap or not isinstance (func , _LambdaDecorator ):
67
+ wrapped = super (_LambdaDecorator , cls ).__new__ (cls )
68
+ logger .debug ("datadog_lambda_wrapper wrapped" )
69
+ return wrapped
70
+ else :
71
+ logger .debug ("datadog_lambda_wrapper already wrapped" )
72
+ return _NoopDecorator (func )
73
+ except Exception :
74
+ traceback .print_exc ()
75
+ return func
71
76
72
77
def __init__ (self , func ):
73
78
"""Executes when the wrapped function gets wrapped"""
74
- self .func = func
75
- self .flush_to_log = os .environ .get ("DD_FLUSH_TO_LOG" , "" ).lower () == "true"
76
- self .logs_injection = (
77
- os .environ .get ("DD_LOGS_INJECTION" , "true" ).lower () == "true"
78
- )
79
-
80
- # Inject trace correlation ids to logs
81
- if self .logs_injection :
82
- inject_correlation_ids ()
83
-
84
- # Patch HTTP clients to propagate Datadog trace context
85
- patch_all ()
86
- logger .debug ("datadog_lambda_wrapper initialized" )
79
+ try :
80
+ self .func = func
81
+ self .flush_to_log = os .environ .get ("DD_FLUSH_TO_LOG" , "" ).lower () == "true"
82
+ self .logs_injection = (
83
+ os .environ .get ("DD_LOGS_INJECTION" , "true" ).lower () == "true"
84
+ )
85
+
86
+ # Inject trace correlation ids to logs
87
+ if self .logs_injection :
88
+ inject_correlation_ids ()
89
+
90
+ # Patch HTTP clients to propagate Datadog trace context
91
+ patch_all ()
92
+ logger .debug ("datadog_lambda_wrapper initialized" )
93
+ except Exception :
94
+ traceback .print_exc ()
87
95
88
96
def __call__ (self , event , context , ** kwargs ):
89
97
"""Executes when the wrapped function gets called"""
@@ -97,21 +105,23 @@ def __call__(self, event, context, **kwargs):
97
105
self ._after (event , context )
98
106
99
107
def _before (self , event , context ):
100
- set_cold_start ()
101
108
try :
109
+ set_cold_start ()
102
110
submit_invocations_metric (context )
103
111
# Extract Datadog trace context from incoming requests
104
112
extract_dd_trace_context (event )
105
113
106
114
# Set log correlation ids using extracted trace context
107
115
set_correlation_ids ()
116
+ logger .debug ("datadog_lambda_wrapper _before() done" )
108
117
except Exception :
109
118
traceback .print_exc ()
110
119
111
120
def _after (self , event , context ):
112
121
try :
113
122
if not self .flush_to_log :
114
123
lambda_stats .flush (float ("inf" ))
124
+ logger .debug ("datadog_lambda_wrapper _after() done" )
115
125
except Exception :
116
126
traceback .print_exc ()
117
127
0 commit comments