@@ -105,7 +105,7 @@ def __init__(
105
105
self .base_msg ,
106
106
)
107
107
self .span_ids_to_send : Set [str ] = set ()
108
- self .spans : List [ Dict ] = []
108
+ self .spans : Dict [ str , Dict ] = {}
109
109
if is_new_invocation :
110
110
SpansContainer .is_cold = False
111
111
@@ -127,7 +127,7 @@ def start(self, event=None, context=None):
127
127
128
128
def handle_timeout (self , * args ):
129
129
get_logger ().info ("The tracer reached the end of the timeout timer" )
130
- to_send = [s for s in self .spans if s [ "id" ] in self .span_ids_to_send ]
130
+ to_send = [self .spans [ span_id ] for span_id in self .span_ids_to_send ]
131
131
self .span_ids_to_send .clear ()
132
132
if Configuration .send_only_if_error :
133
133
to_send .append (self ._generate_start_span ())
@@ -150,44 +150,48 @@ def add_span(self, span: dict) -> dict:
150
150
This function parses an request event and add it to the span.
151
151
"""
152
152
new_span = recursive_json_join (span , self .base_msg )
153
- self .spans .append (new_span )
154
- self .span_ids_to_send .add (span ["id" ])
153
+ span_id = new_span ["id" ]
154
+ self .spans [span_id ] = new_span
155
+ self .span_ids_to_send .add (span_id )
155
156
return new_span
156
157
157
- def get_last_span (self ) -> Optional [dict ]:
158
- if not self . spans :
158
+ def get_span_by_id (self , span_id : Optional [ str ] ) -> Optional [dict ]:
159
+ if not span_id :
159
160
return None
160
- return self .spans [ - 1 ]
161
+ return self .spans . get ( span_id )
161
162
162
- def get_span_by_id (self , span_id : str ) -> Optional [dict ]:
163
- for span in self .spans :
164
- if span .get ("id" ) == span_id :
165
- return span
166
- return None
167
-
168
- def pop_last_span (self ) -> Optional [dict ]:
169
- return self .spans .pop () if self .spans else None
163
+ def pop_span (self , span_id : Optional [str ]) -> Optional [dict ]:
164
+ if not span_id :
165
+ return None
166
+ self .span_ids_to_send .discard (span_id )
167
+ return self .spans .pop (span_id , None )
170
168
171
- def update_event_end_time (self ) -> None :
169
+ def update_event_end_time (self , span_id : str ) -> None :
172
170
"""
173
171
This function assumes synchronous execution - we update the last http event.
174
172
"""
175
- if self .spans :
176
- span = self .spans [- 1 ]
177
- span ["ended" ] = get_current_ms_time ()
178
- self .span_ids_to_send .add (span ["id" ])
173
+ if span_id in self .spans :
174
+ self .spans [span_id ]["ended" ] = get_current_ms_time ()
175
+ self .span_ids_to_send .add (span_id )
176
+ else :
177
+ get_logger ().warning (f"update_event_end_time: Got unknown span id: { span_id } " )
179
178
180
179
def update_event_times (
181
- self , start_time : Optional [datetime ] = None , end_time : Optional [datetime ] = None
180
+ self ,
181
+ span_id : str ,
182
+ start_time : Optional [datetime ] = None ,
183
+ end_time : Optional [datetime ] = None ,
182
184
) -> None :
183
185
"""
184
186
This function assumes synchronous execution - we update the last http event.
185
187
"""
186
- if self .spans :
188
+ if span_id in self .spans :
187
189
start_timestamp = start_time .timestamp () if start_time else time .time ()
188
190
end_timestamp = end_time .timestamp () if end_time else time .time ()
189
- self .spans [- 1 ]["started" ] = int (start_timestamp * 1000 )
190
- self .spans [- 1 ]["ended" ] = int (end_timestamp * 1000 )
191
+ self .spans [span_id ]["started" ] = int (start_timestamp * 1000 )
192
+ self .spans [span_id ]["ended" ] = int (end_timestamp * 1000 )
193
+ else :
194
+ get_logger ().warning (f"update_event_times: Got unknown span id: { span_id } " )
191
195
192
196
@staticmethod
193
197
def _create_exception_event (
@@ -223,8 +227,9 @@ def add_exception_event(
223
227
def add_step_end_event (self , ret_val ):
224
228
message_id = str (uuid .uuid4 ())
225
229
step_function_span = create_step_function_span (message_id )
226
- self .spans .append (recursive_json_join (step_function_span , self .base_msg ))
227
- self .span_ids_to_send .add (step_function_span ["id" ])
230
+ span_id = step_function_span ["id" ]
231
+ self .spans [span_id ] = recursive_json_join (step_function_span , self .base_msg )
232
+ self .span_ids_to_send .add (span_id )
228
233
if isinstance (ret_val , dict ):
229
234
ret_val [LUMIGO_EVENT_KEY ] = {STEP_FUNCTION_UID_KEY : message_id }
230
235
get_logger ().debug (f"Added key { LUMIGO_EVENT_KEY } to the user's return value" )
@@ -259,12 +264,12 @@ def end(self, ret_val=None, event: Optional[dict] = None, context=None) -> Optio
259
264
if _is_span_has_error (self .function_span ):
260
265
self ._set_error_extra_data (event )
261
266
spans_contain_errors : bool = any (
262
- _is_span_has_error (s ) for s in self .spans + [ self . function_span ]
263
- )
267
+ _is_span_has_error (s ) for s in self .spans . values ()
268
+ ) or _is_span_has_error ( self . function_span )
264
269
265
270
if (not Configuration .send_only_if_error ) or spans_contain_errors :
266
271
to_send = [self .function_span ] + [
267
- s for s in self .spans if s [ "id" ] in self .span_ids_to_send
272
+ span for span_id , span in self .spans . items () if span_id in self .span_ids_to_send
268
273
]
269
274
reported_rtt = lumigo_utils .report_json (region = self .region , msgs = to_send )
270
275
else :
0 commit comments