Skip to content

Commit 85ac7e7

Browse files
committed
fix: set MAF framework provider for agent executor spans
1 parent 07b0657 commit 85ac7e7

4 files changed

Lines changed: 42 additions & 0 deletions

File tree

instrumentation-loongsuite/loongsuite-instrumentation-microsoft-agent-framework/src/opentelemetry/instrumentation/microsoft_agent_framework/span_processor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
_EDGE_GROUP_PROCESS = "edge_group.process"
9595
_LIVE_SPAN_MAX_AGE_NS = 60 * 1_000_000_000
9696
_GEN_AI_TOOL_NAME = "gen_ai.tool.name"
97+
_FRAMEWORK_PROVIDER_NAME = "microsoft.agent_framework"
9798

9899

99100
def _attr_value(span: Any, key: str) -> Any:
@@ -876,6 +877,16 @@ def _apply_semantic_attributes(
876877
normalized = _normalize_provider(provider)
877878
if normalized is not None and normalized != provider:
878879
_set_attr(live, GEN_AI_PROVIDER_NAME, normalized)
880+
elif (
881+
normalized is None
882+
and span_kind == GenAISpanKind.AGENT
883+
and op_name
884+
in {
885+
GenAIOperation.CREATE_AGENT,
886+
GenAIOperation.INVOKE_AGENT,
887+
}
888+
):
889+
_set_attr(live, GEN_AI_PROVIDER_NAME, _FRAMEWORK_PROVIDER_NAME)
879890

880891
# 5) Normalize finish reasons written by MAF as a JSON string.
881892
_normalize_finish_reasons(live, readable)

instrumentation-loongsuite/loongsuite-instrumentation-microsoft-agent-framework/src/opentelemetry/instrumentation/microsoft_agent_framework/util_genai_bridge.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
GenAISpanKind,
7777
)
7878
from .span_processor import (
79+
_FRAMEWORK_PROVIDER_NAME,
7980
_attr_value,
8081
_classify_span,
8182
_is_exception_event,
@@ -598,6 +599,11 @@ def _set_common_live_attributes(
598599
provider = _normalize_provider(_attr_value(span, GEN_AI_PROVIDER_NAME))
599600
if provider is not None:
600601
span.set_attribute(GEN_AI_PROVIDER_NAME, provider)
602+
elif span_kind == GenAISpanKind.AGENT and op_name in {
603+
GenAIOperation.CREATE_AGENT,
604+
GenAIOperation.INVOKE_AGENT,
605+
}:
606+
span.set_attribute(GEN_AI_PROVIDER_NAME, _FRAMEWORK_PROVIDER_NAME)
601607
finish_reasons = _finish_reasons(span)
602608
if finish_reasons:
603609
span.set_attribute(GEN_AI_RESPONSE_FINISH_REASONS, finish_reasons)

instrumentation-loongsuite/loongsuite-instrumentation-microsoft-agent-framework/tests/test_processor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ def test_executor_process_agent_executor_becomes_agent():
207207
assert (
208208
s.attributes.get(GEN_AI_OPERATION_NAME) == GenAIOperation.INVOKE_AGENT
209209
)
210+
assert (
211+
s.attributes.get(GEN_AI_PROVIDER_NAME) == "microsoft.agent_framework"
212+
)
210213

211214

212215
def test_executor_process_unknown_executor_stays_workflow_operation():

instrumentation-loongsuite/loongsuite-instrumentation-microsoft-agent-framework/tests/test_util_genai_bridge.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,28 @@ def test_agent_span_is_finalized_by_util_genai_before_export(monkeypatch):
303303
assert span.attributes.get("gen_ai.agent.id") == "agent-1"
304304

305305

306+
def test_agent_span_gets_framework_provider_when_missing(monkeypatch):
307+
obs_mod, exporter = _install_fake_observability(monkeypatch)
308+
util_genai_bridge.apply_util_genai_bridge()
309+
try:
310+
with obs_mod._get_span(
311+
{
312+
GEN_AI_OPERATION_NAME: GenAIOperation.INVOKE_AGENT,
313+
"gen_ai.agent.name": "planner",
314+
},
315+
"gen_ai.agent.name",
316+
):
317+
pass
318+
finally:
319+
util_genai_bridge.revert_util_genai_bridge()
320+
321+
span = exporter.get_finished_spans()[0]
322+
assert (
323+
span.attributes.get(GEN_AI_PROVIDER_NAME)
324+
== "microsoft.agent_framework"
325+
)
326+
327+
306328
def test_mcp_span_is_seeded_before_export(monkeypatch):
307329
obs_mod, exporter = _install_fake_observability(monkeypatch)
308330
util_genai_bridge.apply_util_genai_bridge()

0 commit comments

Comments
 (0)