|
3 | 3 | import json |
4 | 4 | import sys |
5 | 5 | from pathlib import Path |
| 6 | +from types import SimpleNamespace |
| 7 | +from typing import Any |
6 | 8 |
|
7 | 9 | TESTS_ROOT = Path(__file__).resolve().parent |
8 | 10 | stub_path = TESTS_ROOT / "stubs" |
|
25 | 27 | from opentelemetry.instrumentation.openai_agents import ( # noqa: E402 |
26 | 28 | OpenAIAgentsInstrumentor, |
27 | 29 | ) |
| 30 | +from opentelemetry.instrumentation.openai_agents.genai_semantic_processor import ( # noqa: E402 |
| 31 | + ContentPayload, |
| 32 | + GenAISemanticProcessor, |
| 33 | +) |
28 | 34 | from opentelemetry.sdk.trace import TracerProvider # noqa: E402 |
29 | 35 |
|
30 | 36 | try: |
@@ -202,6 +208,160 @@ def test_agent_create_span_records_attributes(): |
202 | 208 | exporter.clear() |
203 | 209 |
|
204 | 210 |
|
| 211 | +def _placeholder_message() -> dict[str, Any]: |
| 212 | + return { |
| 213 | + "role": "user", |
| 214 | + "parts": [{"type": "text", "content": "readacted"}], |
| 215 | + } |
| 216 | + |
| 217 | + |
| 218 | +def test_normalize_messages_skips_empty_when_sensitive_enabled(): |
| 219 | + processor = GenAISemanticProcessor(metrics_enabled=False) |
| 220 | + normalized = processor._normalize_messages_to_role_parts( |
| 221 | + [{"role": "user", "content": None}] |
| 222 | + ) |
| 223 | + assert normalized == [] |
| 224 | + |
| 225 | + |
| 226 | +def test_normalize_messages_emits_placeholder_when_sensitive_disabled(): |
| 227 | + processor = GenAISemanticProcessor( |
| 228 | + include_sensitive_data=False, metrics_enabled=False |
| 229 | + ) |
| 230 | + normalized = processor._normalize_messages_to_role_parts( |
| 231 | + [{"role": "user", "content": None}] |
| 232 | + ) |
| 233 | + assert normalized == [_placeholder_message()] |
| 234 | + |
| 235 | + |
| 236 | +def test_agent_content_aggregation_skips_duplicate_snapshots(): |
| 237 | + processor = GenAISemanticProcessor(metrics_enabled=False) |
| 238 | + agent_id = "agent-span" |
| 239 | + processor._agent_content[agent_id] = { |
| 240 | + "input_messages": [], |
| 241 | + "output_messages": [], |
| 242 | + "system_instructions": [], |
| 243 | + } |
| 244 | + |
| 245 | + payload = ContentPayload( |
| 246 | + input_messages=[ |
| 247 | + {"role": "user", "parts": [{"type": "text", "content": "hello"}]}, |
| 248 | + { |
| 249 | + "role": "user", |
| 250 | + "parts": [{"type": "text", "content": "readacted"}], |
| 251 | + }, |
| 252 | + ] |
| 253 | + ) |
| 254 | + |
| 255 | + processor._update_agent_aggregate( |
| 256 | + SimpleNamespace(span_id="child-1", parent_id=agent_id, span_data=None), |
| 257 | + payload, |
| 258 | + ) |
| 259 | + processor._update_agent_aggregate( |
| 260 | + SimpleNamespace(span_id="child-2", parent_id=agent_id, span_data=None), |
| 261 | + payload, |
| 262 | + ) |
| 263 | + |
| 264 | + aggregated = processor._agent_content[agent_id]["input_messages"] |
| 265 | + assert aggregated == [ |
| 266 | + {"role": "user", "parts": [{"type": "text", "content": "hello"}]} |
| 267 | + ] |
| 268 | + # ensure data copied rather than reused to prevent accidental mutation |
| 269 | + assert aggregated is not payload.input_messages |
| 270 | + |
| 271 | + |
| 272 | +def test_agent_content_aggregation_filters_placeholder_append_when_sensitive(): |
| 273 | + processor = GenAISemanticProcessor(metrics_enabled=False) |
| 274 | + agent_id = "agent-span" |
| 275 | + processor._agent_content[agent_id] = { |
| 276 | + "input_messages": [], |
| 277 | + "output_messages": [], |
| 278 | + "system_instructions": [], |
| 279 | + } |
| 280 | + |
| 281 | + initial_payload = ContentPayload( |
| 282 | + input_messages=[ |
| 283 | + {"role": "user", "parts": [{"type": "text", "content": "hello"}]} |
| 284 | + ] |
| 285 | + ) |
| 286 | + processor._update_agent_aggregate( |
| 287 | + SimpleNamespace(span_id="child-1", parent_id=agent_id, span_data=None), |
| 288 | + initial_payload, |
| 289 | + ) |
| 290 | + |
| 291 | + placeholder_payload = ContentPayload( |
| 292 | + input_messages=[_placeholder_message()] |
| 293 | + ) |
| 294 | + processor._update_agent_aggregate( |
| 295 | + SimpleNamespace(span_id="child-2", parent_id=agent_id, span_data=None), |
| 296 | + placeholder_payload, |
| 297 | + ) |
| 298 | + |
| 299 | + aggregated = processor._agent_content[agent_id]["input_messages"] |
| 300 | + assert aggregated == [ |
| 301 | + {"role": "user", "parts": [{"type": "text", "content": "hello"}]} |
| 302 | + ] |
| 303 | + |
| 304 | + |
| 305 | +def test_agent_content_aggregation_retains_placeholder_when_sensitive_disabled(): |
| 306 | + processor = GenAISemanticProcessor( |
| 307 | + include_sensitive_data=False, metrics_enabled=False |
| 308 | + ) |
| 309 | + agent_id = "agent-span" |
| 310 | + processor._agent_content[agent_id] = { |
| 311 | + "input_messages": [], |
| 312 | + "output_messages": [], |
| 313 | + "system_instructions": [], |
| 314 | + } |
| 315 | + |
| 316 | + placeholder_payload = ContentPayload( |
| 317 | + input_messages=[_placeholder_message()] |
| 318 | + ) |
| 319 | + processor._update_agent_aggregate( |
| 320 | + SimpleNamespace(span_id="child-1", parent_id=agent_id, span_data=None), |
| 321 | + placeholder_payload, |
| 322 | + ) |
| 323 | + |
| 324 | + aggregated = processor._agent_content[agent_id]["input_messages"] |
| 325 | + assert aggregated == [_placeholder_message()] |
| 326 | + |
| 327 | + |
| 328 | +def test_agent_content_aggregation_appends_new_messages_once(): |
| 329 | + processor = GenAISemanticProcessor(metrics_enabled=False) |
| 330 | + agent_id = "agent-span" |
| 331 | + processor._agent_content[agent_id] = { |
| 332 | + "input_messages": [], |
| 333 | + "output_messages": [], |
| 334 | + "system_instructions": [], |
| 335 | + } |
| 336 | + |
| 337 | + initial_payload = ContentPayload( |
| 338 | + input_messages=[ |
| 339 | + {"role": "user", "parts": [{"type": "text", "content": "hello"}]} |
| 340 | + ] |
| 341 | + ) |
| 342 | + processor._update_agent_aggregate( |
| 343 | + SimpleNamespace(span_id="child-1", parent_id=agent_id, span_data=None), |
| 344 | + initial_payload, |
| 345 | + ) |
| 346 | + |
| 347 | + extended_messages = [ |
| 348 | + {"role": "user", "parts": [{"type": "text", "content": "hello"}]}, |
| 349 | + { |
| 350 | + "role": "assistant", |
| 351 | + "parts": [{"type": "text", "content": "hi there"}], |
| 352 | + }, |
| 353 | + ] |
| 354 | + extended_payload = ContentPayload(input_messages=extended_messages) |
| 355 | + processor._update_agent_aggregate( |
| 356 | + SimpleNamespace(span_id="child-2", parent_id=agent_id, span_data=None), |
| 357 | + extended_payload, |
| 358 | + ) |
| 359 | + |
| 360 | + aggregated = processor._agent_content[agent_id]["input_messages"] |
| 361 | + assert aggregated == extended_messages |
| 362 | + assert extended_payload.input_messages == extended_messages |
| 363 | + |
| 364 | + |
205 | 365 | def test_agent_span_collects_child_messages(): |
206 | 366 | instrumentor, exporter = _instrument_with_provider() |
207 | 367 |
|
|
0 commit comments