Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Update agent tracing span name and attributes #10398

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from opentelemetry.trace import get_tracer

from semantic_kernel.utils.experimental_decorator import experimental_function
from semantic_kernel.utils.telemetry.agent_diagnostics import gen_ai_attributes

if TYPE_CHECKING:
from semantic_kernel.agents.agent import Agent
Expand All @@ -19,12 +20,22 @@
@experimental_function
def trace_agent_invocation(invoke_func: Callable) -> Callable:
"""Decorator to trace agent invocation."""
OPERATION_NAME = "invoke_agent"

@functools.wraps(invoke_func)
async def wrapper_decorator(*args: Any, **kwargs: Any) -> AsyncIterable:
agent: "Agent" = args[0]

with tracer.start_as_current_span(agent.name):
with tracer.start_as_current_span(f"{OPERATION_NAME} {agent.name}") as span:
span.set_attributes({
gen_ai_attributes.OPERATION: OPERATION_NAME,
gen_ai_attributes.AGENT_ID: agent.id,
gen_ai_attributes.AGENT_NAME: agent.name,
})

if agent.description:
span.set_attribute(gen_ai_attributes.AGENT_DESCRIPTION, agent.description)

async for response in invoke_func(*args, **kwargs):
yield response

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (c) Microsoft. All rights reserved.

# Constants for tracing agent activities with semantic conventions.
# Ideally, we should use the attributes from the semcov package.
# However, many of the attributes are not yet available in the package,
# so we define them here for now.

# Activity tags
OPERATION = "gen_ai.operation.name"
AGENT_ID = "gen_ai.agent.id"
AGENT_NAME = "gen_ai.agent.name"
AGENT_DESCRIPTION = "gen_ai.agent.description"
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def test_chat_completion_agent_invoke(mock_tracer, chat_history):
async for _ in chat_completion_agent.invoke(chat_history):
pass
# Assert
mock_tracer.start_as_current_span.assert_called_once_with(chat_completion_agent.name)
mock_tracer.start_as_current_span.assert_called_once_with(f"invoke_agent {chat_completion_agent.name}")


@patch("semantic_kernel.utils.telemetry.agent_diagnostics.decorators.tracer")
Expand All @@ -30,4 +30,4 @@ async def test_chat_completion_agent_invoke_stream(mock_tracer, chat_history):
async for _ in chat_completion_agent.invoke_stream(chat_history):
pass
# Assert
mock_tracer.start_as_current_span.assert_called_once_with(chat_completion_agent.name)
mock_tracer.start_as_current_span.assert_called_once_with(f"invoke_agent {chat_completion_agent.name}")
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def test_open_ai_assistant_agent_invoke(mock_tracer, chat_history, openai_
async for _ in open_ai_assistant_agent.invoke(chat_history):
pass
# Assert
mock_tracer.start_as_current_span.assert_called_once_with(open_ai_assistant_agent.name)
mock_tracer.start_as_current_span.assert_called_once_with(f"invoke_agent {open_ai_assistant_agent.name}")


@patch("semantic_kernel.utils.telemetry.agent_diagnostics.decorators.tracer")
Expand All @@ -30,4 +30,4 @@ async def test_open_ai_assistant_agent_invoke_stream(mock_tracer, chat_history,
async for _ in open_ai_assistant_agent.invoke_stream(chat_history):
pass
# Assert
mock_tracer.start_as_current_span.assert_called_once_with(open_ai_assistant_agent.name)
mock_tracer.start_as_current_span.assert_called_once_with(f"invoke_agent {open_ai_assistant_agent.name}")
Loading