|
7 | 7 | from opentelemetry.sdk.trace import ReadableSpan |
8 | 8 | from opentelemetry.sdk.trace.export import SpanExportResult |
9 | 9 |
|
10 | | -from uipath.platform.common._span_utils import SpanStatus |
| 10 | +from uipath.platform.common._span_utils import SpanSource, SpanStatus |
11 | 11 | from uipath.platform.constants import ( |
12 | 12 | HEADER_INTERNAL_ACCOUNT_ID, |
13 | 13 | HEADER_INTERNAL_TENANT_ID, |
@@ -290,6 +290,77 @@ def test_build_url_uses_v3_endpoint(mock_env_vars): |
290 | 290 | assert "/api/Traces/spans" not in url.replace("/api/Traces/v3/spans", "") |
291 | 291 |
|
292 | 292 |
|
| 293 | +def test_build_url_uses_span_source_agents(mock_env_vars): |
| 294 | + """_build_url must render the span's Source (Agents), not the hardcoded CodedAgents.""" |
| 295 | + with patch("uipath.tracing._otel_exporters.httpx.Client"): |
| 296 | + exporter = LlmOpsHttpExporter() |
| 297 | + span_list = [{"TraceId": "ab" * 16, "Source": SpanSource.AGENTS}] |
| 298 | + url = exporter._build_url(span_list) |
| 299 | + assert "&source=Agents" in url |
| 300 | + assert "&source=CodedAgents" not in url |
| 301 | + assert "/api/Traces/v3/spans" in url |
| 302 | + |
| 303 | + |
| 304 | +def test_build_url_uses_span_source_coded_agents(mock_env_vars): |
| 305 | + """An explicit CodedAgents Source still renders CodedAgents.""" |
| 306 | + with patch("uipath.tracing._otel_exporters.httpx.Client"): |
| 307 | + exporter = LlmOpsHttpExporter() |
| 308 | + span_list = [{"TraceId": "ab" * 16, "Source": SpanSource.CODED_AGENTS}] |
| 309 | + url = exporter._build_url(span_list) |
| 310 | + assert "&source=CodedAgents" in url |
| 311 | + |
| 312 | + |
| 313 | +def test_build_url_defaults_to_coded_agents_when_source_missing(mock_env_vars): |
| 314 | + """When the span dict has no Source key, default to CodedAgents (back-compat).""" |
| 315 | + with patch("uipath.tracing._otel_exporters.httpx.Client"): |
| 316 | + exporter = LlmOpsHttpExporter() |
| 317 | + span_list = [{"TraceId": "ab" * 16}] |
| 318 | + url = exporter._build_url(span_list) |
| 319 | + assert "&source=CodedAgents" in url |
| 320 | + |
| 321 | + |
| 322 | +def test_agent_builder_span_yields_source_agents(mock_env_vars): |
| 323 | + """A span with uipath.source=1 must flow through to &source=Agents in the URL. |
| 324 | +
|
| 325 | + Drives a real span dict through otel_span_to_uipath_span().to_dict() rather |
| 326 | + than hand-building it, guarding the whole attribute->Source->URL path. |
| 327 | + """ |
| 328 | + from opentelemetry.trace import SpanContext, StatusCode |
| 329 | + |
| 330 | + from uipath.platform.common import _SpanUtils |
| 331 | + |
| 332 | + # otel_span_to_uipath_span reads the context via get_span_context() and |
| 333 | + # formats trace_id/span_id as hex, so provide a real SpanContext. |
| 334 | + span = MagicMock(spec=ReadableSpan) |
| 335 | + span.get_span_context.return_value = SpanContext( |
| 336 | + trace_id=0xABCDEF1234567890ABCDEF1234567890, |
| 337 | + span_id=0x1234567890ABCDEF, |
| 338 | + is_remote=False, |
| 339 | + ) |
| 340 | + span.parent = None |
| 341 | + span.name = "agent-span" |
| 342 | + span.status.status_code = StatusCode.OK |
| 343 | + span.status.description = None |
| 344 | + span.attributes = { |
| 345 | + "uipath.custom_instrumentation": True, |
| 346 | + "uipath.source": 1, # SourceEnum.Agents |
| 347 | + } |
| 348 | + span.events = [] |
| 349 | + span.links = [] |
| 350 | + span.start_time = 0 |
| 351 | + span.end_time = 1 |
| 352 | + |
| 353 | + span_dict = _SpanUtils.otel_span_to_uipath_span( |
| 354 | + span, serialize_attributes=False |
| 355 | + ).to_dict(serialize_attributes=False) |
| 356 | + assert str(span_dict["Source"]) == "Agents" |
| 357 | + |
| 358 | + with patch("uipath.tracing._otel_exporters.httpx.Client"): |
| 359 | + exporter = LlmOpsHttpExporter() |
| 360 | + url = exporter._build_url([span_dict]) |
| 361 | + assert "&source=Agents" in url |
| 362 | + |
| 363 | + |
293 | 364 | def test_determine_status_ok_returns_string(mock_env_vars): |
294 | 365 | with patch("uipath.tracing._otel_exporters.httpx.Client"): |
295 | 366 | exporter = LlmOpsHttpExporter() |
|
0 commit comments