Python opentracing.Format.HTTP_HEADERS Examples
The following are 19
code examples of opentracing.Format.HTTP_HEADERS().
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: context_in_headers.py From lightstep-tracer-python with MIT License | 6 votes |
def before_sending_request(request): """Context manager creates Span and encodes the span's SpanContext into request. """ span = opentracing.tracer.start_span('Sending request') span.set_tag('server.http.url', request.get_full_url()) try: # Python 2 host = request.get_host() except: # Python 3 host = request.host if host: span.set_tag(opentracing.ext.tags.PEER_HOST_IPV4, host) carrier_dict = {} span.tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, carrier_dict) for k, v in carrier_dict.items(): request.add_header(k, v) return span
Example #2
Source File: b3_propagator_test.py From lightstep-tracer-python with MIT License | 6 votes |
def test_invalid_traceid_spanid(self): with raises(SpanContextCorruptedException): self.tracer().extract( Format.HTTP_HEADERS, { "x-b3-spanid": format(345, "x"), "checked": "baggage" } ) with raises(SpanContextCorruptedException): self.tracer().extract( Format.HTTP_HEADERS, { "x-b3-traceid": format(345, "x"), "checked": "baggage" } )
Example #3
Source File: tracer.py From basictracer-python with Apache License 2.0 | 5 votes |
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 #4
Source File: test_codecs.py From jaeger-client-python with Apache License 2.0 | 5 votes |
def test_context_from_readable_headers(self): # provide headers all the way through Config object config = Config( service_name='test', config={ 'trace_id_header': 'Trace_ID', 'baggage_header_prefix': 'Trace-Attr-', }) tracer = config.create_tracer( reporter=InMemoryReporter(), sampler=ConstSampler(True), ) for url_encoding in [False, True]: if url_encoding: codec = tracer.codecs[Format.HTTP_HEADERS] headers = { 'Trace-ID': '100%3A7f:0:1', 'trace-attr-Kiff': 'Amy%20Wang', 'trace-atTR-HERMES': 'LaBarbara%20Hermes' } else: codec = tracer.codecs[Format.HTTP_HEADERS] headers = { 'Trace-ID': '100:7f:0:1', 'trace-attr-Kiff': 'Amy Wang', 'trace-atTR-HERMES': 'LaBarbara Hermes' } ctx = codec.extract(headers) assert ctx.trace_id == 256 assert ctx.span_id == 127 assert ctx.parent_id is None assert ctx.flags == 1 assert ctx.baggage == { 'kiff': 'Amy Wang', 'hermes': 'LaBarbara Hermes', }
Example #5
Source File: tracer.py From apm-agent-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
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 #6
Source File: tracer.py From apm-agent-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
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 #7
Source File: tests.py From apm-agent-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_tracer_inject_http(tracer): span_context = OTSpanContext( trace_parent=TraceParent.from_string("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01") ) carrier = {} tracer.inject(span_context, Format.HTTP_HEADERS, 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 #8
Source File: tests.py From apm-agent-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_tracer_extract_corrupted(tracer): with pytest.raises(opentracing.SpanContextCorruptedException): tracer.extract(Format.HTTP_HEADERS, {"nothing-to": "see-here"})
Example #9
Source File: tests.py From apm-agent-python with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_tracer_extract_http(tracer): span_context = tracer.extract( Format.HTTP_HEADERS, {"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: tracer.py From opentracing-python with Apache License 2.0 | 5 votes |
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 #11
Source File: b3_propagator_test.py From lightstep-tracer-python with MIT License | 5 votes |
def test_extract_single_header(self): result = self.tracer().extract( Format.HTTP_HEADERS, { "b3": "a12-b34-1-c56", "checked": "baggage" } ) self.assertEqual(2578, result.trace_id) self.assertEqual(2868, result.span_id) self.assertDictEqual( { "x-b3-sampled": 1, "x-b3-parentspanid": 3158, "checked": "baggage" }, result.baggage ) result = self.tracer().extract( Format.HTTP_HEADERS, { "b3": "a12-b34-d-c56", "checked": "baggage" } ) self.assertEqual(2578, result.trace_id) self.assertEqual(2868, result.span_id) self.assertDictEqual( { "x-b3-flags": 1, "x-b3-parentspanid": 3158, "checked": "baggage" }, result.baggage )
Example #12
Source File: b3_propagator_test.py From lightstep-tracer-python with MIT License | 5 votes |
def test_inject(self): carrier = {} span = self.tracer().start_span("test_inject") span.set_baggage_item("checked", "baggage") self.tracer().inject(span.context, Format.HTTP_HEADERS, carrier) self.assertEqual( carrier, { "x-b3-traceid": format(span.context.trace_id, "x"), "x-b3-spanid": format(span.context.span_id, "x"), "x-b3-sampled": 1, "checked": "baggage" } ) carrier = {} span = self.tracer().start_span("test_inject") span.set_baggage_item("x-b3-flags", 1) span.set_baggage_item("x-b3-sampled", 0) self.tracer().inject(span.context, Format.HTTP_HEADERS, carrier) self.assertEqual( carrier, { "x-b3-traceid": format(span.context.trace_id, "x"), "x-b3-spanid": format(span.context.span_id, "x"), "x-b3-flags": 1, } )
Example #13
Source File: b3_propagator_test.py From lightstep-tracer-python with MIT License | 5 votes |
def setUp(self): self._tracer = Tracer( periodic_flush_seconds=0, collector_host="localhost" ) self._tracer.register_propagator(Format.HTTP_HEADERS, B3Propagator())
Example #14
Source File: context_in_headers.py From lightstep-tracer-python with MIT License | 5 votes |
def before_answering_request(handler, tracer): """Context manager creates a Span, using SpanContext encoded in handler if possible. """ operation = 'handle_request:' + handler.path carrier_dict = {} for k, v in handler.headers.items(): carrier_dict[k] = v extracted_context = tracer.extract(opentracing.Format.HTTP_HEADERS, carrier_dict) span = None if extracted_context: span = tracer.start_span( operation_name=operation, child_of=extracted_context) else: print('ERROR: Context missing, starting new trace') global _exit_code _exit_code = errno.ENOMSG span = tracer.start_span(operation_name=operation) headers = ', '.join({k + '=' + v for k, v in handler.headers.items()}) span.log_event('extract_failed', headers) print('Could not extract context from http headers: ' + headers) host, port = handler.client_address if host: span.set_tag(opentracing.ext.tags.PEER_HOST_IPV4, host) if port: span.set_tag(opentracing.ext.tags.PEER_PORT, str(port)) return span
Example #15
Source File: tracer.py From lightstep-tracer-python with MIT License | 5 votes |
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 #16
Source File: api_check.py From opentracing-python with Apache License 2.0 | 5 votes |
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 #17
Source File: http_server.py From opentracing-python-instrumentation with MIT License | 4 votes |
def before_request(request, tracer=None): """ Attempts to extract a tracing span from incoming request. If no tracing context is passed in the headers, or the data cannot be parsed, a new root span is started. :param request: HTTP request with `.headers` property exposed that satisfies a regular dictionary interface :param tracer: optional tracer instance to use. If not specified the global opentracing.tracer will be used. :return: returns a new, already started span. """ if tracer is None: tracer = opentracing.tracer # we need to prepare tags upfront, mainly because RPC_SERVER tag must be # set when starting the span, to support Zipkin's one-span-per-RPC model tags_dict = { tags.SPAN_KIND: tags.SPAN_KIND_RPC_SERVER, tags.HTTP_URL: request.full_url, tags.HTTP_METHOD: request.method, } remote_ip = request.remote_ip if remote_ip: tags_dict[tags.PEER_HOST_IPV4] = remote_ip caller_name = request.caller_name if caller_name: tags_dict[tags.PEER_SERVICE] = caller_name remote_port = request.remote_port if remote_port: tags_dict[tags.PEER_PORT] = remote_port operation = request.operation try: carrier = {} for key, value in six.iteritems(request.headers): carrier[key] = value parent_ctx = tracer.extract( format=Format.HTTP_HEADERS, carrier=carrier ) except Exception as e: logging.exception('trace extract failed: %s' % e) parent_ctx = None span = tracer.start_span( operation_name=operation, child_of=parent_ctx, tags=tags_dict) return span
Example #18
Source File: http_client.py From opentracing-python-instrumentation with MIT License | 4 votes |
def before_http_request(request, current_span_extractor): """ A hook to be executed before HTTP request is executed. It returns a Span object that can be used as a context manager around the actual HTTP call implementation, or in case of async callback, it needs its `finish()` method to be called explicitly. :param request: request must match API defined by AbstractRequestWrapper :param current_span_extractor: function that extracts current span from some context :return: returns child tracing span encapsulating this request """ span = utils.start_child_span( operation_name=request.operation, parent=current_span_extractor() ) span.set_tag(tags.SPAN_KIND, tags.SPAN_KIND_RPC_CLIENT) span.set_tag(tags.HTTP_URL, request.full_url) service_name = request.service_name host, port = request.host_port if service_name: span.set_tag(tags.PEER_SERVICE, service_name) if host: span.set_tag(tags.PEER_HOST_IPV4, host) if port: span.set_tag(tags.PEER_PORT, port) # fire interceptors for interceptor in ClientInterceptors.get_interceptors(): interceptor.process(request=request, span=span) try: carrier = {} opentracing.tracer.inject(span_context=span.context, format=Format.HTTP_HEADERS, carrier=carrier) for key, value in six.iteritems(carrier): request.add_header(key, value) except opentracing.UnsupportedFormatException: pass return span
Example #19
Source File: test_middleware.py From opentracing-python-instrumentation with MIT License | 4 votes |
def test_middleware(with_peer_tags, with_context): """ Tests http_server.before_request call :param with_peer_tags: whether Request object exposes peer properties :param with_context: whether the inbound request contains tracing context :return: """ request = mock.MagicMock() request.method = 'GET' request.full_url = 'http://localhost:12345/test' request.operation = 'my-test' if with_peer_tags: request.remote_ip = 'localhost' request.remote_port = 12345 request.caller_name = 'test_middleware' else: request.remote_ip = None request.remote_port = None request.caller_name = None tracer = opentracing.tracer if with_context: span_ctx = mock.MagicMock() else: span_ctx = None p_extract = mock.patch.object(tracer, 'extract', return_value=span_ctx) span = mock.MagicMock() p_start_span = mock.patch.object(tracer, 'start_span', return_value=span) with p_extract as extract_call, p_start_span as start_span_call: span2 = http_server.before_request(request=request, tracer=tracer) assert span == span2 extract_call.assert_called_with( format=Format.HTTP_HEADERS, carrier={}) expected_tags = { 'http.method': 'GET', 'http.url': 'http://localhost:12345/test', 'span.kind': 'server', } if with_peer_tags: expected_tags.update({ 'peer.service': 'test_middleware', 'span.kind': 'server', 'peer.ipv4': 'localhost', 'peer.port': 12345, }) start_span_call.assert_called_with( operation_name='my-test', tags=expected_tags, child_of=span_ctx )