-
Notifications
You must be signed in to change notification settings - Fork 8
fix: use standard gen_ai.conversation.id instead of microsoft.sessiomid in LangChain tracer #152
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
Open
hectorhdzg
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
hectorhdzg:fix/langchain-otel-semconv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
59 changes: 59 additions & 0 deletions
59
src/microsoft/opentelemetry/_genai/_langchain/_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,59 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Span enricher for LangChain. | ||
|
|
||
| Maps standard OTel GenAI attributes to A365-specific keys before export. | ||
| Registered by LangChainInstrumentor when the A365 pipeline is available. | ||
| """ | ||
|
|
||
| from microsoft.opentelemetry.a365.core.constants import ( | ||
| EXECUTE_TOOL_OPERATION_NAME, | ||
| GEN_AI_CONVERSATION_ID_KEY, | ||
| 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, | ||
| SESSION_ID_KEY, | ||
| ) | ||
| from microsoft.opentelemetry.a365.core.exporters.enriched_span import EnrichedReadableSpan | ||
| from opentelemetry.sdk.trace import ReadableSpan | ||
|
|
||
|
|
||
| def enrich_langchain_span(span: ReadableSpan) -> ReadableSpan: | ||
| """Enricher function for LangChain spans. | ||
|
|
||
| Transforms standard OTel GenAI attributes to A365-specific keys: | ||
| - ``gen_ai.conversation.id`` → ``microsoft.session.id`` | ||
| - For invoke_agent spans: extracts content from input/output messages | ||
| - For execute_tool spans: maps tool arguments and results | ||
| """ | ||
| extra_attributes = {} | ||
| attributes = span.attributes or {} | ||
|
|
||
| # Map gen_ai.conversation.id → microsoft.session.id for A365 consumers | ||
| conversation_id = attributes.get(GEN_AI_CONVERSATION_ID_KEY) | ||
| if conversation_id and SESSION_ID_KEY not in attributes: | ||
| extra_attributes[SESSION_ID_KEY] = str(conversation_id) | ||
|
|
||
| if span.name.startswith(INVOKE_AGENT_OPERATION_NAME): | ||
| input_messages = attributes.get(GEN_AI_INPUT_MESSAGES_KEY) | ||
| if input_messages: | ||
| extra_attributes[GEN_AI_INPUT_MESSAGES_KEY] = str(input_messages) | ||
|
|
||
| output_messages = attributes.get(GEN_AI_OUTPUT_MESSAGES_KEY) | ||
| if output_messages: | ||
| extra_attributes[GEN_AI_OUTPUT_MESSAGES_KEY] = str(output_messages) | ||
|
|
||
| elif span.name.startswith(EXECUTE_TOOL_OPERATION_NAME): | ||
| if GEN_AI_TOOL_ARGS_KEY in attributes: | ||
| extra_attributes[GEN_AI_TOOL_ARGS_KEY] = str(attributes[GEN_AI_TOOL_ARGS_KEY]) | ||
|
|
||
| if GEN_AI_TOOL_CALL_RESULT_KEY in attributes: | ||
| extra_attributes[GEN_AI_TOOL_CALL_RESULT_KEY] = str(attributes[GEN_AI_TOOL_CALL_RESULT_KEY]) | ||
|
|
||
| if extra_attributes: | ||
| return EnrichedReadableSpan(span, extra_attributes) | ||
|
|
||
| return span |
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
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,116 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Tests for LangChain span enricher.""" | ||
|
|
||
| import unittest | ||
| from unittest.mock import Mock | ||
|
|
||
| from microsoft.opentelemetry.a365.core.constants import ( | ||
| GEN_AI_CONVERSATION_ID_KEY, | ||
| GEN_AI_INPUT_MESSAGES_KEY, | ||
| GEN_AI_OUTPUT_MESSAGES_KEY, | ||
| GEN_AI_TOOL_ARGS_KEY, | ||
| GEN_AI_TOOL_CALL_RESULT_KEY, | ||
| SESSION_ID_KEY, | ||
| ) | ||
| from microsoft.opentelemetry._genai._langchain._span_enricher import enrich_langchain_span | ||
|
|
||
|
|
||
| class TestLangChainSpanEnricher(unittest.TestCase): | ||
| """Test suite for enrich_langchain_span function.""" | ||
|
|
||
| def test_maps_conversation_id_to_session_id(self): | ||
| """gen_ai.conversation.id is mapped to microsoft.session.id.""" | ||
| span = Mock() | ||
| span.name = "chat gpt-4o" | ||
| span.attributes = { | ||
| GEN_AI_CONVERSATION_ID_KEY: "conv-123", | ||
| } | ||
|
|
||
| result = enrich_langchain_span(span) | ||
|
|
||
| assert result.attributes is not None | ||
|
hectorhdzg marked this conversation as resolved.
|
||
| self.assertEqual(result.attributes[SESSION_ID_KEY], "conv-123") | ||
|
|
||
| def test_does_not_overwrite_existing_session_id(self): | ||
| """If microsoft.session.id is already present, don't overwrite.""" | ||
| span = Mock() | ||
| span.name = "chat gpt-4o" | ||
| span.attributes = { | ||
| GEN_AI_CONVERSATION_ID_KEY: "conv-123", | ||
| SESSION_ID_KEY: "existing-session", | ||
| } | ||
|
|
||
| result = enrich_langchain_span(span) | ||
| # Should return original span unchanged | ||
| self.assertIs(result, span) | ||
|
|
||
| def test_invoke_agent_span_enrichment(self): | ||
| """invoke_agent spans pass through input/output messages.""" | ||
| span = Mock() | ||
| span.name = "invoke_agent Travel_Assistant" | ||
| span.attributes = { | ||
| GEN_AI_INPUT_MESSAGES_KEY: '["Where should I go?"]', | ||
| GEN_AI_OUTPUT_MESSAGES_KEY: '["Try Barcelona!"]', | ||
| } | ||
|
|
||
| result = enrich_langchain_span(span) | ||
|
|
||
| assert result.attributes is not None | ||
|
hectorhdzg marked this conversation as resolved.
|
||
| self.assertEqual(result.attributes[GEN_AI_INPUT_MESSAGES_KEY], '["Where should I go?"]') | ||
| self.assertEqual(result.attributes[GEN_AI_OUTPUT_MESSAGES_KEY], '["Try Barcelona!"]') | ||
|
|
||
| def test_invoke_agent_with_conversation_id(self): | ||
| """invoke_agent spans get both session_id mapping and message enrichment.""" | ||
| span = Mock() | ||
| span.name = "invoke_agent MyAgent" | ||
| span.attributes = { | ||
| GEN_AI_CONVERSATION_ID_KEY: "conv-456", | ||
| GEN_AI_INPUT_MESSAGES_KEY: '["Hello"]', | ||
| } | ||
|
|
||
| result = enrich_langchain_span(span) | ||
|
|
||
| assert result.attributes is not None | ||
|
hectorhdzg marked this conversation as resolved.
|
||
| self.assertEqual(result.attributes[SESSION_ID_KEY], "conv-456") | ||
| self.assertEqual(result.attributes[GEN_AI_INPUT_MESSAGES_KEY], '["Hello"]') | ||
|
|
||
| def test_execute_tool_span_enrichment(self): | ||
| """execute_tool spans map tool arguments and results.""" | ||
| span = Mock() | ||
| span.name = "execute_tool get_weather" | ||
| span.attributes = { | ||
| GEN_AI_TOOL_ARGS_KEY: '{"city": "Barcelona"}', | ||
| GEN_AI_TOOL_CALL_RESULT_KEY: "Sunny, 25C", | ||
| } | ||
|
|
||
| result = enrich_langchain_span(span) | ||
|
|
||
| assert result.attributes is not None | ||
|
hectorhdzg marked this conversation as resolved.
|
||
| self.assertEqual(result.attributes[GEN_AI_TOOL_ARGS_KEY], '{"city": "Barcelona"}') | ||
| self.assertEqual(result.attributes[GEN_AI_TOOL_CALL_RESULT_KEY], "Sunny, 25C") | ||
|
|
||
| def test_non_matching_span_returns_original(self): | ||
| """Non-matching spans without conversation_id return unchanged.""" | ||
| span = Mock() | ||
| span.name = "other_operation" | ||
| span.attributes = {"key": "value"} | ||
|
|
||
| self.assertIs(enrich_langchain_span(span), span) | ||
|
|
||
| def test_none_attributes_returns_original(self): | ||
| """Spans with None attributes return unchanged.""" | ||
| span = Mock() | ||
| span.name = "invoke_agent Test" | ||
| span.attributes = None | ||
|
|
||
| self.assertIs(enrich_langchain_span(span), span) | ||
|
|
||
| def test_empty_attributes_returns_original(self): | ||
| """Spans with empty attributes return unchanged.""" | ||
| span = Mock() | ||
| span.name = "invoke_agent Test" | ||
| span.attributes = {} | ||
|
|
||
| self.assertIs(enrich_langchain_span(span), span) | ||
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
Oops, something went wrong.
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.