Python opentracing.Format.TEXT_MAP Examples

The following are 20 code examples of opentracing.Format.TEXT_MAP(). 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.Format , or try the search function .
Example #1
Source File: test_tracing.py    From tchannel-python with MIT License 6 votes vote down vote up
def _get_span(self):
        try:
            carrier = {}
            for k, v in six.iteritems(self.request.headers):
                carrier[k] = six.moves.urllib.parse.unquote(v)
            span_ctx = opentracing.tracer.extract(Format.TEXT_MAP, carrier)
            span = opentracing.tracer.start_span(
                operation_name='server',
                child_of=span_ctx,
            )
        except Exception as e:
            self.write('ERROR: %s' % e)
            self.set_status(200)
            return None

        if span is None:
            self.write('ERROR: Failed to join trace')
            self.set_status(200)

        return span 
Example #2
Source File: test_codecs.py    From jaeger-client-python with Apache License 2.0 6 votes vote down vote up
def test_debug_id():
    debug_header = 'correlation-id'
    tracer = Tracer(
        service_name='test',
        reporter=InMemoryReporter(),
        sampler=ConstSampler(True),
        debug_id_header=debug_header,
    )
    tracer.codecs[Format.TEXT_MAP] = TextCodec(
        url_encoding=False,
        debug_id_header=debug_header,
    )
    carrier = {debug_header: 'Coraline'}
    context = tracer.extract(Format.TEXT_MAP, carrier)
    assert context.is_debug_id_container_only
    assert context.debug_id == 'Coraline'
    span = tracer.start_span('test', child_of=context)
    assert span.is_debug()
    assert span.is_sampled()
    tags = [t for t in span.tags if t.key == debug_header]
    assert len(tags) == 1
    assert tags[0].vStr == 'Coraline' 
Example #3
Source File: test_noop_span.py    From opentracing-python with Apache License 2.0 6 votes vote down vote up
def test_inject():
    tracer = Tracer()
    span = tracer.start_span()

    bin_carrier = bytearray()
    tracer.inject(
        span_context=span.context,
        format=Format.BINARY,
        carrier=bin_carrier)
    assert bin_carrier == bytearray()

    text_carrier = {}
    tracer.inject(
        span_context=span.context,
        format=Format.TEXT_MAP,
        carrier=text_carrier)
    assert text_carrier == {} 
Example #4
Source File: test_tracer.py    From jaeger-client-python with Apache License 2.0 6 votes vote down vote up
def test_serialization(tracer, inject_mode):
    span = tracer.start_span('help')
    carrier = {}
    if inject_mode == 'span':
        injectable = span
    elif inject_mode == 'context':
        injectable = span.context
    else:
        raise ValueError('bad inject_mode')
    tracer.inject(
        span_context=injectable, format=Format.TEXT_MAP, carrier=carrier
    )
    assert len(carrier) > 0
    h_ctx = tornado.httputil.HTTPHeaders(carrier)
    assert 'UBER-TRACE-ID' in h_ctx
    ctx2 = tracer.extract(Format.TEXT_MAP, carrier)
    assert ctx2 is not None
    assert ctx2.trace_id == span.trace_id
    assert ctx2.span_id == span.span_id
    assert ctx2.parent_id == span.parent_id
    assert ctx2.flags == span.flags 
Example #5
Source File: test_noop_tracer.py    From jaeger-client-python with Apache License 2.0 6 votes vote down vote up
def test_join_trace():
    tracer = Tracer()

    span_ctx = tracer.extract(format=Format.TEXT_MAP, carrier={})
    span = tracer.start_span(operation_name='test',
                             references=opentracing.child_of(span_ctx))
    span.set_tag('x', 'y')
    span.set_baggage_item('a', 'b')
    span.log_event('z')

    child = tracer.start_span(operation_name='child',
                              references=opentracing.child_of(span.context))
    child.log_event('w')
    child.finish()

    span.finish() 
Example #6
Source File: test_noop_tracer.py    From jaeger-client-python with Apache License 2.0 6 votes vote down vote up
def test_new_trace():
    tracer = Tracer()

    span = tracer.start_span(operation_name='test')
    span.set_baggage_item('Fry', 'Leela')
    span.set_tag('x', 'y')
    span.log_event('z')

    child = tracer.start_span(operation_name='child',
                              references=opentracing.child_of(span.context))
    child.log_event('w')
    assert child.get_baggage_item('Fry') is None
    carrier = {}
    tracer.inject(
        span_context=child.context,
        format=Format.TEXT_MAP,
        carrier=carrier)
    assert carrier == dict()
    child.finish()

    span.finish() 
Example #7
Source File: open_tracing_decorator.py    From protoactor-python with Apache License 2.0 6 votes vote down vote up
def receive(self, envelope: MessageEnvelope) -> None:
        message = envelope.message
        parent_span_ctx = None
        if envelope.header is not None:
            parent_span_ctx = self._tracer.extract(format=Format.TEXT_MAP, carrier=envelope.header)

        with OpenTracingHelper.build_started_scope(self._tracer, parent_span_ctx, 'receive', message,
                                                   self._receive_span_setup) as scope:
            try:
                if envelope.sender is not None:
                    scope.span.set_tag('proto.senderpid', envelope.sender.to_short_string())
                if self._receive_span_setup is not None:
                    self._receive_span_setup(scope.span, message)
                await super().receive(envelope)
            except Exception as ex:
                OpenTracingHelper.setup_span(ex, scope.span)
                raise Exception() 
Example #8
Source File: open_tracing_middleware.py    From protoactor-python with Apache License 2.0 6 votes vote down vote up
def open_tracing_sender_middleware(tracer: Tracer = None):
    def level_0(next):
        async def level_1(context: AbstractSenderContext, target: PID, envelope: MessageEnvelope):
            if tracer is None:
                inner_tracer = opentracing.global_tracer()
            else:
                inner_tracer = tracer
            span = inner_tracer.active_span
            if span is None:
                await next(context, target, envelope)
            else:
                dictionary = {}
                inner_tracer.inject(span.context, Format.TEXT_MAP, dictionary)
                envelope = envelope.with_headers(dictionary)
                await next(context, target, envelope)

        return level_1

    return level_0 
Example #9
Source File: tests.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_tracer_extract_map(tracer):
    span_context = tracer.extract(
        Format.TEXT_MAP, {"elastic-apm-traceparent": "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"}
    )

    assert span_context.trace_parent.version == 0
    assert span_context.trace_parent.trace_id == "0af7651916cd43dd8448eb211c80319c"
    assert span_context.trace_parent.span_id == "b7ad6b7169203331" 
Example #10
Source File: test_tracer.py    From jaeger-client-python with Apache License 2.0 5 votes vote down vote up
def test_serialization_error(tracer):
    span = 'span'
    carrier = {}
    with pytest.raises(ValueError):
        tracer.inject(
            span_context=span, format=Format.TEXT_MAP, carrier=carrier
        ) 
Example #11
Source File: tracer.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def inject(self, span_context, format, carrier):
        if format in (Format.HTTP_HEADERS, Format.TEXT_MAP):
            if not isinstance(carrier, dict):
                raise InvalidCarrierException("carrier for {} format should be dict-like".format(format))
            val = span_context.trace_parent.to_ascii()
            carrier[constants.TRACEPARENT_HEADER_NAME] = val
            if self._agent.config.use_elastic_traceparent_header:
                carrier[constants.TRACEPARENT_LEGACY_HEADER_NAME] = val
            return
        raise UnsupportedFormatException 
Example #12
Source File: tracer.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def extract(self, format, carrier):
        if format in (Format.HTTP_HEADERS, Format.TEXT_MAP):
            trace_parent = disttracing.TraceParent.from_headers(carrier)
            if not trace_parent:
                raise SpanContextCorruptedException("could not extract span context from carrier")
            return OTSpanContext(trace_parent=trace_parent)
        raise UnsupportedFormatException 
Example #13
Source File: tests.py    From apm-agent-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_tracer_inject_map(tracer):
    span_context = OTSpanContext(
        trace_parent=TraceParent.from_string("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01")
    )
    carrier = {}
    tracer.inject(span_context, Format.TEXT_MAP, carrier)
    assert carrier[constants.TRACEPARENT_HEADER_NAME] == b"00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"
    if tracer._agent.config.use_elastic_traceparent_header:
        assert carrier[constants.TRACEPARENT_LEGACY_HEADER_NAME] == carrier[constants.TRACEPARENT_HEADER_NAME] 
Example #14
Source File: tracer.py    From opentracing-python with Apache License 2.0 5 votes vote down vote up
def register_propagator(self, format, propagator):
        """Register a propagator with this MockTracer.

        :param string format: a :class:`~opentracing.Format`
            identifier like :attr:`~opentracing.Format.TEXT_MAP`
        :param **Propagator** propagator: a **Propagator** instance to handle
            inject/extract calls involving `format`
        """
        self._propagators[format] = propagator 
Example #15
Source File: tracer.py    From basictracer-python with Apache License 2.0 5 votes vote down vote up
def register_required_propagators(self):
        from .text_propagator import TextPropagator
        from .binary_propagator import BinaryPropagator
        self.register_propagator(Format.TEXT_MAP, TextPropagator())
        self.register_propagator(Format.HTTP_HEADERS, TextPropagator())
        self.register_propagator(Format.BINARY, BinaryPropagator()) 
Example #16
Source File: tracer.py    From basictracer-python with Apache License 2.0 5 votes vote down vote up
def register_propagator(self, format, propagator):
        """Register a propagator with this BasicTracer.

        :param string format: a Format identifier like Format.TEXT_MAP
        :param Propagator propagator: a Propagator instance to handle
            inject/extract calls involving `format`
        """
        self._propagators[format] = propagator 
Example #17
Source File: tracer.py    From lightstep-tracer-python with MIT License 5 votes vote down vote up
def __init__(self, enable_binary_format, recorder, scope_manager):
        """Initialize the LightStep Tracer, deferring to BasicTracer."""
        super(_LightstepTracer, self).__init__(recorder, scope_manager=scope_manager)
        self.register_propagator(Format.TEXT_MAP, TextPropagator())
        self.register_propagator(Format.HTTP_HEADERS, TextPropagator())
        if enable_binary_format:
            # We do this import lazily because protobuf versioning issues
            # can cause process-level failure at import time.
            from basictracer.binary_propagator import BinaryPropagator
            self.register_propagator(Format.BINARY, BinaryPropagator())
            self.register_propagator(LightStepFormat.LIGHTSTEP_BINARY, LightStepBinaryPropagator()) 
Example #18
Source File: api_check.py    From opentracing-python with Apache License 2.0 5 votes vote down vote up
def test_mandatory_formats(self):
        formats = [
            (Format.TEXT_MAP, {}),
            (Format.HTTP_HEADERS, {}),
            (Format.BINARY, bytearray()),
        ]
        with self.tracer().start_span(operation_name='Bender') as span:
            for fmt, carrier in formats:
                # expecting no exceptions
                span.tracer.inject(span.context, fmt, carrier)
                span.tracer.extract(fmt, carrier) 
Example #19
Source File: api_check.py    From opentracing-python with Apache License 2.0 5 votes vote down vote up
def test_text_propagation(self):
        with self.tracer().start_span(operation_name='Bender') as span:
            text_carrier = {}
            self.tracer().inject(
                span_context=span.context,
                format=opentracing.Format.TEXT_MAP,
                carrier=text_carrier)
            extracted_ctx = self.tracer().extract(
                format=opentracing.Format.TEXT_MAP,
                carrier=text_carrier)
            assert extracted_ctx.baggage == {} 
Example #20
Source File: tracer.py    From opentracing-python with Apache License 2.0 5 votes vote down vote up
def _register_required_propagators(self):
        from .text_propagator import TextPropagator
        from .binary_propagator import BinaryPropagator
        self.register_propagator(Format.TEXT_MAP, TextPropagator())
        self.register_propagator(Format.HTTP_HEADERS, TextPropagator())
        self.register_propagator(Format.BINARY, BinaryPropagator())