Skip to content

Commit 85f3abe

Browse files
committed
Properly parent lazily loaded module imports.
1 parent 7e1fa70 commit 85f3abe

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

datadog_lambda/cold_start.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
_cold_start = True
99
_proactive_initialization = False
1010
_lambda_container_initialized = False
11+
_tracer = None
1112

1213

1314
def 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

3437
def 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

6773
root_nodes: List[ImportNode] = []
@@ -183,7 +189,8 @@ def trace(self, root_nodes: List[ImportNode] = root_nodes):
183189
cold_start_span = self.create_cold_start_span(cold_start_span_start_time_ns)
184190
while root_nodes:
185191
root_node = root_nodes.pop()
186-
self.trace_tree(root_node, cold_start_span)
192+
parent = root_node.context or cold_start_span
193+
self.trace_tree(root_node, parent)
187194
self.finish_span(cold_start_span, cold_start_span_end_time_ns)
188195

189196
def trace_tree(self, import_node: ImportNode, parent_span):

0 commit comments

Comments
 (0)