Skip to content

Commit 739aac5

Browse files
hanhan761eavanvalkenburg
authored andcommitted
test: cover standard OTel usage attributes
1 parent a40f255 commit 739aac5

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

python/packages/core/tests/core/test_observability.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ def test_configure_otel_providers_explicit_console_exporters_overrides_env(monke
15351535

15361536

15371537
def test_observability_settings_defaults_instrumentation_true(monkeypatch):
1538-
"""ENABLE_INSTRUMENTATION unset ObservabilitySettings defaults to True."""
1538+
"""ENABLE_INSTRUMENTATION unset 鈫?ObservabilitySettings defaults to True."""
15391539
from agent_framework.observability import ObservabilitySettings
15401540

15411541
monkeypatch.delenv("ENABLE_INSTRUMENTATION", raising=False)
@@ -2154,6 +2154,29 @@ def test_get_response_attributes_with_usage():
21542154
assert result[OtelAttr.OUTPUT_TOKENS] == 50
21552155

21562156

2157+
def test_get_response_attributes_maps_detailed_usage_to_standard_otel_attrs():
2158+
"""Test detailed usage fields use standard OTel GenAI attributes."""
2159+
from unittest.mock import Mock
2160+
2161+
from agent_framework.observability import OtelAttr, _get_response_attributes
2162+
2163+
response = Mock()
2164+
response.response_id = None
2165+
response.finish_reason = None
2166+
response.raw_representation = None
2167+
response.usage_details = {
2168+
"cache_creation_input_token_count": 10,
2169+
"cache_read_input_token_count": 20,
2170+
"reasoning_output_token_count": 30,
2171+
}
2172+
2173+
result = _get_response_attributes({}, response)
2174+
2175+
assert result[OtelAttr.CACHE_CREATION_INPUT_TOKENS] == 10
2176+
assert result[OtelAttr.CACHE_READ_INPUT_TOKENS] == 20
2177+
assert result[OtelAttr.REASONING_OUTPUT_TOKENS] == 30
2178+
2179+
21572180
def test_get_response_attributes_capture_usage_false():
21582181
"""Test _get_response_attributes skips usage when capture_usage is False."""
21592182
from unittest.mock import Mock
@@ -3113,7 +3136,7 @@ async def test_capture_messages_preserves_non_ascii_characters(mock_chat_client,
31133136
"""Test that non-ASCII characters (e.g., Japanese) are preserved in span attributes."""
31143137
import json
31153138

3116-
japanese_text = "こんにちは世界" # "Hello World" in Japanese
3139+
japanese_text = "銇撱倱銇仭銇笘鐣? # "Hello World" in Japanese
31173140

31183141
class ClientWithJapanese(mock_chat_client):
31193142
async def _inner_get_response(self, *, messages, options, **kwargs):
@@ -3158,7 +3181,7 @@ async def test_system_instructions_preserves_non_ascii_characters(span_exporter:
31583181

31593182
from opentelemetry import trace
31603183

3161-
chinese_text = "你好世界" # "Hello World" in Chinese
3184+
chinese_text = "浣犲ソ涓栫晫" # "Hello World" in Chinese
31623185

31633186
tracer = trace.get_tracer("test")
31643187
span_exporter.clear()
@@ -3306,7 +3329,7 @@ async def test_tool_arguments_preserves_non_ascii_characters(span_exporter: InMe
33063329
"""Test that non-ASCII characters are preserved in tool arguments span attribute."""
33073330
import json
33083331

3309-
korean_text = "안녕하세요" # "Hello" in Korean
3332+
korean_text = "鞎堧厱頃橃劯鞖? # "Hello" in Korean
33103333

33113334
@tool
33123335
def greet(message: str) -> str:
@@ -3333,7 +3356,7 @@ def greet(message: str) -> str:
33333356
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
33343357
async def test_tool_result_preserves_non_ascii_characters(span_exporter: InMemorySpanExporter):
33353358
"""Test that non-ASCII characters are preserved in tool result span attribute."""
3336-
arabic_text = "مرحبا بالعالم" # "Hello World" in Arabic
3359+
arabic_text = "賲乇丨亘丕 亘丕賱毓丕賱賲" # "Hello World" in Arabic
33373360

33383361
@tool
33393362
def echo(text: str) -> str:
@@ -3363,7 +3386,7 @@ async def test_tool_arguments_pydantic_preserves_non_ascii_characters(
33633386

33643387
from pydantic import BaseModel
33653388

3366-
japanese_text = "こんにちは" # "Hello" in Japanese
3389+
japanese_text = "銇撱倱銇仭銇? # "Hello" in Japanese
33673390

33683391
class Greeting(BaseModel):
33693392
message: str
@@ -3917,7 +3940,7 @@ def mock_get_meter(*args, **kwargs):
39173940
@tool(name="get_weather", description="Get weather for a city", approval_mode="never_require")
39183941
def _get_weather(city: str) -> str:
39193942
"""Get weather for a city."""
3920-
return "Sunny, 72°F"
3943+
return "Sunny, 72掳F"
39213944

39223945

39233946
@pytest.mark.parametrize("enable_sensitive_data", [False], indirect=True)
@@ -4664,4 +4687,4 @@ async def test_chat_capture_messages_called_when_span_recording(
46644687
assert response is not None
46654688
# Two _capture_messages calls: one for input, one for output messages.
46664689
assert mock_capture_messages.call_count == 2
4667-
assert mock_capture_response.call_count == 1
4690+
assert mock_capture_response.call_count == 1

0 commit comments

Comments
 (0)