|
| 1 | +"""Tests for build_trace_context_headers.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from opentelemetry import trace |
| 7 | +from opentelemetry.sdk.trace import TracerProvider |
| 8 | +from opentelemetry.trace import NonRecordingSpan, SpanContext, TraceFlags |
| 9 | + |
| 10 | +from uipath.core.feature_flags import FeatureFlags |
| 11 | +from uipath.platform.chat.llm_trace_context import build_trace_context_headers |
| 12 | + |
| 13 | +FEATURE_FLAG = "EnableTraceContextHeaders" |
| 14 | + |
| 15 | + |
| 16 | +class TestFeatureFlagDisabled: |
| 17 | + """When the feature flag is off, no headers are returned.""" |
| 18 | + |
| 19 | + def setup_method(self) -> None: |
| 20 | + FeatureFlags.reset_flags() |
| 21 | + |
| 22 | + def test_returns_empty_dict_by_default(self) -> None: |
| 23 | + assert build_trace_context_headers() == {} |
| 24 | + |
| 25 | + def test_returns_empty_dict_when_explicitly_disabled(self) -> None: |
| 26 | + FeatureFlags.configure_flags({FEATURE_FLAG: False}) |
| 27 | + assert build_trace_context_headers() == {} |
| 28 | + |
| 29 | + |
| 30 | +class TestTraceparentHeader: |
| 31 | + """When enabled, x-uipath-traceparent-id is populated from the active span.""" |
| 32 | + |
| 33 | + def setup_method(self) -> None: |
| 34 | + FeatureFlags.reset_flags() |
| 35 | + FeatureFlags.configure_flags({FEATURE_FLAG: True}) |
| 36 | + |
| 37 | + def test_traceparent_from_active_span(self) -> None: |
| 38 | + provider = TracerProvider() |
| 39 | + tracer = provider.get_tracer("test") |
| 40 | + with tracer.start_as_current_span("test-span") as span: |
| 41 | + ctx = span.get_span_context() |
| 42 | + expected_trace_id = format(ctx.trace_id, "032x") |
| 43 | + expected_span_id = format(ctx.span_id, "016x") |
| 44 | + |
| 45 | + headers = build_trace_context_headers() |
| 46 | + |
| 47 | + assert "x-uipath-traceparent-id" in headers |
| 48 | + value = headers["x-uipath-traceparent-id"] |
| 49 | + assert value == f"00-{expected_trace_id}-{expected_span_id}" |
| 50 | + # Verify format: version (2) + dash + trace_id (32) + dash + span_id (16) |
| 51 | + parts = value.split("-") |
| 52 | + assert len(parts) == 3 |
| 53 | + assert parts[0] == "00" |
| 54 | + assert len(parts[1]) == 32 |
| 55 | + assert len(parts[2]) == 16 |
| 56 | + |
| 57 | + def test_no_traceparent_without_active_span(self) -> None: |
| 58 | + # INVALID_SPAN has trace_id=0 and span_id=0 |
| 59 | + ctx = SpanContext( |
| 60 | + trace_id=0, |
| 61 | + span_id=0, |
| 62 | + is_remote=False, |
| 63 | + trace_flags=TraceFlags(0), |
| 64 | + ) |
| 65 | + non_recording = NonRecordingSpan(ctx) |
| 66 | + token = trace.context_api.attach( |
| 67 | + trace.set_span_in_context(non_recording) |
| 68 | + ) |
| 69 | + try: |
| 70 | + headers = build_trace_context_headers() |
| 71 | + finally: |
| 72 | + trace.context_api.detach(token) |
| 73 | + |
| 74 | + assert "x-uipath-traceparent-id" not in headers |
| 75 | + |
| 76 | + |
| 77 | +class TestBaggageHeader: |
| 78 | + """When enabled, x-uipath-tracebaggage is populated from env vars.""" |
| 79 | + |
| 80 | + def setup_method(self) -> None: |
| 81 | + FeatureFlags.reset_flags() |
| 82 | + FeatureFlags.configure_flags({FEATURE_FLAG: True}) |
| 83 | + |
| 84 | + def test_all_env_vars_present(self) -> None: |
| 85 | + env = { |
| 86 | + "UIPATH_FOLDER_KEY": "folder-abc", |
| 87 | + "UIPATH_PROCESS_UUID": "agent-123", |
| 88 | + "UIPATH_PROCESS_KEY": "process-789", |
| 89 | + } |
| 90 | + with patch.dict(os.environ, env, clear=True): |
| 91 | + headers = build_trace_context_headers() |
| 92 | + |
| 93 | + baggage = headers["x-uipath-tracebaggage"] |
| 94 | + assert "source=agents" in baggage |
| 95 | + assert "folderKey=folder-abc" in baggage |
| 96 | + assert "agentId=agent-123" in baggage |
| 97 | + assert "processKey=process-789" in baggage |
| 98 | + |
| 99 | + def test_partial_env_vars(self) -> None: |
| 100 | + env = {"UIPATH_FOLDER_KEY": "folder-only"} |
| 101 | + with patch.dict(os.environ, env, clear=True): |
| 102 | + headers = build_trace_context_headers() |
| 103 | + |
| 104 | + baggage = headers["x-uipath-tracebaggage"] |
| 105 | + assert "source=agents" in baggage |
| 106 | + assert "folderKey=folder-only" in baggage |
| 107 | + |
| 108 | + def test_always_includes_source(self) -> None: |
| 109 | + with patch.dict(os.environ, {}, clear=True): |
| 110 | + headers = build_trace_context_headers() |
| 111 | + |
| 112 | + assert headers["x-uipath-tracebaggage"] == "source=agents" |
| 113 | + |
| 114 | + def test_baggage_comma_separated(self) -> None: |
| 115 | + env = { |
| 116 | + "UIPATH_FOLDER_KEY": "f1", |
| 117 | + "UIPATH_PROCESS_UUID": "a1", |
| 118 | + } |
| 119 | + with patch.dict(os.environ, env, clear=True): |
| 120 | + headers = build_trace_context_headers() |
| 121 | + |
| 122 | + baggage = headers["x-uipath-tracebaggage"] |
| 123 | + parts = baggage.split(",") |
| 124 | + assert len(parts) == 3 # source + folderKey + agentId |
| 125 | + |
| 126 | + |
| 127 | +class TestBothHeaders: |
| 128 | + """When enabled with an active span and env vars, both headers are present.""" |
| 129 | + |
| 130 | + def setup_method(self) -> None: |
| 131 | + FeatureFlags.reset_flags() |
| 132 | + FeatureFlags.configure_flags({FEATURE_FLAG: True}) |
| 133 | + |
| 134 | + def test_both_headers_present(self) -> None: |
| 135 | + provider = TracerProvider() |
| 136 | + tracer = provider.get_tracer("test") |
| 137 | + env = {"UIPATH_FOLDER_KEY": "folder-abc"} |
| 138 | + with ( |
| 139 | + tracer.start_as_current_span("test-span"), |
| 140 | + patch.dict(os.environ, env, clear=True), |
| 141 | + ): |
| 142 | + headers = build_trace_context_headers() |
| 143 | + |
| 144 | + assert "x-uipath-traceparent-id" in headers |
| 145 | + assert "x-uipath-tracebaggage" in headers |
0 commit comments