fix: mark batch as failed when event send fails in first-time handler

The return value of _send_events_to_backend() was discarded in
  _initialize_backend_and_send_events, so _finalize_backend_batch was
  called unconditionally with the full event count even when the send
  returned 500. This finalized the batch as "completed" on the server
  while it received 0 events, producing an empty trace URL.

  Now check the return status and call mark_trace_batch_as_failed on
  500, matching the behavior of the regular finalize_batch path.
This commit is contained in:
Tiago Freire
2026-03-18 20:35:57 -03:00
parent 373913e5d0
commit 09ed63b8cb

View File

@@ -111,7 +111,13 @@ class FirstTimeTraceHandler:
events_count = len(self.batch_manager.event_buffer)
if self.batch_manager.event_buffer:
self.batch_manager._send_events_to_backend()
send_status = self.batch_manager._send_events_to_backend()
if send_status == 500 and self.batch_manager.trace_batch_id:
self.batch_manager.plus_api.mark_trace_batch_as_failed(
self.batch_manager.trace_batch_id,
"Error sending events to backend",
)
return
self.batch_manager._finalize_backend_batch(events_count)
self.ephemeral_url = self.batch_manager.ephemeral_trace_url