Skip to content

Commit b7f8ad6

Browse files
Python: Remove span-only tool attributes from invocation metrics (#9592)
* Python: Remove span-only tool metric attributes Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5e52ce1a-cfca-43c6-990b-1dcf74052845 * Address tool metric review feedback Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5e52ce1a-cfca-43c6-990b-1dcf74052845 --------- Co-authored-by: Copilot App <[email protected]> Copilot-Session: 5e52ce1a-cfca-43c6-990b-1dcf74052845
1 parent 5da1769 commit b7f8ad6

2 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎python/packages/core/agent_framework/_tools.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
from opentelemetry import trace
4949
from opentelemetry.metrics import Histogram, NoOpHistogram
50+
from opentelemetry.util.types import AttributeValue
5051
from pydantic import BaseModel, Field, TypeAdapter, ValidationError, create_model
5152
from pydantic_core import to_jsonable_python
5253

@@ -1231,8 +1232,13 @@ async def invoke(
12311232
return parsed
12321233

12331234
span_attributes = get_function_span_attributes(self, tool_call_id=tool_call_id)
1234-
metric_attributes = dict(span_attributes)
1235-
metric_attributes[OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME] = self.name
1235+
# Keep metric dimensions explicit so span-only attributes cannot flow into the histogram.
1236+
metric_attributes: dict[str, AttributeValue] = {
1237+
OtelAttr.OPERATION: OtelAttr.TOOL_EXECUTION_OPERATION,
1238+
OtelAttr.TOOL_NAME: self.name,
1239+
OtelAttr.TOOL_TYPE: "function",
1240+
OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME: self.name,
1241+
}
12361242
# Filter out framework kwargs that are not JSON serializable.
12371243
serializable_kwargs = {
12381244
k: v

‎python/packages/core/tests/core/test_tools.py‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,11 +1037,12 @@ def telemetry_test_tool(expression: str) -> str:
10371037
call_args = mock_histogram.record.call_args
10381038
assert call_args[0][0] > 0 # duration should be positive
10391039
attributes = call_args[1]["attributes"]
1040-
assert attributes[OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME] == "telemetry_test_tool"
1041-
assert attributes[OtelAttr.TOOL_CALL_ID] == "test_call_id"
1042-
assert OtelAttr.TOOL_ARGUMENTS not in attributes
1043-
assert OtelAttr.TOOL_RESULT not in attributes
1044-
assert expression not in attributes.values()
1040+
assert attributes == {
1041+
OtelAttr.OPERATION: OtelAttr.TOOL_EXECUTION_OPERATION,
1042+
OtelAttr.TOOL_NAME: "telemetry_test_tool",
1043+
OtelAttr.TOOL_TYPE: "function",
1044+
OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME: "telemetry_test_tool",
1045+
}
10451046

10461047

10471048
@pytest.mark.parametrize("enable_sensitive_data", [False], indirect=True)
@@ -1085,8 +1086,12 @@ def telemetry_test_tool(x: int, y: int) -> int:
10851086
call_args = mock_histogram.record.call_args
10861087
assert call_args[0][0] > 0 # duration should be positive
10871088
attributes = call_args[1]["attributes"]
1088-
assert attributes[OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME] == "telemetry_test_tool"
1089-
assert attributes[OtelAttr.TOOL_CALL_ID] == "test_call_id"
1089+
assert attributes == {
1090+
OtelAttr.OPERATION: OtelAttr.TOOL_EXECUTION_OPERATION,
1091+
OtelAttr.TOOL_NAME: "telemetry_test_tool",
1092+
OtelAttr.TOOL_TYPE: "function",
1093+
OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME: "telemetry_test_tool",
1094+
}
10901095

10911096

10921097
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
@@ -1228,9 +1233,13 @@ def exception_test_tool(x: int, y: int) -> int:
12281233
mock_histogram.record.assert_called_once()
12291234
call_args = mock_histogram.record.call_args
12301235
attributes = call_args[1]["attributes"]
1231-
assert attributes[OtelAttr.ERROR_TYPE] == ValueError.__name__
1232-
assert OtelAttr.TOOL_ARGUMENTS not in attributes
1233-
assert OtelAttr.TOOL_RESULT not in attributes
1236+
assert attributes == {
1237+
OtelAttr.OPERATION: OtelAttr.TOOL_EXECUTION_OPERATION,
1238+
OtelAttr.TOOL_NAME: "exception_test_tool",
1239+
OtelAttr.TOOL_TYPE: "function",
1240+
OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME: "exception_test_tool",
1241+
OtelAttr.ERROR_TYPE: ValueError.__name__,
1242+
}
12341243

12351244

12361245
async def test_tool_invoke_telemetry_async_function(span_exporter: InMemorySpanExporter):
@@ -1268,7 +1277,12 @@ async def async_telemetry_test(x: int, y: int) -> int:
12681277
mock_histogram.record.assert_called_once()
12691278
call_args = mock_histogram.record.call_args
12701279
attributes = call_args[1]["attributes"]
1271-
assert attributes[OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME] == "async_telemetry_test"
1280+
assert attributes == {
1281+
OtelAttr.OPERATION: OtelAttr.TOOL_EXECUTION_OPERATION,
1282+
OtelAttr.TOOL_NAME: "async_telemetry_test",
1283+
OtelAttr.TOOL_TYPE: "function",
1284+
OtelAttr.MEASUREMENT_FUNCTION_TAG_NAME: "async_telemetry_test",
1285+
}
12721286

12731287

12741288
async def test_tool_invoke_invalid_pydantic_args():

0 commit comments

Comments
 (0)