-
Notifications
You must be signed in to change notification settings - Fork 45
Properly parent lazily loaded module imports. #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
_cold_start = True | ||
_proactive_initialization = False | ||
_lambda_container_initialized = False | ||
_tracer = None | ||
|
||
|
||
def set_cold_start(init_timestamp_ns): | ||
|
@@ -18,6 +19,7 @@ def set_cold_start(init_timestamp_ns): | |
global _cold_start | ||
global _lambda_container_initialized | ||
global _proactive_initialization | ||
global _tracer | ||
if not _lambda_container_initialized: | ||
now = time.time_ns() | ||
if (now - init_timestamp_ns) // 1_000_000_000 > 10: | ||
|
@@ -29,6 +31,7 @@ def set_cold_start(init_timestamp_ns): | |
_cold_start = False | ||
_proactive_initialization = False | ||
_lambda_container_initialized = True | ||
from ddtrace import tracer as _tracer | ||
|
||
|
||
def is_cold_start(): | ||
|
@@ -62,6 +65,9 @@ def __init__(self, module_name, full_file_path, start_time_ns, end_time_ns=None) | |
self.start_time_ns = start_time_ns | ||
self.end_time_ns = end_time_ns | ||
self.children = [] | ||
self.context = None | ||
if _lambda_container_initialized: | ||
self.context = _tracer.context_provider.active() | ||
|
||
|
||
root_nodes: List[ImportNode] = [] | ||
|
@@ -70,10 +76,8 @@ def __init__(self, module_name, full_file_path, start_time_ns, end_time_ns=None) | |
|
||
|
||
def reset_node_stacks(): | ||
global root_nodes | ||
root_nodes = [] | ||
global import_stack | ||
import_stack = [] | ||
root_nodes.clear() | ||
Comment on lines
78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we removing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. Replacing the lists with new ones was causing my test to fail because somewhere we're holding onto an old reference to these objects. The correct solution is to just remove all the stuff from inside the list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yet, this would create an O(n) complexity instead of O(1) of reassigning right? Although I guess we'd have the remaining O(n) space in the wild while reassigning. I tried researching which was the best solution for this, but theres like 5 ways of doing it. So I'll take this one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method does not do an allocation where as the previous did. |
||
import_stack.clear() | ||
|
||
|
||
def push_node(module_name, file_path): | ||
|
@@ -183,7 +187,8 @@ def trace(self, root_nodes: List[ImportNode] = root_nodes): | |
cold_start_span = self.create_cold_start_span(cold_start_span_start_time_ns) | ||
while root_nodes: | ||
root_node = root_nodes.pop() | ||
self.trace_tree(root_node, cold_start_span) | ||
parent = root_node.context or cold_start_span | ||
self.trace_tree(root_node, parent) | ||
self.finish_span(cold_start_span, cold_start_span_end_time_ns) | ||
|
||
def trace_tree(self, import_node: ImportNode, parent_span): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't know that you could just assign a value by doing an
as
wow