def test_clear_baggage(self): self.assertEqual({}, baggage.get_all()) ctx = baggage.set_baggage("test", "value") self.assertEqual(baggage.get_baggage("test", context=ctx), "value") ctx = baggage.clear(context=ctx) self.assertEqual(baggage.get_all(context=ctx), {})
def test_propagation(self): traceparent_value = "00-{trace_id}-{span_id}-00".format( trace_id=format(self.TRACE_ID, "032x"), span_id=format(self.SPAN_ID, "016x"), ) tracestate_value = "foo=1,bar=2,baz=3" headers = { "baggage": ["key1=val1,key2=val2"], "traceparent": [traceparent_value], "tracestate": [tracestate_value], } ctx = extract(carrier_getter, headers) baggage_entries = baggage.get_all(context=ctx) expected = {"key1": "val1", "key2": "val2"} self.assertEqual(baggage_entries, expected) span_context = get_current_span(context=ctx).get_span_context() self.assertEqual(span_context.trace_id, self.TRACE_ID) self.assertEqual(span_context.span_id, self.SPAN_ID) span = trace.NonRecordingSpan(span_context) ctx = baggage.set_baggage("key3", "val3") ctx = baggage.set_baggage("key4", "val4", context=ctx) ctx = set_span_in_context(span, context=ctx) output = {} inject(dict.__setitem__, output, context=ctx) self.assertEqual(traceparent_value, output["traceparent"]) self.assertIn("key3=val3", output["baggage"]) self.assertIn("key4=val4", output["baggage"]) self.assertIn("foo=1", output["tracestate"]) self.assertIn("bar=2", output["tracestate"]) self.assertIn("baz=3", output["tracestate"])
def inject( self, carrier: CarrierT, context: typing.Optional[Context] = None, setter: Setter = default_setter, ) -> None: span = trace.get_current_span(context=context) span_context = span.get_span_context() if span_context == trace.INVALID_SPAN_CONTEXT: return span_parent_id = span.parent.span_id if span.parent else 0 trace_flags = span_context.trace_flags if trace_flags.sampled: trace_flags |= self.DEBUG_FLAG # set span identity setter.set( carrier, self.TRACE_ID_KEY, _format_uber_trace_id( span_context.trace_id, span_context.span_id, span_parent_id, trace_flags, ), ) # set span baggage, if any baggage_entries = baggage.get_all(context=context) if not baggage_entries: return for key, value in baggage_entries.items(): baggage_key = self.BAGGAGE_PREFIX + key setter.set(carrier, baggage_key, urllib.parse.quote(str(value)))
def test_modifying_baggage(self): ctx = baggage.set_baggage("test", "value") self.assertEqual(baggage.get_baggage("test", context=ctx), "value") baggage_entries = baggage.get_all(context=ctx) with self.assertRaises(TypeError): baggage_entries["test"] = "mess-this-up" self.assertEqual(baggage.get_baggage("test", context=ctx), "value")
def test_remove_baggage_entry(self): self.assertEqual({}, baggage.get_all()) ctx = baggage.set_baggage("test", "value") ctx = baggage.set_baggage("test2", "value2", context=ctx) ctx = baggage.remove_baggage("test", context=ctx) self.assertEqual(baggage.get_baggage("test", context=ctx), None) self.assertEqual(baggage.get_baggage("test2", context=ctx), "value2")
def extract( self, carrier: CarrierT, context: Optional[Context] = None, getter: Getter = default_getter, ) -> Context: if context is None: context = Context() traceid = _extract_identifier( getter.get(carrier, OT_TRACE_ID_HEADER), _valid_extract_traceid, INVALID_TRACE_ID, ) spanid = _extract_identifier( getter.get(carrier, OT_SPAN_ID_HEADER), _valid_extract_spanid, INVALID_SPAN_ID, ) sampled = _extract_first_element( getter.get(carrier, OT_SAMPLED_HEADER) ) if sampled == "true": traceflags = TraceFlags.SAMPLED else: traceflags = TraceFlags.DEFAULT if traceid != INVALID_TRACE_ID and spanid != INVALID_SPAN_ID: context = set_span_in_context( NonRecordingSpan( SpanContext( trace_id=traceid, span_id=spanid, is_remote=True, trace_flags=TraceFlags(traceflags), ) ), context, ) baggage = get_all(context) or {} for key in getter.keys(carrier): if not key.startswith(OT_BAGGAGE_PREFIX): continue baggage[ key[len(OT_BAGGAGE_PREFIX) :] ] = _extract_first_element(getter.get(carrier, key)) for key, value in baggage.items(): context = set_baggage(key, value, context) return context
def test_set_multiple_baggage_entries(self): ctx = baggage.set_baggage("test", "value") ctx = baggage.set_baggage("test2", "value2", context=ctx) self.assertEqual(baggage.get_baggage("test", context=ctx), "value") self.assertEqual(baggage.get_baggage("test2", context=ctx), "value2") self.assertEqual( baggage.get_all(context=ctx), {"test": "value", "test2": "value2"}, )
def extract( self, getter: Getter[TextMapPropagatorT], carrier: TextMapPropagatorT, context: Optional[Context] = None, ) -> Context: traceid = _extract_first_element( getter.get(carrier, OT_TRACE_ID_HEADER)) spanid = _extract_first_element(getter.get(carrier, OT_SPAN_ID_HEADER)) sampled = _extract_first_element(getter.get(carrier, OT_SAMPLED_HEADER)) if sampled == "true": traceflags = TraceFlags.SAMPLED else: traceflags = TraceFlags.DEFAULT if (traceid != INVALID_TRACE_ID and _valid_extract_traceid.fullmatch(traceid) is not None and spanid != INVALID_SPAN_ID and _valid_extract_spanid.fullmatch(spanid) is not None): context = set_span_in_context( DefaultSpan( SpanContext( trace_id=int(traceid, 16), span_id=int(spanid, 16), is_remote=True, trace_flags=traceflags, )), context, ) baggage = get_all(context) or {} for key in getter.keys(carrier): if not key.startswith(OT_BAGGAGE_PREFIX): continue baggage[key[len(OT_BAGGAGE_PREFIX):]] = _extract_first_element( getter.get(carrier, key)) for key, value in baggage.items(): context = set_baggage(key, value, context) return context
def inject( self, set_in_carrier: textmap.Setter[textmap.TextMapPropagatorT], carrier: textmap.TextMapPropagatorT, context: typing.Optional[Context] = None, ) -> None: """Injects Baggage into the carrier. See `opentelemetry.trace.propagation.textmap.TextMapPropagator.inject` """ baggage_entries = baggage.get_all(context=context) if not baggage_entries: return baggage_string = _format_baggage(baggage_entries) set_in_carrier(carrier, self._BAGGAGE_HEADER_NAME, baggage_string)
def inject( self, carrier: textmap.CarrierT, context: Optional[Context] = None, setter: textmap.Setter = textmap.default_setter, ) -> None: """Injects Baggage into the carrier. See `opentelemetry.propagators.textmap.TextMapPropagator.inject` """ baggage_entries = get_all(context=context) if not baggage_entries: return baggage_string = _format_baggage(baggage_entries) setter.set(carrier, self._BAGGAGE_HEADER_NAME, baggage_string)
def inject( self, carrier: CarrierT, context: Optional[Context] = None, setter: Setter = default_setter, ) -> None: span_context = get_current_span(context).get_span_context() if span_context.trace_id == INVALID_TRACE_ID: return setter.set( carrier, OT_TRACE_ID_HEADER, hex(span_context.trace_id)[2:][-16:] ) setter.set( carrier, OT_SPAN_ID_HEADER, hex(span_context.span_id)[2:][-16:], ) if span_context.trace_flags == TraceFlags.SAMPLED: traceflags = "true" else: traceflags = "false" setter.set(carrier, OT_SAMPLED_HEADER, traceflags) baggage = get_all(context) if not baggage: return for header_name, header_value in baggage.items(): if ( _valid_header_name.fullmatch(header_name) is None or _valid_header_value.fullmatch(header_value) is None ): continue setter.set( carrier, "".join([OT_BAGGAGE_PREFIX, header_name]), header_value, )
def test_extract_baggage(self): """Test baggage extraction""" context = self.ot_trace_propagator.extract( { OT_TRACE_ID_HEADER: "64fe8b2a57d3eff7", OT_SPAN_ID_HEADER: "e457b5a2e4d86bd1", OT_SAMPLED_HEADER: "false", "".join([OT_BAGGAGE_PREFIX, "abc"]): "abc", "".join([OT_BAGGAGE_PREFIX, "def"]): "def", }, ) span_context = get_current_span(context).get_span_context() self.assertEqual(hex(span_context.trace_id)[2:], "64fe8b2a57d3eff7") self.assertEqual(hex(span_context.span_id)[2:], "e457b5a2e4d86bd1") self.assertTrue(span_context.is_remote) self.assertEqual(span_context.trace_flags, TraceFlags.DEFAULT) baggage = get_all(context) self.assertEqual(baggage["abc"], "abc") self.assertEqual(baggage["def"], "def")
async def main(): span = tracer.start_span(name="span") ctx = await async_span(span) print(baggage.get_all(context=ctx))
# Copyright The OpenTelemetry Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from opentelemetry import baggage, trace from opentelemetry.sdk.trace import TracerProvider trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) with tracer.start_span(name="root span") as root_span: ctx = baggage.set_baggage("foo", "bar") print("Global context baggage: {}".format(baggage.get_all())) print("Span context baggage: {}".format(baggage.get_all(context=ctx)))
def _extract(self, header_value): """Test helper""" header = {"baggage": [header_value]} return baggage.get_all(self.propagator.extract(header))
def test_no_context_header(self): baggage_entries = baggage.get_all(self.propagator.extract({})) self.assertEqual(baggage_entries, {})
def _extract(self, header_value): """Test helper""" header = {"otcorrelations": [header_value]} return baggage.get_all(self.propagator.extract(get_as_list, header))