Repository navigation
feat: correlate telemetry spans with the conversation #7533
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
chenghao-mou
merged 2 commits into
main
from
chenghao/feat/AGT-3603-conversation-span-correlation
Oct 4, 2026
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
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,121 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable, Iterator | ||
| from contextlib import AbstractContextManager, nullcontext | ||
| from functools import partial | ||
| from types import MappingProxyType | ||
|
|
||
| import pytest | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
| from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
| from opentelemetry.trace import Span, SpanKind | ||
|
|
||
| from livekit.agents.telemetry import gen_ai, set_tracer_provider, tracer | ||
|
|
||
| pytestmark = [pytest.mark.unit, pytest.mark.no_concurrent] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def span_exporter() -> Iterator[InMemorySpanExporter]: | ||
| original_provider = tracer._tracer_provider | ||
| provider = TracerProvider() | ||
| exporter = InMemorySpanExporter() | ||
| provider.add_span_processor(SimpleSpanProcessor(exporter)) | ||
| set_tracer_provider(provider) | ||
| try: | ||
| yield exporter | ||
| finally: | ||
| set_tracer_provider(original_provider) | ||
| provider.shutdown() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("conversation_id", [None, "RM_test"]) | ||
| @pytest.mark.parametrize("explicit_id", [None, "explicit"]) | ||
| @pytest.mark.parametrize("api", ["start_span", "start_as_current_span", "detached_span"]) | ||
| def test_conversation_id_preserves_caller_attributes( | ||
| span_exporter: InMemorySpanExporter, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| conversation_id: str | None, | ||
| explicit_id: str | None, | ||
| api: str, | ||
| ) -> None: | ||
| monkeypatch.setattr(gen_ai, "_conversation_id", lambda: conversation_id) | ||
| attributes = {"test.attribute": "value"} | ||
| if explicit_id: | ||
| attributes["gen_ai.conversation.id"] = explicit_id | ||
| original = attributes.copy() | ||
| # The standard tracer APIs also allow positional, read-only attribute mappings. | ||
| if api == "start_span": | ||
| tracer.start_span("test", None, SpanKind.INTERNAL, MappingProxyType(attributes)).end() | ||
| elif api == "start_as_current_span": | ||
| with tracer.start_as_current_span( | ||
| "test", None, SpanKind.INTERNAL, MappingProxyType(attributes) | ||
| ): | ||
| pass | ||
| else: | ||
| with tracer.detached_span("test", attributes=attributes): | ||
| pass | ||
| assert attributes == original | ||
| expected = dict(original) | ||
| if explicit_id or conversation_id: | ||
| expected["gen_ai.conversation.id"] = explicit_id or conversation_id | ||
| assert dict(span_exporter.get_finished_spans()[0].attributes) == expected | ||
|
|
||
|
|
||
| def test_spans_without_attributes_receive_conversation_id( | ||
| span_exporter: InMemorySpanExporter, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| monkeypatch.setattr(gen_ai, "_conversation_id", lambda: "RM_test") | ||
| tracer.start_span("test").end() | ||
| with tracer.start_as_current_span("test"): | ||
| pass | ||
| with tracer.detached_span("test"): | ||
| pass | ||
| assert len(span_exporter.get_finished_spans()) == 3 | ||
| for span in span_exporter.get_finished_spans(): | ||
| assert span.attributes["gen_ai.conversation.id"] == "RM_test" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "set_attributes", | ||
| [ | ||
| pytest.param(partial(gen_ai.set_request_attributes, operation="chat"), id="request"), | ||
| pytest.param(partial(gen_ai.set_tool_attributes, name="get_weather"), id="tool"), | ||
| pytest.param( | ||
| partial(gen_ai.set_agent_attributes, operation="invoke_agent", agent_name="agent"), | ||
| id="agent", | ||
| ), | ||
| pytest.param(partial(gen_ai.set_workflow_attributes, name="agent_session"), id="workflow"), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize("explicit_id", [None, "explicit"]) | ||
| @pytest.mark.parametrize("conversation_id", [None, "RM_test"]) | ||
| @pytest.mark.parametrize("api", ["start_span", "start_as_current_span", "detached_span"]) | ||
| def test_gen_ai_setters_preserve_conversation_id( | ||
| span_exporter: InMemorySpanExporter, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| set_attributes: Callable[[Span], None], | ||
| explicit_id: str | None, | ||
| conversation_id: str | None, | ||
| api: str, | ||
| ) -> None: | ||
| monkeypatch.setattr(gen_ai, "_conversation_id", lambda: conversation_id) | ||
| attributes = {"gen_ai.conversation.id": explicit_id} if explicit_id is not None else {} | ||
| span_context: AbstractContextManager[Span] | ||
| if api == "start_span": | ||
| span_context = nullcontext(tracer.start_span("test", attributes=attributes)) | ||
| elif api == "start_as_current_span": | ||
| span_context = tracer.start_as_current_span("test", attributes=attributes) | ||
| else: | ||
| span_context = tracer.detached_span("test", attributes=attributes) | ||
|
|
||
| with span_context as span: | ||
| set_attributes(span) | ||
| monkeypatch.setattr(gen_ai, "_conversation_id", lambda: "RM_other") | ||
| set_attributes(span) | ||
| if api == "start_span": | ||
| span.end() | ||
|
|
||
| exported = span_exporter.get_finished_spans()[0] | ||
| assert exported.attributes.get("gen_ai.conversation.id") == (explicit_id or conversation_id) |
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
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.
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.
🟡 External GenAI spans lose conversation grouping
When an integrator calls
set_tool_attributeson a span from their own tracer, it no longer receives a conversation ID. Only the LiveKittracerinjects that ID, so external tool spans disappear from conversation-grouped views.Learn more
The exported GenAI attribute setters accept any OpenTelemetry span. Previously,
set_tool_attributesattached the room SID to a recording span even if another tracer created it. The LiveKit tracer now adds that attribute at span creation, but an SDK tracer obtained from a provider does not pass through that wrapper. The same loss affects the request, agent, and workflow setters when used with external spans. An integrator using the public setters with an SDK tracer therefore emits GenAI spans without conversation grouping.Example: Inside a job for room
RM_42, an integration createsspan = provider.get_tracer("custom").start_span("function_tool"), then callsgen_ai.set_tool_attributes(span, name="lookup"). The exported span hasgen_ai.tool.name, but nogen_ai.conversation.id; previously it carriedRM_42.Recommended fix: Preserve a fallback in all four GenAI setters for spans without an existing conversation ID. Ensure that fallback does not overwrite an explicit ID already present on a span created through the LiveKit tracer or supplied by its caller.
Was this helpful? React with 👍 or 👎 to provide feedback.
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.
external tracer will have to take care of conversation id themselves when choosing not to use ours