|
| 1 | +"""Tests for BaggageSpanProcessor.""" |
| 2 | + |
| 3 | +from typing import Any, Dict |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +from opentelemetry import baggage, context |
| 7 | + |
| 8 | +from sap_cloud_sdk.core.telemetry.span_processors.baggage_span_processor import ( |
| 9 | + ALLOW_ALL_BAGGAGE_KEYS, |
| 10 | + BaggageSpanProcessor, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def _make_context_with_baggage(entries: Dict[str, Any]): |
| 15 | + ctx = context.get_current() |
| 16 | + for k, v in entries.items(): |
| 17 | + ctx = baggage.set_baggage(k, v, context=ctx) |
| 18 | + return ctx |
| 19 | + |
| 20 | + |
| 21 | +def _make_recording_span(span_id: int = 1): |
| 22 | + span = MagicMock() |
| 23 | + span.is_recording.return_value = True |
| 24 | + span.context.span_id = span_id |
| 25 | + return span |
| 26 | + |
| 27 | + |
| 28 | +def _make_ended_span(span_id: int = 1, attributes: Dict[str, Any] | None = None): |
| 29 | + span = MagicMock() |
| 30 | + span.context.span_id = span_id |
| 31 | + span._attributes = dict(attributes or {}) |
| 32 | + return span |
| 33 | + |
| 34 | + |
| 35 | +class TestBaggageSpanProcessorOnStart: |
| 36 | + def setup_method(self): |
| 37 | + self.processor = BaggageSpanProcessor(ALLOW_ALL_BAGGAGE_KEYS) |
| 38 | + |
| 39 | + def test_copies_all_baggage_to_span(self): |
| 40 | + ctx = _make_context_with_baggage({"user.id": "u1", "session.id": "s1"}) |
| 41 | + span = _make_recording_span() |
| 42 | + |
| 43 | + self.processor.on_start(span, ctx) |
| 44 | + |
| 45 | + span.set_attribute.assert_any_call("user.id", "u1") |
| 46 | + span.set_attribute.assert_any_call("session.id", "s1") |
| 47 | + |
| 48 | + def test_predicate_filters_keys(self): |
| 49 | + ctx = _make_context_with_baggage({"keep.this": "yes", "drop.this": "no"}) |
| 50 | + span = _make_recording_span() |
| 51 | + processor = BaggageSpanProcessor(lambda k: k.startswith("keep")) |
| 52 | + |
| 53 | + processor.on_start(span, ctx) |
| 54 | + |
| 55 | + span.set_attribute.assert_called_once_with("keep.this", "yes") |
| 56 | + |
| 57 | + def test_no_op_when_baggage_empty(self): |
| 58 | + span = _make_recording_span() |
| 59 | + self.processor.on_start(span, context.get_current()) |
| 60 | + span.set_attribute.assert_not_called() |
| 61 | + |
| 62 | + def test_no_op_for_non_recording_span(self): |
| 63 | + ctx = _make_context_with_baggage({"gen_ai.conversation.id": "abc"}) |
| 64 | + span = MagicMock() |
| 65 | + span.is_recording.return_value = False |
| 66 | + |
| 67 | + self.processor.on_start(span, ctx) |
| 68 | + |
| 69 | + span.set_attribute.assert_not_called() |
| 70 | + assert not self.processor._pending |
| 71 | + |
| 72 | + def test_snapshots_override_key_when_present_in_baggage(self): |
| 73 | + ctx = _make_context_with_baggage({"gen_ai.conversation.id": "caller-uuid"}) |
| 74 | + span = _make_recording_span(span_id=42) |
| 75 | + |
| 76 | + self.processor.on_start(span, ctx) |
| 77 | + |
| 78 | + assert self.processor._pending[42] == {"gen_ai.conversation.id": "caller-uuid"} |
| 79 | + |
| 80 | + def test_does_not_snapshot_when_override_key_absent(self): |
| 81 | + ctx = _make_context_with_baggage({"other.key": "value"}) |
| 82 | + span = _make_recording_span(span_id=42) |
| 83 | + |
| 84 | + self.processor.on_start(span, ctx) |
| 85 | + |
| 86 | + assert 42 not in self.processor._pending |
| 87 | + |
| 88 | + |
| 89 | +class TestBaggageSpanProcessorOnEnd: |
| 90 | + def setup_method(self): |
| 91 | + self.processor = BaggageSpanProcessor(ALLOW_ALL_BAGGAGE_KEYS) |
| 92 | + |
| 93 | + def test_override_wins_after_framework_overwrites(self): |
| 94 | + ctx = _make_context_with_baggage({"gen_ai.conversation.id": "caller-uuid"}) |
| 95 | + span = _make_recording_span(span_id=7) |
| 96 | + self.processor.on_start(span, ctx) |
| 97 | + |
| 98 | + # simulate framework callback overwriting the value mid-span |
| 99 | + ended = _make_ended_span( |
| 100 | + span_id=7, attributes={"gen_ai.conversation.id": "langgraph-thread-id"} |
| 101 | + ) |
| 102 | + self.processor.on_end(ended) |
| 103 | + |
| 104 | + assert ended._attributes["gen_ai.conversation.id"] == "caller-uuid" |
| 105 | + |
| 106 | + def test_pending_entry_cleaned_up_after_on_end(self): |
| 107 | + ctx = _make_context_with_baggage({"gen_ai.conversation.id": "caller-uuid"}) |
| 108 | + span = _make_recording_span(span_id=7) |
| 109 | + self.processor.on_start(span, ctx) |
| 110 | + |
| 111 | + ended = _make_ended_span(span_id=7) |
| 112 | + self.processor.on_end(ended) |
| 113 | + |
| 114 | + assert 7 not in self.processor._pending |
| 115 | + |
| 116 | + def test_on_end_no_op_when_key_not_in_baggage(self): |
| 117 | + ctx = _make_context_with_baggage({"other.key": "value"}) |
| 118 | + span = _make_recording_span(span_id=5) |
| 119 | + self.processor.on_start(span, ctx) |
| 120 | + |
| 121 | + ended = _make_ended_span( |
| 122 | + span_id=5, attributes={"gen_ai.conversation.id": "framework-set"} |
| 123 | + ) |
| 124 | + self.processor.on_end(ended) |
| 125 | + |
| 126 | + # no override was snapshotted so framework value is untouched |
| 127 | + assert ended._attributes["gen_ai.conversation.id"] == "framework-set" |
| 128 | + |
| 129 | + def test_on_end_no_op_for_unknown_span_id(self): |
| 130 | + ended = _make_ended_span( |
| 131 | + span_id=99, attributes={"gen_ai.conversation.id": "framework-set"} |
| 132 | + ) |
| 133 | + self.processor.on_end(ended) # no KeyError |
| 134 | + assert ended._attributes["gen_ai.conversation.id"] == "framework-set" |
| 135 | + |
| 136 | + def test_on_end_no_op_when_attributes_missing(self): |
| 137 | + ctx = _make_context_with_baggage({"gen_ai.conversation.id": "caller-uuid"}) |
| 138 | + span = _make_recording_span(span_id=3) |
| 139 | + self.processor.on_start(span, ctx) |
| 140 | + |
| 141 | + ended = MagicMock() |
| 142 | + ended.context.span_id = 3 |
| 143 | + del ended._attributes # simulate missing _attributes |
| 144 | + |
| 145 | + self.processor.on_end(ended) # must not raise |
| 146 | + |
| 147 | + def test_on_end_swallows_mutation_error(self): |
| 148 | + ctx = _make_context_with_baggage({"gen_ai.conversation.id": "caller-uuid"}) |
| 149 | + span = _make_recording_span(span_id=8) |
| 150 | + self.processor.on_start(span, ctx) |
| 151 | + |
| 152 | + ended = MagicMock() |
| 153 | + ended.context.span_id = 8 |
| 154 | + ended._attributes = MagicMock() |
| 155 | + ended._attributes.update.side_effect = RuntimeError("immutable") |
| 156 | + |
| 157 | + self.processor.on_end(ended) # must not raise |
| 158 | + |
| 159 | + |
| 160 | +class TestBaggageSpanProcessorCustomOverrideKeys: |
| 161 | + def test_custom_override_key_is_enforced(self): |
| 162 | + processor = BaggageSpanProcessor( |
| 163 | + ALLOW_ALL_BAGGAGE_KEYS, override_keys=("my.custom.key",) |
| 164 | + ) |
| 165 | + ctx = _make_context_with_baggage({"my.custom.key": "baggage-val"}) |
| 166 | + span = _make_recording_span(span_id=1) |
| 167 | + processor.on_start(span, ctx) |
| 168 | + |
| 169 | + ended = _make_ended_span( |
| 170 | + span_id=1, attributes={"my.custom.key": "framework-val"} |
| 171 | + ) |
| 172 | + processor.on_end(ended) |
| 173 | + |
| 174 | + assert ended._attributes["my.custom.key"] == "baggage-val" |
| 175 | + |
| 176 | + def test_empty_override_keys_skips_snapshot(self): |
| 177 | + processor = BaggageSpanProcessor(ALLOW_ALL_BAGGAGE_KEYS, override_keys=()) |
| 178 | + ctx = _make_context_with_baggage({"gen_ai.conversation.id": "caller-uuid"}) |
| 179 | + span = _make_recording_span(span_id=1) |
| 180 | + processor.on_start(span, ctx) |
| 181 | + |
| 182 | + assert not processor._pending |
0 commit comments