def _encode_trace_state(trace_state: TraceState) -> Optional[str]: pb2_trace_state = None if trace_state is not None: pb2_trace_state = ",".join( [f"{key}={value}" for key, value in (trace_state.items())] ) return pb2_trace_state
def test_fields(self, mock_get_current_span, mock_invalid_span_context): mock_get_current_span.configure_mock( return_value=Mock( **{ "get_span_context.return_value": Mock( **{ "trace_id": 1, "span_id": 2, "trace_flags": 3, "trace_state": TraceState([("a", "b")]), } ) } ) ) mock_set_in_carrier = Mock() FORMAT.inject(mock_set_in_carrier, {}) inject_fields = set() for mock_call in mock_set_in_carrier.mock_calls: inject_fields.add(mock_call[1][1]) self.assertEqual(inject_fields, FORMAT.fields)
def test_tracestate_from_header(self): entries = [ "1a-2f@foo=bar1", "1a-_*/2b@foo=bar2", "foo=bar3", "foo-_*/bar=bar4", ] header_list = [",".join(entries)] state = TraceState.from_header(header_list) self.assertEqual(state.to_header(), ",".join(entries))
def test_tracestate_valid_pairs(self): pairs = [("1a-2f@foo", "bar1"), ("foo-_*/bar", "bar4")] state = TraceState(pairs) self.assertEqual(len(state), 2) self.assertIsNotNone(state.get("foo-_*/bar")) self.assertEqual(state.get("foo-_*/bar"), "bar4") self.assertEqual(state.to_header(), "1a-2f@foo=bar1,foo-_*/bar=bar4") self.assertIsNone(state.get("random"))
def extract( self, carrier: textmap.CarrierT, context: typing.Optional[Context] = None, getter: textmap.Getter = textmap.default_getter, ) -> Context: """Extracts SpanContext from the carrier. See `opentelemetry.propagators.textmap.TextMapPropagator.extract` """ if context is None: context = Context() header = getter.get(carrier, self._TRACEPARENT_HEADER_NAME) if not header: return context match = re.search(self._TRACEPARENT_HEADER_FORMAT_RE, header[0]) if not match: return context version = match.group(1) trace_id = match.group(2) span_id = match.group(3) trace_flags = match.group(4) if trace_id == "0" * 32 or span_id == "0" * 16: return context if version == "00": if match.group(5): return context if version == "ff": return context tracestate_headers = getter.get(carrier, self._TRACESTATE_HEADER_NAME) if tracestate_headers is None: tracestate = None else: tracestate = TraceState.from_header(tracestate_headers) span_context = trace.SpanContext( trace_id=int(trace_id, 16), span_id=int(span_id, 16), is_remote=True, trace_flags=trace.TraceFlags(trace_flags), trace_state=tracestate, ) return trace.set_span_in_context(trace.NonRecordingSpan(span_context), context)
def test_tracestate_order_changed(self): entries = [ "1a-2f@foo=bar1", "1a-_*/2b@foo=bar2", "foo=bar3", "foo-_*/bar=bar4", ] header_list = [",".join(entries)] state = TraceState.from_header(header_list) new_state = state.update("foo", "bar33") entries = list(new_state.items()) # type: ignore foo_place = entries.index(("foo", "bar33")) # type: ignore prev_first_place = entries.index(("1a-2f@foo", "bar1")) # type: ignore self.assertLessEqual(foo_place, prev_first_place)
def test_trace_contains(self): entries = [ "1a-2f@foo=bar1", "1a-_*/2b@foo=bar2", "foo=bar3", "foo-_*/bar=bar4", ] header_list = [",".join(entries)] state = TraceState.from_header(header_list) self.assertTrue("foo" in state) self.assertFalse("bar" in state) self.assertIsNone(state.get("bar")) with self.assertRaises(KeyError): state["bar"] # pylint:disable=W0104
def test_tracestate_update_valid(self): state = TraceState([("a", "1")]) new_state = state.update("a", "2") self.assertEqual(new_state.get("a"), "2") new_state = new_state.add("b", "3") self.assertNotEqual(state, new_state)
def test_tracestate_add_valid(self): state = TraceState() new_state = state.add("1a-2f@foo", "bar4") self.assertEqual(len(new_state), 1) self.assertEqual(new_state.get("1a-2f@foo"), "bar4")
def test_empty_tracestate(self): state = TraceState() self.assertEqual(len(state), 0) self.assertEqual(state.to_header(), "")