|
10 | 10 | from uipath.platform.common import UiPathSpan, _SpanUtils |
11 | 11 |
|
12 | 12 |
|
| 13 | +class TestOTelToUiPathSpan: |
| 14 | + """OTEL attribute -> top-level UiPathSpan field mapping. |
| 15 | +
|
| 16 | + `_SpanUtils.otel_span_to_uipath_span` lifts a small set of OTEL |
| 17 | + span attributes onto dedicated `UiPathSpan` fields surfaced under |
| 18 | + `to_dict()`. This test documents that mapping — adding a new row |
| 19 | + means the attribute is newly mapped, removing one breaks |
| 20 | + downstream consumers. |
| 21 | + """ |
| 22 | + |
| 23 | + ATTRIBUTE_FIELD_MAP = [ |
| 24 | + ("executionType", "execution_type", "ExecutionType", 1), |
| 25 | + ("agentVersion", "agent_version", "AgentVersion", "1.2.3"), |
| 26 | + ("agentId", "reference_id", "ReferenceId", "ref-abc"), |
| 27 | + ("verbosityLevel", "verbosity_level", "VerbosityLevel", 6), |
| 28 | + ] |
| 29 | + |
| 30 | + @patch.dict(os.environ, {"UIPATH_ORGANIZATION_ID": "test-org"}) |
| 31 | + def test_attributes_map_to_top_level_fields(self) -> None: |
| 32 | + attrs = { |
| 33 | + otel_attr: value for otel_attr, _, _, value in self.ATTRIBUTE_FIELD_MAP |
| 34 | + } |
| 35 | + |
| 36 | + mock_span = Mock(spec=OTelSpan) |
| 37 | + mock_context = SpanContext( |
| 38 | + trace_id=0x123456789ABCDEF0123456789ABCDEF0, |
| 39 | + span_id=0x0123456789ABCDEF, |
| 40 | + is_remote=False, |
| 41 | + ) |
| 42 | + mock_span.get_span_context.return_value = mock_context |
| 43 | + mock_span.name = "test-span" |
| 44 | + mock_span.parent = None |
| 45 | + mock_span.status.status_code = StatusCode.OK |
| 46 | + mock_span.attributes = attrs |
| 47 | + mock_span.events = [] |
| 48 | + mock_span.links = [] |
| 49 | + now_ns = int(datetime.now().timestamp() * 1e9) |
| 50 | + mock_span.start_time = now_ns |
| 51 | + mock_span.end_time = now_ns + 1_000_000 |
| 52 | + |
| 53 | + uipath_span = _SpanUtils.otel_span_to_uipath_span(mock_span) |
| 54 | + span_dict = uipath_span.to_dict() |
| 55 | + |
| 56 | + for _, span_field, top_level_key, value in self.ATTRIBUTE_FIELD_MAP: |
| 57 | + assert getattr(uipath_span, span_field) == value, span_field |
| 58 | + assert span_dict[top_level_key] == value, top_level_key |
| 59 | + |
| 60 | + @patch.dict(os.environ, {"UIPATH_ORGANIZATION_ID": "test-org"}) |
| 61 | + def test_verbosity_level_omitted_when_unset(self) -> None: |
| 62 | + """Spans that don't set verbosityLevel must not carry the key on the wire. |
| 63 | +
|
| 64 | + Backwards compat: pre-existing spans never emitted VerbosityLevel; the |
| 65 | + LLMOps backend applies its own default. Adding `"VerbosityLevel": null` |
| 66 | + unconditionally would change the wire format for every existing span. |
| 67 | + """ |
| 68 | + mock_span = Mock(spec=OTelSpan) |
| 69 | + mock_context = SpanContext( |
| 70 | + trace_id=0x123456789ABCDEF0123456789ABCDEF0, |
| 71 | + span_id=0x0123456789ABCDEF, |
| 72 | + is_remote=False, |
| 73 | + ) |
| 74 | + mock_span.get_span_context.return_value = mock_context |
| 75 | + mock_span.name = "legacy-span" |
| 76 | + mock_span.parent = None |
| 77 | + mock_span.status.status_code = StatusCode.OK |
| 78 | + mock_span.attributes = {"someOtherAttr": "value"} |
| 79 | + mock_span.events = [] |
| 80 | + mock_span.links = [] |
| 81 | + now_ns = int(datetime.now().timestamp() * 1e9) |
| 82 | + mock_span.start_time = now_ns |
| 83 | + mock_span.end_time = now_ns + 1_000_000 |
| 84 | + |
| 85 | + uipath_span = _SpanUtils.otel_span_to_uipath_span(mock_span) |
| 86 | + span_dict = uipath_span.to_dict() |
| 87 | + |
| 88 | + assert uipath_span.verbosity_level is None |
| 89 | + assert "VerbosityLevel" not in span_dict |
| 90 | + |
| 91 | + |
13 | 92 | class TestNormalizeIds: |
14 | 93 | """Tests for OTEL ID normalization functions.""" |
15 | 94 |
|
|
0 commit comments