Lorenze/traces mark as failed (#3586)
Some checks failed
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Notify Downstream / notify-downstream (push) Has been cancelled
Update Test Durations / update-durations (3.10) (push) Has been cancelled
Update Test Durations / update-durations (3.11) (push) Has been cancelled
Update Test Durations / update-durations (3.12) (push) Has been cancelled
Update Test Durations / update-durations (3.13) (push) Has been cancelled
Mark stale issues and pull requests / stale (push) Has been cancelled

* marking trace batch as failed if its failed

* fix test
This commit is contained in:
Lorenze Jay
2025-09-23 22:02:27 -07:00
committed by GitHub
parent 1dbe8aab52
commit a0b757a12c
4 changed files with 367 additions and 5 deletions

View File

@@ -80,10 +80,6 @@ class TestTraceListenerSetup:
patch("requests.get") as mock_get,
patch("requests.put") as mock_put,
patch("requests.delete") as mock_delete,
patch.object(TraceBatchManager, "initialize_batch", return_value=None),
patch.object(
TraceBatchManager, "_finalize_backend_batch", return_value=True
),
patch.object(TraceBatchManager, "_cleanup_batch_data", return_value=True),
):
mock_response = MagicMock()
@@ -100,11 +96,15 @@ class TestTraceListenerSetup:
mock_put.return_value = mock_response
mock_delete.return_value = mock_response
mock_mark_failed = MagicMock()
mock_mark_failed.return_value = mock_response
yield {
"post": mock_post,
"get": mock_get,
"put": mock_put,
"delete": mock_delete,
"mark_trace_batch_as_failed": mock_mark_failed,
}
@pytest.mark.vcr(filter_headers=["authorization"])
@@ -657,3 +657,51 @@ class TestTraceListenerSetup:
handler.handle_execution_completion()
mock_mark_completed.assert_called_once()
@pytest.mark.vcr(filter_headers=["authorization"])
def test_trace_batch_marked_as_failed_on_finalize_error(self, mock_plus_api_calls):
"""Test that trace batch is marked as failed when finalization returns non-200 status"""
with patch.dict(os.environ, {"CREWAI_TRACING_ENABLED": "true"}):
agent = Agent(
role="Test Agent",
goal="Test goal",
backstory="Test backstory",
llm="gpt-4o-mini",
)
task = Task(
description="Say hello to the world",
expected_output="hello world",
agent=agent,
)
crew = Crew(agents=[agent], tasks=[task], verbose=True)
trace_listener = TraceCollectionListener()
from crewai.events.event_bus import crewai_event_bus
trace_listener.setup_listeners(crewai_event_bus)
with (
patch.object(
trace_listener.batch_manager.plus_api,
"send_trace_events",
return_value=MagicMock(status_code=200),
),
patch.object(
trace_listener.batch_manager.plus_api,
"finalize_trace_batch",
return_value=MagicMock(
status_code=500, text="Internal Server Error"
),
),
patch.object(
trace_listener.batch_manager.plus_api,
"mark_trace_batch_as_failed",
wraps=mock_plus_api_calls["mark_trace_batch_as_failed"],
) as mock_mark_failed,
):
crew.kickoff()
mock_mark_failed.assert_called_once()
call_args = mock_mark_failed.call_args_list[0]
assert call_args[0][1] == "Error sending events to backend"