-
Notifications
You must be signed in to change notification settings - Fork 10
Agent framework extension update for input and output attributes #154
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
...-agentframework/microsoft_agents_a365/observability/extensions/agentframework/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,10 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Agent Framework observability extensions for Agent365.""" | ||
|
|
||
| from .trace_instrumentor import AgentFrameworkInstrumentor | ||
|
|
||
| __all__ = [ | ||
| "AgentFrameworkInstrumentor", | ||
| ] |
51 changes: 51 additions & 0 deletions
51
...tframework/microsoft_agents_a365/observability/extensions/agentframework/span_enricher.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from microsoft_agents_a365.observability.core.constants import ( | ||
| EXECUTE_TOOL_OPERATION_NAME, | ||
| GEN_AI_INPUT_MESSAGES_KEY, | ||
| GEN_AI_OUTPUT_MESSAGES_KEY, | ||
| GEN_AI_TOOL_ARGS_KEY, | ||
| GEN_AI_TOOL_CALL_RESULT_KEY, | ||
| INVOKE_AGENT_OPERATION_NAME, | ||
| ) | ||
| from microsoft_agents_a365.observability.core.exporters.enriched_span import EnrichedReadableSpan | ||
| from opentelemetry.sdk.trace import ReadableSpan | ||
|
|
||
| from .utils import extract_input_content, extract_output_content | ||
|
|
||
| # Agent Framework specific attribute keys | ||
| AF_TOOL_CALL_ARGUMENTS_KEY = "gen_ai.tool.call.arguments" | ||
| AF_TOOL_CALL_RESULT_KEY = "gen_ai.tool.call.result" | ||
|
|
||
|
|
||
| def enrich_agent_framework_span(span: ReadableSpan) -> ReadableSpan: | ||
| """ | ||
| Enricher function for Agent Framework spans. | ||
| """ | ||
| extra_attributes = {} | ||
| attributes = span.attributes or {} | ||
|
|
||
| # Only extract content for invoke_agent spans | ||
| if span.name.startswith(INVOKE_AGENT_OPERATION_NAME): | ||
| # Extract all text content from input messages | ||
nikhilNava marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| input_messages = attributes.get(GEN_AI_INPUT_MESSAGES_KEY) | ||
| if input_messages: | ||
| extra_attributes[GEN_AI_INPUT_MESSAGES_KEY] = extract_input_content(input_messages) | ||
|
|
||
| output_messages = attributes.get(GEN_AI_OUTPUT_MESSAGES_KEY) | ||
| if output_messages: | ||
| extra_attributes[GEN_AI_OUTPUT_MESSAGES_KEY] = extract_output_content(output_messages) | ||
|
|
||
| # Map tool attributes for execute_tool spans | ||
| elif span.name.startswith(EXECUTE_TOOL_OPERATION_NAME): | ||
| if AF_TOOL_CALL_ARGUMENTS_KEY in attributes: | ||
| extra_attributes[GEN_AI_TOOL_ARGS_KEY] = attributes[AF_TOOL_CALL_ARGUMENTS_KEY] | ||
|
|
||
| if AF_TOOL_CALL_RESULT_KEY in attributes: | ||
| extra_attributes[GEN_AI_TOOL_CALL_RESULT_KEY] = attributes[AF_TOOL_CALL_RESULT_KEY] | ||
|
|
||
| if extra_attributes: | ||
| return EnrichedReadableSpan(span, extra_attributes) | ||
|
|
||
| return span | ||
7 changes: 2 additions & 5 deletions
7
...framework/microsoft_agents_a365/observability/extensions/agentframework/span_processor.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...ons-agentframework/microsoft_agents_a365/observability/extensions/agentframework/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Utility functions for Agent Framework observability extensions.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
|
|
||
|
|
||
| def extract_content_as_string_list(messages_json: str, role_filter: str | None = None) -> str: | ||
| """Extract content values from messages JSON and return as JSON string list.""" | ||
| try: | ||
| messages = json.loads(messages_json) | ||
| if isinstance(messages, list): | ||
| contents = [] | ||
| for msg in messages: | ||
| if isinstance(msg, dict): | ||
nikhilNava marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| role = msg.get("role", "") | ||
|
|
||
| # Filter by role if specified | ||
| if role_filter and role != role_filter: | ||
| continue | ||
|
|
||
| # Handle Agent Framework format with "parts" | ||
| parts = msg.get("parts") | ||
| if parts and isinstance(parts, list): | ||
| for part in parts: | ||
| if isinstance(part, dict): | ||
| part_type = part.get("type", "") | ||
| # Only extract text content, not tool_call or tool_call_response | ||
| if part_type == "text" and "content" in part: | ||
| contents.append(part["content"]) | ||
| return json.dumps(contents) | ||
| return messages_json | ||
| except (json.JSONDecodeError, TypeError): | ||
| # If parsing fails, return as-is | ||
| return messages_json | ||
|
|
||
|
|
||
| def extract_input_content(messages_json: str) -> str: | ||
| """Extract text content from user messages only.""" | ||
| return extract_content_as_string_list(messages_json, role_filter="user") | ||
|
|
||
|
|
||
| def extract_output_content(messages_json: str) -> str: | ||
| """Extract only assistant text content from output messages.""" | ||
| return extract_content_as_string_list(messages_json, role_filter="assistant") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. |
68 changes: 68 additions & 0 deletions
68
tests/observability/extensions/agentframework/test_span_enricher.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Tests for Agent Framework span enricher.""" | ||
|
|
||
| import unittest | ||
| from unittest.mock import Mock | ||
|
|
||
| from microsoft_agents_a365.observability.core.constants import ( | ||
| GEN_AI_INPUT_MESSAGES_KEY, | ||
| GEN_AI_OUTPUT_MESSAGES_KEY, | ||
| GEN_AI_TOOL_ARGS_KEY, | ||
| GEN_AI_TOOL_CALL_RESULT_KEY, | ||
| ) | ||
| from microsoft_agents_a365.observability.extensions.agentframework.span_enricher import ( | ||
| AF_TOOL_CALL_ARGUMENTS_KEY, | ||
| AF_TOOL_CALL_RESULT_KEY, | ||
| enrich_agent_framework_span, | ||
| ) | ||
|
|
||
|
|
||
| class TestAgentFrameworkSpanEnricher(unittest.TestCase): | ||
| """Test suite for enrich_agent_framework_span function.""" | ||
|
|
||
| def test_invoke_agent_span_enrichment(self): | ||
| """Test invoke_agent span extracts user input and assistant output text only.""" | ||
| span = Mock( | ||
| name="invoke_agent Agent365Assistant", | ||
| attributes={ | ||
| GEN_AI_INPUT_MESSAGES_KEY: '[{"role": "user", "parts": [{"type": "text", "content": "Compute 15 % 4"}]}]', | ||
| GEN_AI_OUTPUT_MESSAGES_KEY: '[{"role": "assistant", "parts": [{"type": "tool_call", "id": "c1"}]}, {"role": "tool", "parts": [{"type": "tool_call_response"}]}, {"role": "assistant", "parts": [{"type": "text", "content": "Result is 3."}]}]', | ||
| }, | ||
| ) | ||
| span.name = "invoke_agent Agent365Assistant" | ||
| result = enrich_agent_framework_span(span) | ||
| self.assertEqual(result.attributes[GEN_AI_INPUT_MESSAGES_KEY], '["Compute 15 % 4"]') | ||
| self.assertEqual(result.attributes[GEN_AI_OUTPUT_MESSAGES_KEY], '["Result is 3."]') | ||
|
|
||
| def test_execute_tool_span_enrichment(self): | ||
| """Test execute_tool span maps tool arguments and result to standard keys.""" | ||
| span = Mock( | ||
| name="execute_tool calculate", | ||
| attributes={ | ||
| AF_TOOL_CALL_ARGUMENTS_KEY: '{"expression": "2 + 2"}', | ||
| AF_TOOL_CALL_RESULT_KEY: "Result is 4", | ||
| }, | ||
| ) | ||
| span.name = "execute_tool calculate" | ||
| result = enrich_agent_framework_span(span) | ||
| self.assertEqual(result.attributes[GEN_AI_TOOL_ARGS_KEY], '{"expression": "2 + 2"}') | ||
| self.assertEqual(result.attributes[GEN_AI_TOOL_CALL_RESULT_KEY], "Result is 4") | ||
|
|
||
| def test_non_matching_and_edge_cases_return_original(self): | ||
| """Test non-matching, None, and empty attribute spans return unchanged.""" | ||
| span = Mock(name="other_op", attributes={"key": "value"}) | ||
| span.name = "other_op" | ||
| self.assertEqual(enrich_agent_framework_span(span), span) | ||
|
|
||
| span.name = "invoke_agent Test" | ||
| span.attributes = None | ||
| self.assertEqual(enrich_agent_framework_span(span), span) | ||
|
|
||
| span.attributes = {} | ||
| self.assertEqual(enrich_agent_framework_span(span), span) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
35 changes: 35 additions & 0 deletions
35
tests/observability/extensions/agentframework/test_utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Tests for Agent Framework utils.""" | ||
|
|
||
| import unittest | ||
|
|
||
| from microsoft_agents_a365.observability.extensions.agentframework.utils import ( | ||
| extract_content_as_string_list, | ||
| extract_input_content, | ||
| extract_output_content, | ||
| ) | ||
|
|
||
|
|
||
| class TestAgentFrameworkUtils(unittest.TestCase): | ||
| """Test suite for Agent Framework utility functions.""" | ||
|
|
||
| def test_extract_content_filters_text_by_role(self): | ||
| """Test text extraction with role filtering, ignoring tool calls.""" | ||
| msgs = '[{"role": "user", "parts": [{"type": "text", "content": "Hi"}]}, {"role": "assistant", "parts": [{"type": "tool_call"}, {"type": "text", "content": "Hello"}]}]' | ||
| self.assertEqual(extract_content_as_string_list(msgs), '["Hi", "Hello"]') | ||
| self.assertEqual(extract_content_as_string_list(msgs, role_filter="user"), '["Hi"]') | ||
| self.assertEqual(extract_input_content(msgs), '["Hi"]') | ||
| self.assertEqual(extract_output_content(msgs), '["Hello"]') | ||
|
|
||
| def test_handles_invalid_and_edge_cases(self): | ||
| """Test invalid JSON and edge cases return appropriate values.""" | ||
| self.assertEqual(extract_content_as_string_list("invalid"), "invalid") | ||
| self.assertEqual(extract_content_as_string_list('{"not": "list"}'), '{"not": "list"}') | ||
| self.assertEqual(extract_content_as_string_list("[]"), "[]") | ||
| self.assertEqual(extract_content_as_string_list('[{"role": "user"}]'), "[]") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.