Skip to content

Commit c833e33

Browse files
committed
omit creating cold start on LMI
1 parent 0287c5d commit c833e33

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

datadog_lambda/cold_start.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import time
23
from typing import List, Hashable
34
import logging
@@ -63,6 +64,21 @@ def get_proactive_init_tag():
6364
)
6465

6566

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

146162

147163
def initialize_cold_start_tracing():
164+
# Skip cold start tracing initialization in managed instances mode
165+
if is_managed_instances_mode():
166+
return
167+
148168
if is_new_sandbox() and config.cold_start_tracing:
149169
from sys import meta_path
150170

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)