-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Add GenAI main-agent attribution processor #46700
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
rads-1996
wants to merge
8
commits into
Azure:main
Choose a base branch
from
rads-1996:implement-genai-spec
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8247c91
Add GenAI main-agent attribution processors to propagate microsoft.ge…
rads-1996 c379138
Update CHANGELOG
rads-1996 a583a4c
Move constants
rads-1996 96b0cfc
Address comments
rads-1996 f8285cd
Merge branch 'main' into implement-genai-spec
rads-1996 0534e79
Merge branch 'main' into implement-genai-spec
rads-1996 bc8e659
Merge branch 'main' into implement-genai-spec
rads-1996 8718ece
Merge branch 'main' into implement-genai-spec
rads-1996 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
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
5 changes: 5 additions & 0 deletions
5
...e-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_gen_ai/__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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- |
117 changes: 117 additions & 0 deletions
117
...monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_gen_ai/_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
|
|
||
| from typing import Optional | ||
|
|
||
| from opentelemetry.context import Context | ||
| from opentelemetry.sdk._logs import LogRecordProcessor, ReadWriteLogRecord | ||
| from opentelemetry.sdk.trace import ReadableSpan, SpanProcessor | ||
| from opentelemetry.trace import get_current_span, Span | ||
|
|
||
| from azure.monitor.opentelemetry.exporter._constants import ( | ||
| _MAIN_AGENT_ATTRIBUTES, | ||
| _MAIN_AGENT_PREFIX, | ||
| _MAIN_AGENT_SELF_ATTRIBUTES, | ||
| ) | ||
|
|
||
|
|
||
| # pylint: disable=protected-access | ||
| class _GenAIMainAgentSpanProcessor(SpanProcessor): | ||
| """Propagates main-agent context in GenAI multi-agent systems. | ||
|
|
||
| In OnStart, copies microsoft.gen_ai.main_agent.* attributes from the parent span | ||
| to the child span (with fallback to gen_ai.agent.* on the parent). | ||
|
|
||
| In OnEnd, self-attributes root invoke_agent spans that have no main_agent context. | ||
| """ | ||
|
|
||
| def on_start(self, span: Span, parent_context: Optional[Context] = None) -> None: # type: ignore | ||
| if parent_context is None: | ||
| return | ||
| parent_span = get_current_span(parent_context) | ||
| parent_span_context = parent_span.get_span_context() | ||
| if not parent_span_context.is_valid: | ||
| return | ||
|
|
||
| parent_attributes = getattr(parent_span, "attributes", None) | ||
| if parent_attributes is None: | ||
| return | ||
|
|
||
| for target, primary_source, fallback_source in _MAIN_AGENT_ATTRIBUTES: | ||
| value = parent_attributes.get(primary_source) | ||
| if value is None: | ||
| value = parent_attributes.get(fallback_source) | ||
| if value is not None: | ||
| span.set_attribute(target, value) | ||
|
|
||
| def on_end(self, span: ReadableSpan) -> None: | ||
| attributes = span.attributes | ||
| if attributes is None: | ||
| return | ||
|
|
||
| # Only apply to spans with gen_ai.operation.name = "invoke_agent" | ||
| if attributes.get("gen_ai.operation.name") != "invoke_agent": | ||
| return | ||
|
|
||
| # If span already has any microsoft.gen_ai.main_agent.* attribute, return | ||
| for key in attributes: | ||
| if key.startswith(_MAIN_AGENT_PREFIX): | ||
| return | ||
|
|
||
| # Self-attribute from the span's own gen_ai attributes | ||
| for target, source in _MAIN_AGENT_SELF_ATTRIBUTES: | ||
| value = attributes.get(source) | ||
| if value is not None: | ||
| span._attributes[target] = value # type: ignore | ||
|
rads-1996 marked this conversation as resolved.
|
||
|
|
||
| def shutdown(self): | ||
| pass | ||
|
|
||
| def force_flush(self, timeout_millis: int = 30000): | ||
| return True | ||
|
|
||
|
|
||
| class _GenAIMainAgentLogRecordProcessor(LogRecordProcessor): | ||
| """Copies microsoft.gen_ai.main_agent.* attributes from the current span onto log records.""" | ||
|
|
||
| def on_emit(self, log_record: ReadWriteLogRecord) -> None: # type: ignore # pylint: disable=arguments-renamed | ||
| current_span = get_current_span() | ||
| span_context = current_span.get_span_context() | ||
| if not span_context.is_valid: | ||
| return | ||
|
|
||
| span_attributes = getattr(current_span, "attributes", None) | ||
| if span_attributes is None: | ||
| return | ||
|
|
||
| # Collect all microsoft.gen_ai.main_agent.* attributes from the current span | ||
| main_agent_attrs = {key: value for key, value in span_attributes.items() if key.startswith(_MAIN_AGENT_PREFIX)} | ||
|
|
||
| if not main_agent_attrs: | ||
| return | ||
|
|
||
| # Copy them onto the log record without overwriting any existing log-level values | ||
| if hasattr(log_record, "log_record") and log_record.log_record is not None: | ||
| if log_record.log_record.attributes is None: | ||
| log_record.log_record.attributes = {} | ||
| for key, value in main_agent_attrs.items(): | ||
| if key not in log_record.log_record.attributes: | ||
| log_record.log_record.attributes[key] = value # type: ignore[index] | ||
| elif hasattr(log_record, "attributes"): | ||
| if log_record.attributes is None: # type: ignore[union-attr] | ||
| log_record.attributes = {} # type: ignore[union-attr] | ||
| for key, value in main_agent_attrs.items(): | ||
| if key not in log_record.attributes: # type: ignore[operator] | ||
| log_record.attributes[key] = value # type: ignore[index] | ||
|
|
||
| def emit(self, log_record: ReadWriteLogRecord) -> None: # pylint: disable=arguments-renamed | ||
| self.on_emit(log_record) | ||
|
|
||
| def shutdown(self): | ||
| pass | ||
|
|
||
| def force_flush(self, timeout_millis: int = 30000): | ||
| return True | ||
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.