Skip to content

Commit 09caf6a

Browse files
committed
fix: enable content recording in OTel tests
Existing tests verify that content is attached to spans, so they need record_content=True. Add autouse fixture in conftest that sets the flag for the test suite while production default remains OFF.
1 parent 30d26b6 commit 09caf6a

File tree

5 files changed

+39
-26
lines changed

5 files changed

+39
-26
lines changed

autogen/opentelemetry/instrumentators/agent_instrumentators/chat.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
from opentelemetry.trace import Tracer
99

10+
import autogen.opentelemetry.instrumentators.agent_instrumentators._config as _otel_cfg
1011
from autogen import Agent
1112
from autogen.opentelemetry.consts import SpanType
12-
from autogen.opentelemetry.instrumentators.agent_instrumentators._config import RECORD_CONTENT
1313
from autogen.opentelemetry.utils import (
1414
aggregate_usage,
1515
get_model_name,
@@ -49,7 +49,7 @@ async def a_initiate_traced_chat(
4949
span.set_attribute("gen_ai.conversation.max_turns", max_turns)
5050

5151
# Capture input message
52-
if RECORD_CONTENT and message is not None:
52+
if _otel_cfg.RECORD_CONTENT and message is not None:
5353
if isinstance(message, str):
5454
input_msg = {"role": "user", "content": message}
5555
elif isinstance(message, dict):
@@ -67,7 +67,7 @@ async def a_initiate_traced_chat(
6767
span.set_attribute("gen_ai.conversation.turns", len(result.chat_history))
6868

6969
# Capture output messages (full chat history)
70-
if RECORD_CONTENT and result.chat_history:
70+
if _otel_cfg.RECORD_CONTENT and result.chat_history:
7171
otel_output = messages_to_otel(result.chat_history)
7272
span.set_attribute("gen_ai.output.messages", json.dumps(otel_output))
7373

@@ -115,7 +115,7 @@ def initiate_traced_chat(
115115
if max_turns:
116116
span.set_attribute("gen_ai.conversation.max_turns", max_turns)
117117

118-
if RECORD_CONTENT and message is not None:
118+
if _otel_cfg.RECORD_CONTENT and message is not None:
119119
if isinstance(message, str):
120120
input_msg = {"role": "user", "content": message}
121121
elif isinstance(message, dict):
@@ -132,7 +132,7 @@ def initiate_traced_chat(
132132
span.set_attribute("gen_ai.conversation.id", str(result.chat_id))
133133
span.set_attribute("gen_ai.conversation.turns", len(result.chat_history))
134134

135-
if RECORD_CONTENT and result.chat_history:
135+
if _otel_cfg.RECORD_CONTENT and result.chat_history:
136136
otel_output = messages_to_otel(result.chat_history)
137137
span.set_attribute("gen_ai.output.messages", json.dumps(otel_output))
138138

@@ -194,14 +194,14 @@ def run_chat_traced(
194194
span.set_attribute("gen_ai.agent.name", agent.name)
195195

196196
# Capture input messages
197-
if RECORD_CONTENT and messages:
197+
if _otel_cfg.RECORD_CONTENT and messages:
198198
otel_input = messages_to_otel(messages)
199199
span.set_attribute("gen_ai.input.messages", json.dumps(otel_input))
200200

201201
result = old_run_chat(messages=messages, sender=sender, config=config, **kwargs)
202202

203203
# Capture output messages from groupchat
204-
if RECORD_CONTENT and config and hasattr(config, "messages") and config.messages:
204+
if _otel_cfg.RECORD_CONTENT and config and hasattr(config, "messages") and config.messages:
205205
otel_output = messages_to_otel(config.messages)
206206
span.set_attribute("gen_ai.output.messages", json.dumps(otel_output))
207207

@@ -226,14 +226,14 @@ async def a_run_chat_traced(
226226
span.set_attribute("gen_ai.agent.name", agent.name)
227227

228228
# Capture input messages
229-
if RECORD_CONTENT and messages:
229+
if _otel_cfg.RECORD_CONTENT and messages:
230230
otel_input = messages_to_otel(messages)
231231
span.set_attribute("gen_ai.input.messages", json.dumps(otel_input))
232232

233233
result = await old_a_run_chat(messages=messages, sender=sender, config=config, **kwargs)
234234

235235
# Capture output messages from groupchat
236-
if RECORD_CONTENT and config and hasattr(config, "messages") and config.messages:
236+
if _otel_cfg.RECORD_CONTENT and config and hasattr(config, "messages") and config.messages:
237237
otel_output = messages_to_otel(config.messages)
238238
span.set_attribute("gen_ai.output.messages", json.dumps(otel_output))
239239

@@ -272,7 +272,7 @@ def initiate_chats_traced(chat_queue: list[dict[str, Any]]) -> list:
272272
span.set_attribute("ag2.chats.ids", json.dumps(chat_ids))
273273

274274
# Capture summaries
275-
if RECORD_CONTENT:
275+
if _otel_cfg.RECORD_CONTENT:
276276
summaries = [r.summary for r in results if hasattr(r, "summary")]
277277
span.set_attribute("ag2.chats.summaries", json.dumps(summaries))
278278

@@ -316,7 +316,7 @@ async def a_initiate_chats_traced(chat_queue: list[dict[str, Any]]) -> dict:
316316
span.set_attribute("ag2.chats.ids", json.dumps(chat_ids))
317317

318318
# Capture summaries (results is a dict for async version)
319-
if RECORD_CONTENT:
319+
if _otel_cfg.RECORD_CONTENT:
320320
summaries = [r.summary for r in results.values() if hasattr(r, "summary")]
321321
span.set_attribute("ag2.chats.summaries", json.dumps(summaries))
322322

autogen/opentelemetry/instrumentators/agent_instrumentators/human_input.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
from opentelemetry.trace import Tracer
88

9+
import autogen.opentelemetry.instrumentators.agent_instrumentators._config as _otel_cfg
910
from autogen import Agent
1011
from autogen.opentelemetry.consts import SpanType
11-
from autogen.opentelemetry.instrumentators.agent_instrumentators._config import RECORD_CONTENT
1212

1313

1414
def instrument_human_input(agent: Agent, *, tracer: Tracer) -> Agent:
@@ -25,12 +25,12 @@ def get_human_input_traced(
2525
span.set_attribute("ag2.span.type", SpanType.HUMAN_INPUT.value)
2626
span.set_attribute("gen_ai.operation.name", "await_human_input")
2727
span.set_attribute("gen_ai.agent.name", agent.name)
28-
if RECORD_CONTENT:
28+
if _otel_cfg.RECORD_CONTENT:
2929
span.set_attribute("ag2.human_input.prompt", prompt)
3030

3131
response = old_get_human_input(prompt, *args, **kwargs)
3232

33-
if RECORD_CONTENT:
33+
if _otel_cfg.RECORD_CONTENT:
3434
span.set_attribute("ag2.human_input.response", response)
3535
return response
3636

@@ -50,12 +50,12 @@ async def a_get_human_input_traced(
5050
span.set_attribute("ag2.span.type", SpanType.HUMAN_INPUT.value)
5151
span.set_attribute("gen_ai.operation.name", "await_human_input")
5252
span.set_attribute("gen_ai.agent.name", agent.name)
53-
if RECORD_CONTENT:
53+
if _otel_cfg.RECORD_CONTENT:
5454
span.set_attribute("ag2.human_input.prompt", prompt)
5555

5656
response = await old_a_get_human_input(prompt, *args, **kwargs)
5757

58-
if RECORD_CONTENT:
58+
if _otel_cfg.RECORD_CONTENT:
5959
span.set_attribute("ag2.human_input.response", response)
6060
return response
6161

autogen/opentelemetry/instrumentators/agent_instrumentators/reply.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from opentelemetry.context import Context
1212
from opentelemetry.trace import Tracer
1313

14+
import autogen.opentelemetry.instrumentators.agent_instrumentators._config as _otel_cfg
1415
from autogen import Agent
1516
from autogen.io import IOStream
1617
from autogen.opentelemetry.consts import SpanType
17-
from autogen.opentelemetry.instrumentators.agent_instrumentators._config import RECORD_CONTENT
1818
from autogen.opentelemetry.utils import (
1919
get_model_name,
2020
get_provider_name,
@@ -47,14 +47,14 @@ async def a_generate_traced_reply(
4747
span.set_attribute("gen_ai.request.model", model)
4848

4949
# Capture input messages
50-
if RECORD_CONTENT and messages:
50+
if _otel_cfg.RECORD_CONTENT and messages:
5151
otel_input = messages_to_otel(messages)
5252
span.set_attribute("gen_ai.input.messages", json.dumps(otel_input))
5353

5454
reply = await old_a_generate_reply(messages, *args, **kwargs)
5555

5656
# Capture output message
57-
if RECORD_CONTENT and reply is not None:
57+
if _otel_cfg.RECORD_CONTENT and reply is not None:
5858
otel_output = reply_to_otel_message(reply)
5959
span.set_attribute("gen_ai.output.messages", json.dumps(otel_output))
6060

@@ -85,13 +85,13 @@ def generate_traced_reply(
8585
if model:
8686
span.set_attribute("gen_ai.request.model", model)
8787

88-
if RECORD_CONTENT and messages:
88+
if _otel_cfg.RECORD_CONTENT and messages:
8989
otel_input = messages_to_otel(messages)
9090
span.set_attribute("gen_ai.input.messages", json.dumps(otel_input))
9191

9292
reply = old_generate_reply(messages, *args, **kwargs)
9393

94-
if RECORD_CONTENT and reply is not None:
94+
if _otel_cfg.RECORD_CONTENT and reply is not None:
9595
otel_output = reply_to_otel_message(reply)
9696
span.set_attribute("gen_ai.output.messages", json.dumps(otel_output))
9797

autogen/opentelemetry/instrumentators/agent_instrumentators/tool.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
from opentelemetry.trace import Tracer
99

10+
import autogen.opentelemetry.instrumentators.agent_instrumentators._config as _otel_cfg
1011
from autogen import Agent
1112
from autogen.opentelemetry.consts import SpanType
12-
from autogen.opentelemetry.instrumentators.agent_instrumentators._config import RECORD_CONTENT
1313

1414

1515
def instrument_execute_function(agent: Agent, *, tracer: Tracer) -> Agent:
@@ -32,7 +32,7 @@ def execute_function_traced(
3232
span.set_attribute("gen_ai.tool.call.id", call_id)
3333

3434
# Opt-in: Add tool call arguments (requires AG2_OTEL_RECORD_CONTENT=1)
35-
if RECORD_CONTENT:
35+
if _otel_cfg.RECORD_CONTENT:
3636
arguments = func_call.get("arguments", "{}")
3737
if isinstance(arguments, str):
3838
span.set_attribute("gen_ai.tool.call.arguments", arguments)
@@ -43,7 +43,7 @@ def execute_function_traced(
4343

4444
if not is_success:
4545
span.set_attribute("error.type", "ExecutionError")
46-
elif RECORD_CONTENT:
46+
elif _otel_cfg.RECORD_CONTENT:
4747
# Opt-in: Add tool call result (requires AG2_OTEL_RECORD_CONTENT=1)
4848
content = result.get("content", "")
4949
span.set_attribute("gen_ai.tool.call.result", str(content))
@@ -72,7 +72,7 @@ async def a_execute_function_traced(
7272
span.set_attribute("gen_ai.tool.call.id", call_id)
7373

7474
# Opt-in: Add tool call arguments (requires AG2_OTEL_RECORD_CONTENT=1)
75-
if RECORD_CONTENT:
75+
if _otel_cfg.RECORD_CONTENT:
7676
arguments = func_call.get("arguments", "{}")
7777
if isinstance(arguments, str):
7878
span.set_attribute("gen_ai.tool.call.arguments", arguments)
@@ -83,7 +83,7 @@ async def a_execute_function_traced(
8383

8484
if not is_success:
8585
span.set_attribute("error.type", "ExecutionError")
86-
elif RECORD_CONTENT:
86+
elif _otel_cfg.RECORD_CONTENT:
8787
# Opt-in: Add tool call result (requires AG2_OTEL_RECORD_CONTENT=1)
8888
content = result.get("content", "")
8989
span.set_attribute("gen_ai.tool.call.result", str(content))

test/opentelemetry/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,23 @@
77
import threading
88
from collections.abc import Sequence
99

10+
import pytest
1011
from opentelemetry.sdk.trace import ReadableSpan
1112
from opentelemetry.sdk.trace.export import SpanExportResult, SpanExporter
1213

1314

15+
@pytest.fixture(autouse=True)
16+
def _enable_otel_content_recording(monkeypatch: pytest.MonkeyPatch) -> None:
17+
"""Enable content recording for all OTel tests.
18+
19+
Production default is OFF (to prevent secret leakage). Tests need it ON
20+
to verify that content is correctly attached to spans.
21+
"""
22+
import autogen.opentelemetry.instrumentators.agent_instrumentators._config as _cfg
23+
24+
monkeypatch.setattr(_cfg, "RECORD_CONTENT", True)
25+
26+
1427
class InMemorySpanExporter(SpanExporter):
1528
"""Simple in-memory span exporter for tests.
1629

0 commit comments

Comments
 (0)