Skip to content

Commit 911cabf

Browse files
committed
fix(ag2-classic): keep the processor first across add_span_processor
Future AGI's TracerProvider.add_span_processor shuts down and clears every processor on the first call after register() (fi_instrumentation/otel.py 336-339), including AG2ClassicSpanProcessor. Content then leaked to the new processor and conversation spans kept the promoted gen_ai.usage.* tokens. setup() now wraps add_span_processor on the provider instance once. After the original runs, our processor is re-enabled if that call shut it down and re-prepended ahead of every other processor. setup() finds the processor through the wrapper, so repeated setup() calls never stack a second guard or processor. README Privacy and Troubleshooting updated. approved: Nikhil 2026-10-03 blanket Refs: TH-8237
1 parent e4540e4 commit 911cabf

4 files changed

Lines changed: 152 additions & 15 deletions

File tree

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ summaries. With content off this package removes these keys before export:
132132
`output.value`. That is stricter than upstream. With content on they are kept
133133
and lifted into `input.value` / `output.value`.
134134

135+
The filtering lives in `AG2ClassicSpanProcessor`, which must run before any
136+
exporting processor. `setup()` keeps it first on the provider:
137+
138+
- `trace_provider.add_span_processor(...)` after `setup()`: Future AGI's
139+
provider shuts down and removes every processor on the first such call after
140+
`register()` (`fi_instrumentation/otel.py:336-339`), this one included.
141+
`setup()` wraps that method on your provider instance, so after the call this
142+
processor is put back first and re-enabled. Your processor sees filtered
143+
spans.
144+
- `tracing.uninstrument()` leaves the processor in place, because agents
145+
instrumented earlier keep emitting spans.
146+
- Editing `_active_span_processor._span_processors` yourself bypasses this;
147+
call `setup()` again to put the processor back first.
148+
135149
## Attribute inventory (autogen 0.14.1)
136150

137151
Paths are relative to `autogen/opentelemetry/`. Scope name
@@ -160,8 +174,9 @@ Paths are relative to `autogen/opentelemetry/`. Scope name
160174
`OpenAIWrapper.create` once and keeps the first provider
161175
(`llm_wrapper.py:64-65`). Call `uninstrument()` on the first handle first.
162176
- Calling `trace_provider.add_span_processor(...)` after `register()` drops
163-
Future AGI's default exporter (and this processor); that is
164-
`fi_instrumentation` behaviour. `setup()` does not use it.
177+
Future AGI's default exporter; that is `fi_instrumentation` behaviour.
178+
`setup()` does not use it, and puts this package's processor back first
179+
when you call it (see Privacy).
165180
- `setup()` raises and names three packages: you have Microsoft AutoGen, `ag2`
166181
1.x, or `pyautogen` instead of `autogen` 0.14.x.
167182
- Content missing: content is off by default here; pass `capture_content=True`.

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,63 @@ def test_register_then_setup_keeps_one_provider_resource_and_headers(monkeypatch
169169
signal.signal(sig, handler)
170170

171171

172+
def test_add_span_processor_after_register_and_setup_keeps_our_processor_first(monkeypatch, fake):
173+
"""fi's TracerProvider.add_span_processor shuts down and clears every
174+
processor, ours included (fi_instrumentation/otel.py:336-339). setup()
175+
re-prepends and re-enables ours so the new processor still sees filtered spans.
176+
"""
177+
from fi_instrumentation import register
178+
from fi_instrumentation.fi_types import ProjectType
179+
180+
monkeypatch.setenv("FI_API_KEY", "placeholder-api-key")
181+
monkeypatch.setenv("FI_SECRET_KEY", "placeholder-secret-key")
182+
monkeypatch.setenv("FI_BASE_URL", "http://127.0.0.1:9")
183+
saved = {sig: signal.getsignal(sig) for sig in (signal.SIGINT, signal.SIGTERM)}
184+
provider = register(
185+
project_type=ProjectType.OBSERVE,
186+
project_name="ag2-classic-add",
187+
verbose=False,
188+
set_global_tracer_provider=False,
189+
)
190+
tracing = setup(tracer_provider=provider)
191+
memory = InMemorySpanExporter()
192+
try:
193+
added = SimpleSpanProcessor(memory)
194+
provider.add_span_processor(added)
195+
processors = provider._active_span_processor._span_processors
196+
assert processors == (tracing.processor, added)
197+
assert tracing.processor._disabled is False
198+
199+
two_agent_tool_chat(tracing, fake)
200+
spans = memory.get_finished_spans()
201+
assert any(s.name == "conversation user" for s in spans)
202+
_assert_no_content_and_no_promoted_usage_off_llm(spans)
203+
conversation = next(s for s in spans if s.name == "conversation user").attributes
204+
assert conversation["gen_ai.span.kind"] == "CHAIN"
205+
206+
# A second add keeps exactly one copy of ours, still first.
207+
second = SimpleSpanProcessor(InMemorySpanExporter())
208+
provider.add_span_processor(second)
209+
assert provider._active_span_processor._span_processors == (tracing.processor, added, second)
210+
finally:
211+
tracing.uninstrument()
212+
provider.shutdown()
213+
for sig, handler in saved.items():
214+
signal.signal(sig, handler)
215+
216+
217+
def test_add_span_processor_on_a_plain_sdk_provider_keeps_one_processor_first(pipeline):
218+
provider, _exporter, make = pipeline
219+
first = make()
220+
make() # a second setup() must not stack a second guard or processor
221+
extra = SimpleSpanProcessor(InMemorySpanExporter())
222+
provider.add_span_processor(extra)
223+
processors = provider._active_span_processor._span_processors
224+
assert processors[0] is first.processor
225+
assert processors[-1] is extra
226+
assert sum(isinstance(p, AG2ClassicSpanProcessor) for p in processors) == 1
227+
228+
172229
def test_setup_is_idempotent_per_provider_and_uninstrument_restores(pipeline):
173230
from autogen.oai.client import OpenAIWrapper
174231

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ def shutdown(self) -> None:
310310
except Exception: # pragma: no cover - never break SDK shutdown
311311
pass
312312

313+
def _reopen(self) -> None:
314+
"""Undo :meth:`shutdown` after Future AGI's ``add_span_processor`` ran.
315+
316+
That method shuts down every processor on the provider, not only its
317+
own default exporter; ``setup()`` re-installs this one and calls this.
318+
"""
319+
self._disabled = False
320+
313321
def force_flush(self, timeout_millis: int = 30000) -> bool:
314322
return True
315323

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

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,81 @@ def _install_processor(provider: Any, capture_content: bool) -> AG2ClassicSpanPr
5151
that the ``error.type`` -> ERROR promotion runs before Future AGI's
5252
exporting processor turns every UNSET status into OK
5353
(``fi_instrumentation/otel.py`` ``BatchSpanProcessor.on_end``).
54+
55+
The provider instance's ``add_span_processor`` is wrapped once (see
56+
:func:`_guard_add_span_processor`) so a later call by the user keeps this
57+
processor installed, first and enabled.
5458
"""
5559
active = _active_multi_processor(provider)
56-
existing = tuple(active._span_processors)
57-
for processor in existing:
58-
if isinstance(processor, AG2ClassicSpanProcessor):
59-
if processor.capture_content != bool(capture_content):
60-
logger.warning(
61-
"traceai-ag2-classic is already installed on this provider with "
62-
"capture_content=%s; keeping that setting.",
63-
processor.capture_content,
64-
)
65-
return processor
66-
processor = AG2ClassicSpanProcessor(capture_content=capture_content)
67-
with getattr(active, "_lock", _NullLock()):
68-
active._span_processors = (processor,) + tuple(active._span_processors)
60+
processor = getattr(getattr(provider, "add_span_processor", None), _GUARD_ATTR, None)
61+
if not isinstance(processor, AG2ClassicSpanProcessor):
62+
processor = next(
63+
(p for p in tuple(active._span_processors) if isinstance(p, AG2ClassicSpanProcessor)),
64+
None,
65+
)
66+
if processor is not None:
67+
if processor.capture_content != bool(capture_content):
68+
logger.warning(
69+
"traceai-ag2-classic is already installed on this provider with "
70+
"capture_content=%s; keeping that setting.",
71+
processor.capture_content,
72+
)
73+
if processor not in tuple(active._span_processors):
74+
_prepend(active, processor)
75+
else:
76+
processor = AG2ClassicSpanProcessor(capture_content=capture_content)
77+
_prepend(active, processor)
78+
_guard_add_span_processor(provider, processor)
6979
return processor
7080

7181

82+
# Attribute on our add_span_processor wrapper; holds the provider's processor.
83+
_GUARD_ATTR = "_traceai_ag2_classic_processor"
84+
85+
86+
def _prepend(active: Any, processor: AG2ClassicSpanProcessor) -> None:
87+
with getattr(active, "_lock", _NullLock()):
88+
rest = tuple(p for p in active._span_processors if p is not processor)
89+
active._span_processors = (processor,) + rest
90+
91+
92+
def _guard_add_span_processor(provider: Any, processor: AG2ClassicSpanProcessor) -> None:
93+
"""Keep ``processor`` first and enabled across ``provider.add_span_processor``.
94+
95+
Future AGI's ``TracerProvider.add_span_processor`` shuts down and clears
96+
every processor while ``register()``'s default exporter is still in place
97+
(``fi_instrumentation/otel.py:336-339``), ours included. Without this,
98+
content would be exported and conversation spans would keep the promoted
99+
token keys after a user adds their own processor. Only this provider
100+
instance is wrapped, once.
101+
"""
102+
current = getattr(provider, "add_span_processor", None)
103+
if current is None or getattr(current, _GUARD_ATTR, None) is not None:
104+
return
105+
106+
def add_span_processor(*args: Any, **kwargs: Any) -> Any:
107+
was_enabled = not processor._disabled
108+
result = current(*args, **kwargs)
109+
try:
110+
if was_enabled and processor._disabled:
111+
processor._reopen()
112+
_prepend(_active_multi_processor(provider), processor)
113+
except Exception: # pragma: no cover - never break the caller's add
114+
logger.warning("traceai-ag2-classic could not re-install its span processor.", exc_info=True)
115+
return result
116+
117+
setattr(add_span_processor, _GUARD_ATTR, processor)
118+
add_span_processor.__doc__ = getattr(current, "__doc__", None)
119+
try:
120+
provider.add_span_processor = add_span_processor
121+
except Exception: # pragma: no cover - provider without instance attributes
122+
logger.warning(
123+
"traceai-ag2-classic could not guard %s.add_span_processor; calling it after setup() "
124+
"may remove this package's span processor.",
125+
type(provider).__name__,
126+
)
127+
128+
72129
class _NullLock:
73130
def __enter__(self) -> None:
74131
return None

0 commit comments

Comments
 (0)