-
Notifications
You must be signed in to change notification settings - Fork 42
Prototype showing gen_ai.conversation_root span attribute to mark the root GenAI span of a conversation #187
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
wrisa
wants to merge
3
commits into
open-telemetry:main
Choose a base branch
from
wrisa:conversation-root
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
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
69 changes: 69 additions & 0 deletions
69
util/opentelemetry-util-genai/src/opentelemetry/util/genai/context_attributes.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,69 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file should probably be internal? |
||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """ | ||
| Context-scoped attributes for GenAI instrumentation. | ||
|
|
||
| Follows the API shape proposed by OTel spec PR #4931 (Context-scoped Attributes). | ||
| Attributes stored here are process-local — they are never serialised into W3C | ||
| Baggage headers or any outbound propagation format. | ||
|
|
||
| Currently used to propagate ``gen_ai.conversation_root`` from a root | ||
| WorkflowInvocation or AgentInvocation to child spans so that the root can be | ||
| identified without relying on OTel span parentage (which includes non-GenAI | ||
| parents such as HTTP spans). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from opentelemetry import context as otel_context | ||
| from opentelemetry.context import Context | ||
|
|
||
| # Private context key — never leaked outside this module. | ||
| _GENAI_CONTEXT_ATTRS_KEY = otel_context.create_key( | ||
| "opentelemetry.util.genai.context_scoped_attrs" | ||
| ) | ||
|
|
||
|
|
||
| def set_context_scoped_attributes( | ||
| attrs: dict[str, Any], | ||
| context: Context | None = None, | ||
| ) -> Context: | ||
| """Return a new Context with *attrs* merged in (existing keys win). | ||
|
|
||
| Keys already present in the context are **not** overwritten — lower-priority | ||
| semantics matching the CSA spec: the first writer (outermost scope) wins. | ||
|
|
||
| Args: | ||
| attrs: Attributes to add to the context. | ||
| context: Base context to merge into. Defaults to the current context. | ||
|
|
||
| Returns: | ||
| A new Context containing the merged attributes. The caller is | ||
| responsible for attaching it if needed. | ||
| """ | ||
| ctx = context if context is not None else otel_context.get_current() | ||
| existing: dict[str, Any] = ( | ||
| otel_context.get_value(_GENAI_CONTEXT_ATTRS_KEY, context=ctx) or {} | ||
| ) | ||
| # Existing keys win — new attrs only fill in gaps. | ||
| merged = {**attrs, **existing} | ||
| return otel_context.set_value(_GENAI_CONTEXT_ATTRS_KEY, merged, ctx) | ||
|
|
||
|
|
||
| def get_context_scoped_attributes( | ||
| context: Context | None = None, | ||
| ) -> dict[str, Any]: | ||
| """Return context-scoped GenAI attributes, or an empty dict. | ||
|
|
||
| Args: | ||
| context: Context to read from. Defaults to the current context. | ||
|
|
||
| Returns: | ||
| A dict of attributes previously set via | ||
| :func:`set_context_scoped_attributes`, or ``{}`` if none are present. | ||
| """ | ||
| ctx = context if context is not None else otel_context.get_current() | ||
| return otel_context.get_value(_GENAI_CONTEXT_ATTRS_KEY, context=ctx) or {} | ||
78 changes: 78 additions & 0 deletions
78
util/opentelemetry-util-genai/tests/test_context_attributes.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,78 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from unittest import TestCase | ||
|
|
||
| from opentelemetry import context as otel_context | ||
| from opentelemetry.util.genai.context_attributes import ( | ||
| get_context_scoped_attributes, | ||
| set_context_scoped_attributes, | ||
| ) | ||
|
|
||
|
|
||
| class TestSetContextScopedAttributes(TestCase): | ||
| def test_returns_new_context(self) -> None: | ||
| original = otel_context.get_current() | ||
| new_ctx = set_context_scoped_attributes({"key": "value"}, original) | ||
| self.assertIsNot(new_ctx, original) | ||
|
|
||
| def test_values_readable_from_new_context(self) -> None: | ||
| ctx = set_context_scoped_attributes({"gen_ai.conversation_root": True}) | ||
| attrs = get_context_scoped_attributes(ctx) | ||
| self.assertEqual(attrs["gen_ai.conversation_root"], True) | ||
|
|
||
| def test_multiple_attributes_stored(self) -> None: | ||
| ctx = set_context_scoped_attributes({"a": "1", "b": "2"}) | ||
| attrs = get_context_scoped_attributes(ctx) | ||
| self.assertEqual(attrs["a"], "1") | ||
| self.assertEqual(attrs["b"], "2") | ||
|
|
||
| def test_existing_key_not_overwritten(self) -> None: | ||
| """Lower-priority semantics: a key already in context is not replaced.""" | ||
| ctx = set_context_scoped_attributes({"gen_ai.conversation_root": True}) | ||
| ctx2 = set_context_scoped_attributes( | ||
| {"gen_ai.conversation_root": False}, ctx | ||
| ) | ||
| attrs = get_context_scoped_attributes(ctx2) | ||
| # Original value wins | ||
| self.assertEqual(attrs["gen_ai.conversation_root"], True) | ||
|
|
||
| def test_new_key_added_alongside_existing(self) -> None: | ||
| ctx = set_context_scoped_attributes({"first": "a"}) | ||
| ctx2 = set_context_scoped_attributes({"second": "b"}, ctx) | ||
| attrs = get_context_scoped_attributes(ctx2) | ||
| self.assertEqual(attrs["first"], "a") | ||
| self.assertEqual(attrs["second"], "b") | ||
|
|
||
| def test_defaults_to_current_context(self) -> None: | ||
| ctx = set_context_scoped_attributes({"implicit": "yes"}) | ||
| token = otel_context.attach(ctx) | ||
| try: | ||
| attrs = get_context_scoped_attributes() | ||
| self.assertEqual(attrs["implicit"], "yes") | ||
| finally: | ||
| otel_context.detach(token) | ||
|
|
||
|
|
||
| class TestGetContextScopedAttributes(TestCase): | ||
| def test_empty_context_returns_empty_dict(self) -> None: | ||
| fresh_ctx = otel_context.get_current() | ||
| attrs = get_context_scoped_attributes(fresh_ctx) | ||
| self.assertEqual(attrs, {}) | ||
|
|
||
| def test_no_argument_uses_current_context(self) -> None: | ||
| ctx = set_context_scoped_attributes({"k": "v"}) | ||
| token = otel_context.attach(ctx) | ||
| try: | ||
| attrs = get_context_scoped_attributes() | ||
| self.assertEqual(attrs["k"], "v") | ||
| finally: | ||
| otel_context.detach(token) | ||
|
|
||
| def test_returns_same_dict_instance(self) -> None: | ||
| ctx = set_context_scoped_attributes({"x": "1"}) | ||
| attrs1 = get_context_scoped_attributes(ctx) | ||
| attrs2 = get_context_scoped_attributes(ctx) | ||
| self.assertIs(attrs1, attrs2) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might have missed the discussion for this but is this in genai semconv? If this is a prototype to drive semantic conventions, should this be gated/opt-in?