Skip to content

Commit 6d411c8

Browse files
RD-4424 - cold start indication in pre-invocation calls (#177)
* cold start indication in pre-invocation calls
1 parent 826c688 commit 6d411c8

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

src/lumigo_tracer/spans_container.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __init__(
5555
trace_id_suffix: str = None,
5656
trigger_by: dict = None,
5757
max_finish_time: int = None,
58+
is_new_invocation: bool = False,
5859
event: str = None,
5960
envs: str = None,
6061
):
@@ -102,7 +103,8 @@ def __init__(
102103
)
103104
self.span_ids_to_send: Set[str] = set()
104105
self.spans: List[Dict] = []
105-
SpansContainer.is_cold = False
106+
if is_new_invocation:
107+
SpansContainer.is_cold = False
106108

107109
def _generate_start_span(self) -> dict:
108110
to_send = self.function_span.copy()
@@ -285,14 +287,14 @@ def get_span(cls) -> "SpansContainer":
285287
return cls.create_span()
286288

287289
@classmethod
288-
def create_span(cls, event=None, context=None, force=False) -> "SpansContainer":
290+
def create_span(cls, event=None, context=None, is_new_invocation=False) -> "SpansContainer":
289291
"""
290292
This function creates a span out of a given AWS context.
291293
The force flag delete any existing span-container (to handle with warm execution of lambdas).
292294
Note that if lambda will be executed directly (regular pythonic function call and not invoked),
293295
it will override the container.
294296
"""
295-
if cls._span and not force:
297+
if cls._span and not is_new_invocation:
296298
return cls._span
297299
# copy the event to ensure that we will not change it
298300
event = copy.deepcopy(event)
@@ -319,6 +321,7 @@ def create_span(cls, event=None, context=None, force=False) -> "SpansContainer":
319321
account=safe_split_get(getattr(context, "invoked_function_arn", ""), ":", 4, ""),
320322
trigger_by=parse_triggered_by(event),
321323
max_finish_time=get_current_ms_time() + remaining_time,
324+
is_new_invocation=is_new_invocation,
322325
**additional_info,
323326
)
324327
return cls._span

src/lumigo_tracer/tracer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def lambda_wrapper(*args, **kwargs):
5454
try:
5555
if Configuration.enhanced_print:
5656
_enhance_output(args, local_print, local_logging_format)
57-
SpansContainer.create_span(*args, force=True)
57+
SpansContainer.create_span(*args, is_new_invocation=True)
5858
with lumigo_safe_execute("auto tag"):
5959
AutoTagEvent.auto_tag_event(args[0])
6060
SpansContainer.get_span().start(*args)

src/test/unit/test_tracer.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,18 @@ def wrapped(event, context):
463463
from_lumigo = line_dropper.sub("-", stacktrace)
464464
original = line_dropper.sub("-", traceback.format_tb(e.value.__traceback__)[1])
465465
assert from_lumigo == original
466+
467+
468+
def test_cold_indicator_with_request_in_cold_phase(context):
469+
SpansContainer.is_cold = True
470+
# Create a request the might invert the `is_cold` field
471+
http.client.HTTPConnection("www.google.com").request("POST", "/")
472+
assert SpansContainer.is_cold is True
473+
474+
@lumigo_tracer(step_function=True)
475+
def lambda_test_function(event, context):
476+
http.client.HTTPConnection("www.google.com").request("POST", "/")
477+
478+
lambda_test_function({}, context)
479+
assert SpansContainer.get_span().function_span["readiness"] == "cold"
480+
assert SpansContainer.is_cold is False

0 commit comments

Comments
 (0)