Skip to content

Commit a383f20

Browse files
committed
fix(ag2-classic): apply using_attributes context to AG2 spans
Upstream spans come from a plain OpenTelemetry tracer; only Future AGI's own tracer applies context attributes (fi_instrumentation/instrumentation/ _tracers.py:165). using_session / using_user / using_attributes were therefore ignored: session.id was the chat id and user.id was absent. on_start now copies get_attributes_from_context() (read from the span's parent context) onto AG2 spans, without overwriting keys upstream already set. A context session.id is on the span before it ends, so it wins over the gen_ai.conversation.id copy; the conversation span keeps the real chat id in gen_ai.conversation.id. initiate_chats keeps one session per outermost conversation and no session on the multi_conversation parent. The README Session row now says so and points to using_session(...) for one session over the whole call; tests cover both. approved: Nikhil 2026-10-03 blanket Refs: TH-8237
1 parent 5805ca0 commit a383f20

4 files changed

Lines changed: 152 additions & 7 deletions

File tree

‎python/frameworks/ag2-classic/README.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,19 @@ provider and shuts down with `trace_provider.shutdown()`.
100100
## Conformance
101101

102102
Every row was read from `autogen/opentelemetry/instrumentators/` at 0.14.1.
103-
LLM, agent, tool, conversation, group-chat speaker selection and pattern spans
104-
are exercised by real `autogen` runs against a loopback fake model in
105-
`tests/`; code execution, human input, `initiate_chats` and remote-agent spans
106-
are covered by mapping unit tests only.
103+
LLM, agent, tool, conversation, group-chat speaker selection, pattern and
104+
`initiate_chats` spans are exercised by real `autogen` runs against a loopback
105+
fake model in `tests/`; code execution, human input and remote-agent spans are
106+
covered by mapping unit tests only.
107107

108108
| Field | What you get | When it is omitted |
109109
|---|---|---|
110110
| Span kind | `gen_ai.span.kind` from upstream `ag2.span.type`: `llm` → `LLM`; `agent` → `AGENT`; `tool`, `code_execution` → `TOOL`; `conversation`, `multi_conversation`, `speaker_selection`, `human_input` → `CHAIN`; `handoff` → `CHAIN` (declared upstream as TODO, never emitted at 0.14.x). `ag2.span.type` and `gen_ai.operation.name` are kept. | The `a2a-execution` span from `instrument_a2a_server` has no `ag2.span.type` and gets no kind. |
111111
| Model, provider | `gen_ai.request.model`, `gen_ai.provider.name` on LLM, agent and conversation spans; `gen_ai.response.model` on LLM spans. Upstream keys, passed through. | Agents without an LLM config carry no model or provider. |
112112
| Tokens | `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens` on LLM spans (from the response `usage`), plus `gen_ai.usage.total_tokens` added here. | A response without `usage` gives no token keys. Conversation spans carry upstream's aggregate input/output tokens for the whole chat. Future AGI sums tokens over every span in a trace, so those move to `ag2.usage.input_tokens` / `ag2.usage.output_tokens`; each LLM call counts once. |
113113
| Cost | `gen_ai.cost.total` on LLM spans, copied from upstream `gen_ai.usage.cost` (AG2's own price table). | Conversation spans keep only upstream `gen_ai.usage.cost` (aggregate). |
114-
| Session | `session.id` from upstream `gen_ai.conversation.id` (`ChatResult.chat_id`) on the outermost `conversation` span. | Nested chats (for example the internal chat behind group-chat speaker selection) keep `gen_ai.conversation.id` but get no `session.id`. Child spans end before the chat id exists, so only the conversation span carries it. A `GroupChatManager` run emits no own conversation span at 0.14.x. |
115-
| User | Not emitted upstream. | Always. |
114+
| Session | `session.id` from upstream `gen_ai.conversation.id` (`ChatResult.chat_id`) on the outermost `conversation` span. Inside `using_session(...)` or `using_attributes(session_id=...)` (from `fi_instrumentation`), that id is set on every AG2 span instead and wins over the chat id; `gen_ai.conversation.id` keeps the chat id. | Nested chats (for example the internal chat behind group-chat speaker selection, or an inner chat a tool starts) keep `gen_ai.conversation.id` but get no `session.id`. Child spans end before the chat id exists, so only the conversation span carries it. A `GroupChatManager` run emits no own conversation span at 0.14.x. `initiate_chats` gives one session per chat: each chat is its own outermost conversation, and the parent `initiate_chats` span (`multi_conversation`) has no `session.id`, so one trace holds several sessions. For one session over the whole call, wrap it in `with using_session("..."):`. |
115+
| User | `user.id` (and `metadata`, `tag.tags`) from `using_user(...)` / `using_attributes(...)`, set on every AG2 span started inside it. | Not emitted upstream; absent without that context. |
116116
| Tools | `gen_ai.tool.name`, `gen_ai.tool.type` (`function`), `gen_ai.tool.call.id`. | Arguments and result (`gen_ai.tool.call.arguments` / `.result`) only with `capture_content=True`. |
117117
| Errors | Exceptions keep OTel's ERROR status. A failing tool is caught inside `ConversableAgent.execute_function`, so upstream only sets `error.type=ExecutionError`; this package sets status ERROR from `error.type`. A non-zero code exit (`error.type=CodeExecutionError`) also becomes ERROR. | |
118118
| Retrieval | Not emitted upstream. | Always. |

‎python/frameworks/ag2-classic/tests/test_processor.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,50 @@ def test_live_span_tracking_is_bounded(monkeypatch):
295295
assert len(exporter.get_finished_spans()) == 3
296296

297297

298+
# Context attributes (using_attributes / using_session / using_user) ------------
299+
300+
301+
def test_context_attributes_are_copied_onto_ag2_spans():
302+
from fi_instrumentation import using_attributes
303+
304+
provider, exporter = _pipeline()
305+
ag2 = provider.get_tracer(AG2_SCOPE)
306+
other = provider.get_tracer("someone.else")
307+
with using_attributes(session_id="my-session", user_id="u-1", metadata={"k": "v"}, tags=["t1"]):
308+
with ag2.start_as_current_span("conversation c") as conversation:
309+
conversation.set_attribute("ag2.span.type", "conversation")
310+
with ag2.start_as_current_span("chat m") as llm:
311+
llm.set_attribute("ag2.span.type", "llm")
312+
conversation.set_attribute("gen_ai.conversation.id", "chat-1")
313+
with other.start_as_current_span("x"):
314+
pass
315+
by_name = {s.name: s.attributes for s in exporter.get_finished_spans()}
316+
for name in ("conversation c", "chat m"):
317+
assert by_name[name]["session.id"] == "my-session", name
318+
assert by_name[name]["user.id"] == "u-1", name
319+
assert json.loads(by_name[name]["metadata"]) == {"k": "v"}, name
320+
assert tuple(by_name[name]["tag.tags"]) == ("t1",), name
321+
# The user's session wins over the upstream chat id; the chat id is kept.
322+
assert by_name["conversation c"]["gen_ai.conversation.id"] == "chat-1"
323+
# Spans from other instrumentations are left to their own tracer.
324+
assert "session.id" not in by_name["x"] and "user.id" not in by_name["x"]
325+
326+
327+
def test_context_attributes_do_not_overwrite_upstream_values():
328+
from fi_instrumentation import using_session
329+
330+
provider, exporter = _pipeline()
331+
ag2 = provider.get_tracer(AG2_SCOPE)
332+
with using_session("my-session"):
333+
with ag2.start_as_current_span(
334+
"conversation c", attributes={"ag2.span.type": "conversation", "gen_ai.conversation.id": "chat-1"}
335+
):
336+
pass
337+
attrs = exporter.get_finished_spans()[0].attributes
338+
assert attrs["session.id"] == "my-session"
339+
assert attrs["gen_ai.conversation.id"] == "chat-1"
340+
341+
298342
def test_processor_preserves_bounded_attribute_limits():
299343
from opentelemetry.attributes import BoundedAttributes
300344

‎python/frameworks/ag2-classic/tests/test_setup.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,64 @@ def ask_expert(city: str) -> str:
484484
assert "session.id" not in inner.attributes
485485

486486

487+
# Context attributes and initiate_chats sessions -----------------------------------
488+
489+
490+
def test_using_attributes_sets_session_and_user_on_ag2_spans(pipeline, fake):
491+
from fi_instrumentation import using_attributes
492+
493+
_provider, exporter, make = pipeline
494+
tracing = make()
495+
with using_attributes(session_id="my-session", user_id="u-1"):
496+
result = two_agent_tool_chat(tracing, fake)
497+
finished = exporter.get_finished_spans()
498+
assert {s.name for s in finished} >= {"conversation user", "chat {0}".format(FAKE_MODEL), "execute_tool get_weather"}
499+
for span in finished:
500+
assert span.attributes["session.id"] == "my-session", span.name
501+
assert span.attributes["user.id"] == "u-1", span.name
502+
conversation = next(s for s in finished if s.name == "conversation user").attributes
503+
assert conversation["gen_ai.conversation.id"] == str(result.chat_id)
504+
505+
506+
def _initiate_chats(tracing, fake):
507+
from autogen import ConversableAgent
508+
509+
first = ConversableAgent("first", llm_config=fake.llm_config(), human_input_mode="NEVER")
510+
second = ConversableAgent("second", llm_config=fake.llm_config(), human_input_mode="NEVER")
511+
sender = ConversableAgent("sender", llm_config=False, human_input_mode="NEVER")
512+
for agent in (first, second, sender):
513+
tracing.instrument_agent(agent)
514+
return sender.initiate_chats(
515+
[
516+
{"recipient": first, "message": "one", "max_turns": 1, "silent": True},
517+
{"recipient": second, "message": "two", "max_turns": 1, "silent": True},
518+
]
519+
)
520+
521+
522+
def test_initiate_chats_has_one_session_per_chat_by_default(pipeline, fake):
523+
_provider, exporter, make = pipeline
524+
results = _initiate_chats(make(), fake)
525+
finished = exporter.get_finished_spans()
526+
assert len({s.context.trace_id for s in finished}) == 1
527+
sessions = {s.attributes.get("session.id") for s in finished} - {None}
528+
assert sessions == {str(r.chat_id) for r in results} and len(sessions) == 2
529+
multi = next(s for s in finished if s.attributes.get("ag2.span.type") == "multi_conversation")
530+
assert "session.id" not in multi.attributes
531+
532+
533+
def test_using_session_gives_initiate_chats_one_session(pipeline, fake):
534+
from fi_instrumentation import using_session
535+
536+
_provider, exporter, make = pipeline
537+
tracing = make()
538+
with using_session("batch-1"):
539+
_initiate_chats(tracing, fake)
540+
finished = exporter.get_finished_spans()
541+
assert any(s.attributes.get("ag2.span.type") == "multi_conversation" for s in finished)
542+
assert {s.attributes.get("session.id") for s in finished} == {"batch-1"}
543+
544+
487545
def test_patterns_are_passed_to_upstream_instrument_pattern(pipeline, fake):
488546
from autogen import ConversableAgent
489547
from autogen.agentchat import initiate_group_chat

‎python/frameworks/ag2-classic/traceai_ag2_classic/_processor.py‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
selection, or an inner chat a tool starts inside the user's own span) get
1616
their own ``chat_id`` upstream; those are not copied, so one run maps to one
1717
session. The ancestor walk follows every live span, AG2 or not.
18+
* Future AGI context attributes (``using_session``, ``using_user``,
19+
``using_attributes``: ``session.id``, ``user.id``, ``metadata``, ...) copied
20+
onto AG2 spans when they start; a context ``session.id`` wins over the
21+
conversation id.
1822
* ``gen_ai.cost.total`` from upstream ``gen_ai.usage.cost`` on LLM spans, and
1923
``gen_ai.usage.total_tokens`` = input + output on LLM spans.
2024
* ``gen_ai.request.parameters`` JSON from upstream ``gen_ai.request.*``
@@ -40,11 +44,13 @@
4044
import threading
4145
from typing import Any, Dict, Mapping, Optional, Tuple
4246

47+
from opentelemetry import context as context_api
4348
from opentelemetry.context import Context
4449
from opentelemetry.sdk.trace import ReadableSpan, Span, SpanProcessor
4550
from opentelemetry.trace import Status, StatusCode
4651

4752
from fi_instrumentation.fi_types import FiMimeTypeValues, FiSpanKindValues, SpanAttributes
53+
from fi_instrumentation import get_attributes_from_context
4854

4955
logger = logging.getLogger(__name__)
5056

@@ -279,11 +285,14 @@ def on_start(self, span: Span, parent_context: Optional[Context] = None) -> None
279285
if self._disabled:
280286
return
281287
try:
288+
ag2 = _is_ag2(span)
282289
parent = span.parent
283-
entry = (parent.span_id if parent is not None else None, span if _is_ag2(span) else None)
290+
entry = (parent.span_id if parent is not None else None, span if ag2 else None)
284291
with self._lock:
285292
if len(self._live) < _MAX_LIVE_SPANS:
286293
self._live[span.context.span_id] = entry
294+
if ag2:
295+
_apply_context_attributes(span, parent_context)
287296
except Exception: # pragma: no cover - never break the SDK
288297
return
289298

@@ -353,3 +362,37 @@ def _has_live_conversation_ancestor(self, span: ReadableSpan) -> bool:
353362
def _is_ag2(span: Any) -> bool:
354363
scope = getattr(span, "instrumentation_scope", None)
355364
return getattr(scope, "name", None) == AG2_SCOPE
365+
366+
367+
def _context_attributes(parent_context: Optional[Context]) -> Dict[str, Any]:
368+
"""Return Future AGI context attributes (``using_attributes`` & co.).
369+
370+
Upstream spans come from a plain OpenTelemetry tracer, which never reads
371+
these; only Future AGI's own tracer applies them
372+
(``fi_instrumentation/instrumentation/_tracers.py:165``).
373+
"""
374+
if parent_context is None:
375+
return dict(get_attributes_from_context())
376+
token = context_api.attach(parent_context)
377+
try:
378+
return dict(get_attributes_from_context())
379+
finally:
380+
context_api.detach(token)
381+
382+
383+
def _apply_context_attributes(span: Span, parent_context: Optional[Context]) -> None:
384+
"""Copy context attributes onto a starting AG2 span.
385+
386+
Keys upstream already set are left alone. A ``session.id`` from
387+
``using_session`` / ``using_attributes`` is therefore on the span before
388+
it ends, and wins over the ``gen_ai.conversation.id`` copy in
389+
:func:`map_ag2_attributes`. Upstream sets the conversation span's
390+
``gen_ai.conversation.id`` later, so it keeps the real chat id.
391+
"""
392+
values = _context_attributes(parent_context)
393+
if not values:
394+
return
395+
existing = span.attributes or {}
396+
missing = {key: value for key, value in values.items() if key not in existing}
397+
if missing:
398+
span.set_attributes(missing)

0 commit comments

Comments
 (0)