コード例 #1
0
    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 = {
            "otcorrelationcontext": ["key1=val1,key2=val2"],
            "traceparent": [traceparent_value],
            "tracestate": [tracestate_value],
        }
        ctx = extract(get_as_list, headers)
        correlations = correlationcontext.get_correlations(context=ctx)
        expected = {"key1": "val1", "key2": "val2"}
        self.assertEqual(correlations, expected)
        span_context = get_span_from_context(context=ctx).get_context()

        self.assertEqual(span_context.trace_id, self.TRACE_ID)
        self.assertEqual(span_context.span_id, self.SPAN_ID)

        span = trace.DefaultSpan(span_context)
        ctx = correlationcontext.set_correlation("key3", "val3")
        ctx = correlationcontext.set_correlation("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["otcorrelationcontext"])
        self.assertIn("key4=val4", output["otcorrelationcontext"])
        self.assertIn("foo=1", output["tracestate"])
        self.assertIn("bar=2", output["tracestate"])
        self.assertIn("baz=3", output["tracestate"])
コード例 #2
0
    def test_remove_correlations(self):
        self.assertEqual({}, cctx.get_correlations())

        ctx = cctx.set_correlation("test", "value")
        ctx = cctx.set_correlation("test2", "value2", context=ctx)
        ctx = cctx.remove_correlation("test", context=ctx)
        self.assertEqual(cctx.get_correlation("test", context=ctx), None)
        self.assertEqual(cctx.get_correlation("test2", context=ctx), "value2")
コード例 #3
0
 def test_set_multiple_correlations(self):
     ctx = cctx.set_correlation("test", "value")
     ctx = cctx.set_correlation("test2", "value2", context=ctx)
     self.assertEqual(cctx.get_correlation("test", context=ctx), "value")
     self.assertEqual(cctx.get_correlation("test2", context=ctx), "value2")
     self.assertEqual(
         cctx.get_correlations(context=ctx),
         {"test": "value", "test2": "value2"},
     )
コード例 #4
0
 def test_modifying_correlations(self):
     ctx = cctx.set_correlation("test", "value")
     self.assertEqual(cctx.get_correlation("test", context=ctx), "value")
     correlations = cctx.get_correlations(context=ctx)
     with self.assertRaises(TypeError):
         correlations["test"] = "mess-this-up"
     self.assertEqual(cctx.get_correlation("test", context=ctx), "value")
コード例 #5
0
    def test_clear_correlations(self):
        self.assertEqual({}, cctx.get_correlations())

        ctx = cctx.set_correlation("test", "value")
        self.assertEqual(cctx.get_correlation("test", context=ctx), "value")

        ctx = cctx.clear_correlations(context=ctx)
        self.assertEqual(cctx.get_correlations(context=ctx), {})
 def _inject(self, values):
     """Test helper"""
     ctx = get_current()
     for k, v in values.items():
         ctx = correlationcontext.set_correlation(k, v, context=ctx)
     output = {}
     self.propagator.inject(dict.__setitem__, output, context=ctx)
     return output.get("otcorrelationcontext")
コード例 #7
0
    def set_baggage_item(self, key: str, value: str):
        """Stores a Baggage item in the span as a key/value
        pair.

        Args:
            key: A tag key.
            value: A tag value.
        """
        # pylint: disable=protected-access
        self._context._baggage = set_correlation(
            key, value, context=self._context._baggage)
コード例 #8
0
    def extract(
        self,
        get_from_carrier: httptextformat.Getter[
            httptextformat.HTTPTextFormatT],
        carrier: httptextformat.HTTPTextFormatT,
        context: typing.Optional[Context] = None,
    ) -> Context:
        """Extract CorrelationContext from the carrier.

        See
        `opentelemetry.trace.propagation.httptextformat.HTTPTextFormat.extract`
        """

        if context is None:
            context = get_current()

        header = _extract_first_element(
            get_from_carrier(carrier, self._CORRELATION_CONTEXT_HEADER_NAME))

        if not header or len(header) > self.MAX_HEADER_LENGTH:
            return context

        correlations = header.split(",")
        total_correlations = self.MAX_PAIRS
        for correlation in correlations:
            if total_correlations <= 0:
                return context
            total_correlations -= 1
            if len(correlation) > self.MAX_PAIR_LENGTH:
                continue
            try:
                name, value = correlation.split("=", 1)
            except Exception:  # pylint: disable=broad-except
                continue
            context = correlationcontext.set_correlation(
                urllib.parse.unquote(name).strip(),
                urllib.parse.unquote(value).strip(),
                context=context,
            )

        return context
コード例 #9
0
 def test_correlations_current_context(self):
     token = context.attach(cctx.set_correlation("test", "value"))
     self.assertEqual(cctx.get_correlation("test"), "value")
     context.detach(token)
     self.assertEqual(cctx.get_correlation("test"), None)
コード例 #10
0
 def set_baggage_item(self, key, value):
     """Implements the ``set_baggage_item`` method from the base class."""
     # pylint: disable=protected-access
     self._context._baggage = set_correlation(
         key, value, context=self._context._baggage)