8
8
_cold_start = True
9
9
_proactive_initialization = False
10
10
_lambda_container_initialized = False
11
+ _tracer = None
11
12
12
13
13
14
def set_cold_start (init_timestamp_ns ):
@@ -18,6 +19,7 @@ def set_cold_start(init_timestamp_ns):
18
19
global _cold_start
19
20
global _lambda_container_initialized
20
21
global _proactive_initialization
22
+ global _tracer
21
23
if not _lambda_container_initialized :
22
24
now = time .time_ns ()
23
25
if (now - init_timestamp_ns ) // 1_000_000_000 > 10 :
@@ -29,6 +31,7 @@ def set_cold_start(init_timestamp_ns):
29
31
_cold_start = False
30
32
_proactive_initialization = False
31
33
_lambda_container_initialized = True
34
+ from ddtrace import tracer as _tracer
32
35
33
36
34
37
def is_cold_start ():
@@ -62,6 +65,9 @@ def __init__(self, module_name, full_file_path, start_time_ns, end_time_ns=None)
62
65
self .start_time_ns = start_time_ns
63
66
self .end_time_ns = end_time_ns
64
67
self .children = []
68
+ self .context = None
69
+ if _lambda_container_initialized :
70
+ self .context = _tracer .context_provider .active ()
65
71
66
72
67
73
root_nodes : List [ImportNode ] = []
@@ -70,10 +76,8 @@ def __init__(self, module_name, full_file_path, start_time_ns, end_time_ns=None)
70
76
71
77
72
78
def reset_node_stacks ():
73
- global root_nodes
74
- root_nodes = []
75
- global import_stack
76
- import_stack = []
79
+ root_nodes .clear ()
80
+ import_stack .clear ()
77
81
78
82
79
83
def push_node (module_name , file_path ):
@@ -183,7 +187,8 @@ def trace(self, root_nodes: List[ImportNode] = root_nodes):
183
187
cold_start_span = self .create_cold_start_span (cold_start_span_start_time_ns )
184
188
while root_nodes :
185
189
root_node = root_nodes .pop ()
186
- self .trace_tree (root_node , cold_start_span )
190
+ parent = root_node .context or cold_start_span
191
+ self .trace_tree (root_node , parent )
187
192
self .finish_span (cold_start_span , cold_start_span_end_time_ns )
188
193
189
194
def trace_tree (self , import_node : ImportNode , parent_span ):
0 commit comments