Skip to content

Commit c720feb

Browse files
authored
Add back span status (#5147)
### Description For some reason since Performance was implemented ages ago, we were not sending the `span.status` field at all. Add it back since we are moving to a Span-First world and Agent Monitoring also expects span statuses to be correct for some product features. Some other notes regarding the status mess: * the transaction status is added to the `trace_context` - this is fine * the span status is also added as a tag, this should be removed in a major since it's redundant * `set_http_status` further adds another tag called `http.status_code`, remove this in the major too #### Issues * resolves: #5059 * resolves: PY-1960
1 parent 7b7ea33 commit c720feb

File tree

11 files changed

+26
-1
lines changed

11 files changed

+26
-1
lines changed

sentry_sdk/tracing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ def set_http_status(self, http_status):
666666
# type: (int) -> None
667667
self.set_tag(
668668
"http.status_code", str(http_status)
669-
) # we keep this for backwards compatibility
669+
) # TODO-neel remove in major, we keep this for backwards compatibility
670670
self.set_data(SPANDATA.HTTP_STATUS_CODE, http_status)
671671
self.set_status(get_span_status_from_http_code(http_status))
672672

@@ -729,6 +729,8 @@ def to_json(self):
729729
} # type: Dict[str, Any]
730730

731731
if self.status:
732+
rv["status"] = self.status
733+
# TODO-neel remove redundant tag in major
732734
self._tags["status"] = self.status
733735

734736
if len(self._measurements) > 0:

tests/integrations/anthropic/test_anthropic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ def test_span_status_error(sentry_init, capture_events):
722722

723723
(error, transaction) = events
724724
assert error["level"] == "error"
725+
assert transaction["spans"][0]["status"] == "internal_error"
725726
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
726727
assert transaction["contexts"]["trace"]["status"] == "internal_error"
727728

@@ -745,6 +746,7 @@ async def test_span_status_error_async(sentry_init, capture_events):
745746

746747
(error, transaction) = events
747748
assert error["level"] == "error"
749+
assert transaction["spans"][0]["status"] == "internal_error"
748750
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
749751
assert transaction["contexts"]["trace"]["status"] == "internal_error"
750752

tests/integrations/cohere/test_cohere.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def test_span_status_error(sentry_init, capture_events):
181181

182182
(error, transaction) = events
183183
assert error["level"] == "error"
184+
assert transaction["spans"][0]["status"] == "internal_error"
184185
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
185186
assert transaction["contexts"]["trace"]["status"] == "internal_error"
186187

tests/integrations/huggingface_hub/test_huggingface_hub.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ def test_chat_completion_api_error(
792792
assert span["op"] == "gen_ai.chat"
793793
assert span["description"] == "chat test-model"
794794
assert span["origin"] == "auto.ai.huggingface_hub"
795+
assert span["status"] == "internal_error"
795796
assert span.get("tags", {}).get("status") == "internal_error"
796797

797798
assert (
@@ -835,6 +836,7 @@ def test_span_status_error(sentry_init, capture_events, mock_hf_api_with_errors)
835836
assert sp["op"] == "http.client"
836837

837838
assert span is not None
839+
assert span["status"] == "internal_error"
838840
assert span["tags"]["status"] == "internal_error"
839841

840842
assert transaction["contexts"]["trace"]["status"] == "internal_error"

tests/integrations/langchain/test_langchain.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ def test_span_status_error(sentry_init, capture_events):
347347

348348
(error, transaction) = events
349349
assert error["level"] == "error"
350+
assert transaction["spans"][0]["status"] == "internal_error"
350351
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
351352
assert transaction["contexts"]["trace"]["status"] == "internal_error"
352353

tests/integrations/langgraph/test_langgraph.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ def original_invoke(self, *args, **kwargs):
403403
assert len(invoke_spans) == 1
404404

405405
invoke_span = invoke_spans[0]
406+
assert invoke_span.get("status") == "internal_error"
406407
assert invoke_span.get("tags", {}).get("status") == "internal_error"
407408

408409

@@ -436,6 +437,7 @@ async def run_error_test():
436437
assert len(invoke_spans) == 1
437438

438439
invoke_span = invoke_spans[0]
440+
assert invoke_span.get("status") == "internal_error"
439441
assert invoke_span.get("tags", {}).get("status") == "internal_error"
440442

441443

tests/integrations/mcp/test_mcp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ def failing_tool(tool_name, arguments):
300300

301301
# Error flag should be set for tools
302302
assert span["data"][SPANDATA.MCP_TOOL_RESULT_IS_ERROR] is True
303+
assert span["status"] == "internal_error"
303304
assert span["tags"]["status"] == "internal_error"
304305

305306

tests/integrations/openai/test_openai.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ def test_span_status_error(sentry_init, capture_events):
440440

441441
(error, transaction) = events
442442
assert error["level"] == "error"
443+
assert transaction["spans"][0]["status"] == "internal_error"
443444
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
444445
assert transaction["contexts"]["trace"]["status"] == "internal_error"
445446

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ async def test_error_handling(sentry_init, capture_events, test_agent):
645645

646646
assert ai_client_span["description"] == "chat gpt-4"
647647
assert ai_client_span["origin"] == "auto.ai.openai_agents"
648+
assert ai_client_span["status"] == "internal_error"
648649
assert ai_client_span["tags"]["status"] == "internal_error"
649650

650651

@@ -685,6 +686,7 @@ async def test_error_captures_input_data(sentry_init, capture_events, test_agent
685686
ai_client_span = [s for s in spans if s["op"] == "gen_ai.chat"][0]
686687

687688
assert ai_client_span["description"] == "chat gpt-4"
689+
assert ai_client_span["status"] == "internal_error"
688690
assert ai_client_span["tags"]["status"] == "internal_error"
689691

690692
assert "gen_ai.request.messages" in ai_client_span["data"]
@@ -724,6 +726,7 @@ async def test_span_status_error(sentry_init, capture_events, test_agent):
724726

725727
(error, transaction) = events
726728
assert error["level"] == "error"
729+
assert transaction["spans"][0]["status"] == "internal_error"
727730
assert transaction["spans"][0]["tags"]["status"] == "internal_error"
728731
assert transaction["contexts"]["trace"]["status"] == "internal_error"
729732

@@ -827,6 +830,7 @@ async def test_mcp_tool_execution_spans(sentry_init, capture_events, test_agent)
827830
)
828831

829832
# Verify no error status since error was None
833+
assert mcp_tool_span.get("status") != "internal_error"
830834
assert mcp_tool_span.get("tags", {}).get("status") != "internal_error"
831835

832836

@@ -927,6 +931,7 @@ async def test_mcp_tool_execution_with_error(sentry_init, capture_events, test_a
927931
assert mcp_tool_span["data"]["gen_ai.tool.output"] is None
928932

929933
# Verify error status was set
934+
assert mcp_tool_span["status"] == "internal_error"
930935
assert mcp_tool_span["tags"]["status"] == "internal_error"
931936

932937

@@ -1218,4 +1223,5 @@ def failing_tool(message: str) -> str:
12181223

12191224
# Verify error status was set (this is the key test for our patch)
12201225
# The span should be marked as error because the tool execution failed
1226+
assert execute_tool_span["status"] == "internal_error"
12211227
assert execute_tool_span["tags"]["status"] == "internal_error"

tests/integrations/pymongo/test_pymongo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
9999
and "4" not in insert_fail["description"]
100100
)
101101

102+
assert find["status"] == "ok"
102103
assert find["tags"]["status"] == "ok"
104+
assert insert_success["status"] == "ok"
103105
assert insert_success["tags"]["status"] == "ok"
106+
assert insert_fail["status"] == "internal_error"
104107
assert insert_fail["tags"]["status"] == "internal_error"
105108

106109

0 commit comments

Comments
 (0)