66import time
77import uuid
88import signal
9- from typing import List , Dict , Optional , Callable , Set
9+ from typing import List , Dict , Optional , Callable , Set , Union
1010
1111from lumigo_tracer .event .event_dumper import EventDumper
1212from lumigo_tracer .lumigo_utils import (
3636_VERSION_PATH = os .path .join (os .path .dirname (__file__ ), "VERSION" )
3737MAX_LAMBDA_TIME = 15 * 60 * 1000
3838FUNCTION_TYPE = "function"
39+ ENRICHMENT_TYPE = "enrichment"
3940MALFORMED_TXID = "000000000000000000000000"
4041
4142
@@ -103,11 +104,17 @@ def __init__(
103104 ** (trigger_by or {}),
104105 },
105106 "isMalformedTransactionId" : malformed_txid ,
106- EXECUTION_TAGS_KEY : [],
107107 MANUAL_TRACES_KEY : [],
108108 },
109109 self .base_msg ,
110110 )
111+ self .base_enrichment_span = {
112+ "type" : ENRICHMENT_TYPE ,
113+ "token" : Configuration .token ,
114+ "invocation_id" : request_id ,
115+ "transaction_id" : transaction_id ,
116+ }
117+ self .execution_tags : List [Dict [str , str ]] = []
111118 self .span_ids_to_send : Set [str ] = set ()
112119 self .spans : Dict [str , Dict ] = {}
113120 self .manual_trace_start_times : Dict [str , int ] = {}
@@ -121,6 +128,14 @@ def _generate_start_span(self) -> dict:
121128 to_send ["maxFinishTime" ] = self .max_finish_time
122129 return to_send
123130
131+ def generate_enrichment_span (self ) -> Optional [Dict [str , Union [str , int ]]]:
132+ if not self .execution_tags :
133+ return None
134+ return recursive_json_join (
135+ {"sending_time" : get_current_ms_time (), EXECUTION_TAGS_KEY : self .execution_tags .copy ()},
136+ self .base_enrichment_span ,
137+ )
138+
124139 def start (self , event = None , context = None ):
125140 to_send = self ._generate_start_span ()
126141 if not Configuration .send_only_if_error :
@@ -137,6 +152,9 @@ def handle_timeout(self, *args):
137152 get_logger ().info ("The tracer reached the end of the timeout timer" )
138153 spans_id_copy = self .span_ids_to_send .copy ()
139154 to_send = [self .spans [span_id ] for span_id in spans_id_copy ]
155+ enrichment_span = self .generate_enrichment_span ()
156+ if enrichment_span :
157+ to_send .insert (0 , enrichment_span )
140158 self .span_ids_to_send .clear ()
141159 if Configuration .send_only_if_error :
142160 to_send .append (self ._generate_start_span ())
@@ -244,10 +262,10 @@ def add_step_end_event(self, ret_val):
244262 get_logger ().debug (f"Added key { LUMIGO_EVENT_KEY } to the user's return value" )
245263
246264 def get_tags_len (self ) -> int :
247- return len (self .function_span [ EXECUTION_TAGS_KEY ] )
265+ return len (self .execution_tags )
248266
249267 def add_tag (self , key : str , value : str ) -> None :
250- self .function_span [ EXECUTION_TAGS_KEY ] .append ({"key" : key , "value" : value })
268+ self .execution_tags .append ({"key" : key , "value" : value })
251269
252270 def start_manual_trace (self , name : str ) -> None :
253271 now = get_current_ms_time ()
@@ -292,6 +310,9 @@ def end(self, ret_val=None, event: Optional[dict] = None, context=None) -> Optio
292310 to_send = [self .function_span ] + [
293311 span for span_id , span in self .spans .items () if span_id in self .span_ids_to_send
294312 ]
313+ enrichment_span = self .generate_enrichment_span ()
314+ if enrichment_span :
315+ to_send .append (enrichment_span )
295316 reported_rtt = lumigo_utils .report_json (region = self .region , msgs = to_send )
296317 else :
297318 get_logger ().debug (
0 commit comments