88_cold_start = True
99_proactive_initialization = False
1010_lambda_container_initialized = False
11+ _tracer = None
1112
1213
1314def set_cold_start (init_timestamp_ns ):
@@ -18,6 +19,7 @@ def set_cold_start(init_timestamp_ns):
1819 global _cold_start
1920 global _lambda_container_initialized
2021 global _proactive_initialization
22+ global _tracer
2123 if not _lambda_container_initialized :
2224 now = time .time_ns ()
2325 if (now - init_timestamp_ns ) // 1_000_000_000 > 10 :
@@ -29,6 +31,7 @@ def set_cold_start(init_timestamp_ns):
2931 _cold_start = False
3032 _proactive_initialization = False
3133 _lambda_container_initialized = True
34+ from ddtrace import tracer as _tracer
3235
3336
3437def is_cold_start ():
@@ -62,6 +65,9 @@ def __init__(self, module_name, full_file_path, start_time_ns, end_time_ns=None)
6265 self .start_time_ns = start_time_ns
6366 self .end_time_ns = end_time_ns
6467 self .children = []
68+ self .context = None
69+ if _lambda_container_initialized :
70+ self .context = _tracer .context_provider .active ()
6571
6672
6773root_nodes : List [ImportNode ] = []
@@ -70,10 +76,8 @@ def __init__(self, module_name, full_file_path, start_time_ns, end_time_ns=None)
7076
7177
7278def 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 ()
7781
7882
7983def push_node (module_name , file_path ):
@@ -183,7 +187,8 @@ def trace(self, root_nodes: List[ImportNode] = root_nodes):
183187 cold_start_span = self .create_cold_start_span (cold_start_span_start_time_ns )
184188 while root_nodes :
185189 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 )
187192 self .finish_span (cold_start_span , cold_start_span_end_time_ns )
188193
189194 def trace_tree (self , import_node : ImportNode , parent_span ):
0 commit comments