Skip to content

Commit 88bf2f2

Browse files
authored
fix: omit creating cold start on LMI (#716)
* omit creating cold start on LMI * set env in config instead of using os to do it
1 parent aa9cbba commit 88bf2f2

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

datadog_lambda/cold_start.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ def get_proactive_init_tag():
6363
)
6464

6565

66+
def is_managed_instances_mode():
67+
"""
68+
Checks if the Lambda function is running in managed instances mode.
69+
In managed instances mode, we should not create cold start tracing spans
70+
as the gap between the sandbox initialization and the first
71+
invocation might not be be a great experience.
72+
73+
Returns:
74+
bool: True if running in managed instances mode, False otherwise
75+
"""
76+
return config.aws_lambda_initialization_type == "lambda-managed-instances"
77+
78+
6679
class ImportNode(object):
6780
def __init__(self, module_name, full_file_path, start_time_ns, end_time_ns=None):
6881
self.module_name = module_name
@@ -145,6 +158,10 @@ def wrapped_find_spec(*args, **kwargs):
145158

146159

147160
def initialize_cold_start_tracing():
161+
# Skip cold start tracing initialization in managed instances mode
162+
if is_managed_instances_mode():
163+
return
164+
148165
if is_new_sandbox() and config.cold_start_tracing:
149166
from sys import meta_path
150167

datadog_lambda/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def _resolve_env(self, key, default=None, cast=None, depends_on_tracing=False):
9898
integration_test = _get_env("DD_INTEGRATION_TEST", "false", as_bool)
9999

100100
aws_lambda_function_name = _get_env("AWS_LAMBDA_FUNCTION_NAME")
101+
aws_lambda_initialization_type = _get_env("AWS_LAMBDA_INITIALIZATION_TYPE")
101102

102103
@property
103104
def function_name(self):

datadog_lambda/wrapper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
is_cold_start,
1717
is_proactive_init,
1818
is_new_sandbox,
19+
is_managed_instances_mode,
1920
ColdStartTracer,
2021
)
2122
from datadog_lambda.config import config
@@ -338,7 +339,13 @@ def _after(self, event, context):
338339
create_dd_dummy_metadata_subsegment(
339340
self.trigger_tags, XraySubsegment.LAMBDA_FUNCTION_TAGS_KEY
340341
)
341-
should_trace_cold_start = config.cold_start_tracing and is_new_sandbox()
342+
# Skip creating cold start spans in managed instances mode
343+
# In managed instances, the tracer library handles cold start independently
344+
should_trace_cold_start = (
345+
config.cold_start_tracing
346+
and is_new_sandbox()
347+
and not is_managed_instances_mode()
348+
)
342349
if should_trace_cold_start:
343350
trace_ctx = tracer.current_trace_context()
344351

tests/test_cold_start.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,52 @@
1212

1313

1414
class TestColdStartTracingSetup(unittest.TestCase):
15+
def test_is_managed_instances_mode_when_set(self):
16+
os.environ["AWS_LAMBDA_INITIALIZATION_TYPE"] = "lambda-managed-instances"
17+
self.assertTrue(cold_start.is_managed_instances_mode())
18+
# Clean up
19+
if "AWS_LAMBDA_INITIALIZATION_TYPE" in os.environ:
20+
del os.environ["AWS_LAMBDA_INITIALIZATION_TYPE"]
21+
22+
def test_is_not_managed_instances_mode_when_not_set(self):
23+
if "AWS_LAMBDA_INITIALIZATION_TYPE" in os.environ:
24+
del os.environ["AWS_LAMBDA_INITIALIZATION_TYPE"]
25+
self.assertFalse(cold_start.is_managed_instances_mode())
26+
27+
def test_is_not_managed_instances_mode_with_different_value(self):
28+
os.environ["AWS_LAMBDA_INITIALIZATION_TYPE"] = "on-demand"
29+
self.assertFalse(cold_start.is_managed_instances_mode())
30+
# Clean up
31+
if "AWS_LAMBDA_INITIALIZATION_TYPE" in os.environ:
32+
del os.environ["AWS_LAMBDA_INITIALIZATION_TYPE"]
33+
34+
def test_initialize_cold_start_tracing_skips_in_managed_instances(self):
35+
# Set managed instances mode
36+
os.environ["AWS_LAMBDA_INITIALIZATION_TYPE"] = "lambda-managed-instances"
37+
os.environ["DD_COLD_START_TRACING"] = "true"
38+
cold_start._cold_start = True
39+
cold_start._lambda_container_initialized = False
40+
41+
# Reset node stacks and wrapped loaders to get clean state
42+
cold_start.reset_node_stacks()
43+
cold_start.already_wrapped_loaders.clear()
44+
45+
# Count wrapped loaders before
46+
wrapped_loaders_before = len(cold_start.already_wrapped_loaders)
47+
48+
# Initialize cold start tracing - should skip in managed instances mode
49+
cold_start.initialize_cold_start_tracing()
50+
51+
# Verify no loaders were wrapped
52+
wrapped_loaders_after = len(cold_start.already_wrapped_loaders)
53+
self.assertEqual(wrapped_loaders_before, wrapped_loaders_after)
54+
55+
# Clean up
56+
if "AWS_LAMBDA_INITIALIZATION_TYPE" in os.environ:
57+
del os.environ["AWS_LAMBDA_INITIALIZATION_TYPE"]
58+
if "DD_COLD_START_TRACING" in os.environ:
59+
del os.environ["DD_COLD_START_TRACING"]
60+
1561
def test_proactive_init(self):
1662
cold_start._cold_start = True
1763
cold_start._proactive_initialization = False

0 commit comments

Comments
 (0)