Python opentracing.SpanContext() Examples

The following are 16 code examples of opentracing.SpanContext(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module opentracing , or try the search function .
Example #1
Source File: context.py    From opentracing-python with Apache License 2.0 5 votes vote down vote up
def __init__(
            self,
            trace_id=None,
            span_id=None,
            baggage=None):
        self.trace_id = trace_id
        self.span_id = span_id
        self._baggage = baggage or opentracing.SpanContext.EMPTY_BAGGAGE 
Example #2
Source File: context.py    From opentracing-python with Apache License 2.0 5 votes vote down vote up
def with_baggage_item(self, key, value):
        new_baggage = self._baggage.copy()
        new_baggage[key] = value
        return SpanContext(
            trace_id=self.trace_id,
            span_id=self.span_id,
            baggage=new_baggage) 
Example #3
Source File: context.py    From basictracer-python with Apache License 2.0 5 votes vote down vote up
def __init__(
            self,
            trace_id=None,
            span_id=None,
            baggage=None,
            sampled=True):
        self.trace_id = trace_id
        self.span_id = span_id
        self.sampled = sampled
        self._baggage = baggage or opentracing.SpanContext.EMPTY_BAGGAGE 
Example #4
Source File: context.py    From basictracer-python with Apache License 2.0 5 votes vote down vote up
def with_baggage_item(self, key, value):
        new_baggage = self._baggage.copy()
        new_baggage[key] = value
        return SpanContext(
            trace_id=self.trace_id,
            span_id=self.span_id,
            sampled=self.sampled,
            baggage=new_baggage) 
Example #5
Source File: __init__.py    From opentelemetry-python with Apache License 2.0 5 votes vote down vote up
def unwrap(self):
        """Returns the wrapped :class:`opentelemetry.trace.SpanContext`
        object.

        Returns:
            The :class:`opentelemetry.trace.SpanContext` object wrapped by this
            :class:`SpanContextShim`.
        """

        return self._otel_context 
Example #6
Source File: test_shim.py    From opentelemetry-python with Apache License 2.0 5 votes vote down vote up
def test_parent_child_explicit_span_context(self):
        """Test parent-child relationship of spans when specifying a
        `SpanContext` object as a parent upon creation.
        """

        with self.shim.start_span("ParentSpan") as parent:
            with self.shim.start_active_span(
                "ChildSpan", child_of=parent.context
            ) as child:
                parent_trace_id = parent.unwrap().get_context().trace_id
                child_trace_id = child.span.unwrap().get_context().trace_id

                self.assertEqual(child_trace_id, parent_trace_id)
                self.assertEqual(
                    child.span.unwrap().parent, parent.context.unwrap()
                )

        with self.shim.start_span("ParentSpan") as parent:
            with self.shim.start_span(
                "SpanWithContextParent", child_of=parent.context
            ) as child:
                parent_trace_id = parent.unwrap().get_context().trace_id
                child_trace_id = child.unwrap().get_context().trace_id

                self.assertEqual(child_trace_id, parent_trace_id)
                self.assertEqual(
                    child.unwrap().parent, parent.context.unwrap()
                ) 
Example #7
Source File: test_shim.py    From opentelemetry-python with Apache License 2.0 5 votes vote down vote up
def test_span_context(self):
        """Test construction of `SpanContextShim` objects."""

        otel_context = trace.SpanContext(1234, 5678, is_remote=False)
        context = opentracingshim.SpanContextShim(otel_context)

        self.assertIsInstance(context, opentracing.SpanContext)
        self.assertEqual(context.unwrap().trace_id, 1234)
        self.assertEqual(context.unwrap().span_id, 5678) 
Example #8
Source File: test_shim.py    From opentelemetry-python with Apache License 2.0 5 votes vote down vote up
def test_inject_http_headers(self):
        """Test `inject()` method for Format.HTTP_HEADERS."""

        otel_context = trace.SpanContext(
            trace_id=1220, span_id=7478, is_remote=False
        )
        context = opentracingshim.SpanContextShim(otel_context)

        headers = {}
        self.shim.inject(context, opentracing.Format.HTTP_HEADERS, headers)
        self.assertEqual(headers[MockHTTPTextFormat.TRACE_ID_KEY], str(1220))
        self.assertEqual(headers[MockHTTPTextFormat.SPAN_ID_KEY], str(7478)) 
Example #9
Source File: test_shim.py    From opentelemetry-python with Apache License 2.0 5 votes vote down vote up
def test_inject_text_map(self):
        """Test `inject()` method for Format.TEXT_MAP."""

        otel_context = trace.SpanContext(
            trace_id=1220, span_id=7478, is_remote=False
        )
        context = opentracingshim.SpanContextShim(otel_context)

        # Verify Format.TEXT_MAP
        text_map = {}
        self.shim.inject(context, opentracing.Format.TEXT_MAP, text_map)
        self.assertEqual(text_map[MockHTTPTextFormat.TRACE_ID_KEY], str(1220))
        self.assertEqual(text_map[MockHTTPTextFormat.SPAN_ID_KEY], str(7478)) 
Example #10
Source File: test_shim.py    From opentelemetry-python with Apache License 2.0 5 votes vote down vote up
def test_extract_empty_context_returns_invalid_context(self):
        """In the case where the propagator cannot extract a
        SpanContext, extract should return and invalid span context.
        """
        _old_propagator = propagators.get_global_httptextformat()
        propagators.set_global_httptextformat(NOOPHTTPTextFormat())
        try:
            carrier = {}

            ctx = self.shim.extract(opentracing.Format.HTTP_HEADERS, carrier)
            self.assertEqual(ctx.unwrap(), trace.INVALID_SPAN_CONTEXT)
        finally:
            propagators.set_global_httptextformat(_old_propagator) 
Example #11
Source File: span_context.py    From jaeger-client-python with Apache License 2.0 5 votes vote down vote up
def __init__(self, trace_id, span_id, parent_id, flags, baggage=None, debug_id=None):
        self.trace_id = trace_id
        self.span_id = span_id
        self.parent_id = parent_id or None
        self.flags = flags
        self._baggage = baggage or opentracing.SpanContext.EMPTY_BAGGAGE
        self._debug_id = debug_id 
Example #12
Source File: span_context.py    From jaeger-client-python with Apache License 2.0 5 votes vote down vote up
def baggage(self):
        return self._baggage or opentracing.SpanContext.EMPTY_BAGGAGE 
Example #13
Source File: span_context.py    From jaeger-client-python with Apache License 2.0 5 votes vote down vote up
def with_baggage_item(self, key, value):
        baggage = dict(self._baggage)
        if value is not None:
            baggage[key] = value
        else:
            baggage.pop(key, None)
        return SpanContext(
            trace_id=self.trace_id,
            span_id=self.span_id,
            parent_id=self.parent_id,
            flags=self.flags,
            baggage=baggage,
        ) 
Example #14
Source File: span_context.py    From jaeger-client-python with Apache License 2.0 5 votes vote down vote up
def with_debug_id(debug_id):
        """Deprecated, not used by Jaeger."""
        ctx = SpanContext(
            trace_id=None, span_id=None, parent_id=None, flags=None
        )
        ctx._debug_id = debug_id
        return ctx 
Example #15
Source File: tracer.py    From opentracing-python with Apache License 2.0 4 votes vote down vote up
def start_span(self,
                   operation_name=None,
                   child_of=None,
                   references=None,
                   tags=None,
                   start_time=None,
                   ignore_active_span=False):

        start_time = time.time() if start_time is None else start_time

        # See if we have a parent_ctx in `references`
        parent_ctx = None
        if child_of is not None:
            parent_ctx = (
                child_of if isinstance(child_of, opentracing.SpanContext)
                else child_of.context)
        elif references is not None and len(references) > 0:
            # TODO only the first reference is currently used
            parent_ctx = references[0].referenced_context

        # retrieve the active SpanContext
        if not ignore_active_span and parent_ctx is None:
            scope = self.scope_manager.active
            if scope is not None:
                parent_ctx = scope.span.context

        # Assemble the child ctx
        ctx = SpanContext(span_id=self._generate_id())
        if parent_ctx is not None:
            if parent_ctx._baggage is not None:
                ctx._baggage = parent_ctx._baggage.copy()
            ctx.trace_id = parent_ctx.trace_id
        else:
            ctx.trace_id = self._generate_id()

        # Tie it all together
        return MockSpan(
            self,
            operation_name=operation_name,
            context=ctx,
            parent_id=(None if parent_ctx is None else parent_ctx.span_id),
            tags=tags,
            start_time=start_time) 
Example #16
Source File: tracer.py    From basictracer-python with Apache License 2.0 4 votes vote down vote up
def start_span(self,
                   operation_name=None,
                   child_of=None,
                   references=None,
                   tags=None,
                   start_time=None,
                   ignore_active_span=False):

        if isinstance(references, opentracing.Reference):
            references = [references]

        start_time = time.time() if start_time is None else start_time

        # See if we have a parent_ctx in `references`
        parent_ctx = None
        if child_of is not None:
            parent_ctx = (
                child_of if isinstance(child_of, opentracing.SpanContext)
                else child_of.context)
        elif references is not None and len(references) > 0:
            # TODO only the first reference is currently used
            first_ref = references[0]
            if not isinstance(first_ref, opentracing.Reference):
                msg = (
                    'references[0] should be a opentracing.Reference '
                    'objects, got %r instead'
                )
                raise TypeError(msg % first_ref)
            parent_ctx = first_ref.referenced_context

        # retrieve the active SpanContext
        if not ignore_active_span and parent_ctx is None:
            scope = self.scope_manager.active
            if scope is not None:
                parent_ctx = scope.span.context

        # Assemble the child ctx
        ctx = SpanContext(span_id=generate_id())
        if parent_ctx is not None:
            if parent_ctx._baggage is not None:
                ctx._baggage = parent_ctx._baggage.copy()
            ctx.trace_id = parent_ctx.trace_id
            ctx.sampled = parent_ctx.sampled
        else:
            ctx.trace_id = generate_id()
            ctx.sampled = self.sampler.sampled(ctx.trace_id)

        # Tie it all together
        return BasicSpan(
            self,
            operation_name=operation_name,
            context=ctx,
            parent_id=(None if parent_ctx is None else parent_ctx.span_id),
            tags=tags,
            start_time=start_time)