def test_b3_extract(self): codec = B3Codec() with self.assertRaises(InvalidCarrierException): codec.extract([]) carrier = { 'x-b3-spanid': 'a2fb4a1d1a96d312', 'x-b3-parentspanid': '0020000000000001', 'x-b3-traceid': '463ac35c9f6413ad48485a3953bb6124', 'x-b3-flags': '1' } span_context = codec.extract(carrier) assert span_context.span_id == int('a2fb4a1d1a96d312', 16) assert span_context.trace_id == int('463ac35c9f6413ad48485a3953bb6124', 16) assert span_context.parent_id == int('0020000000000001', 16) assert span_context.flags == 0x02 carrier.update({'x-b3-sampled': '1'}) span_context = codec.extract(carrier) assert span_context.flags == 0x03 # validate invalid hex string with self.assertRaises(SpanContextCorruptedException): codec.extract({'x-b3-traceid': 'a2fb4a1d1a96d312z'}) # validate non-string header with self.assertRaises(SpanContextCorruptedException): codec.extract({'x-b3-traceid': 123})
def test_zipkin_b3_codec_extract_injected(self): codec = B3Codec() ctx = SpanContext(trace_id=256, span_id=127, parent_id=None, flags=0) span = Span(context=ctx, operation_name='x', tracer=None, start_time=1) carrier = {} codec.inject(span_context=span, carrier=carrier) extracted = codec.extract(carrier) assert extracted.trace_id == ctx.trace_id assert extracted.span_id == ctx.span_id assert extracted.parent_id == ctx.parent_id assert extracted.flags == ctx.flags
def test_zipkin_b3_codec_inject(self): codec = B3Codec() with self.assertRaises(InvalidCarrierException): codec.inject(span_context=None, carrier=[]) ctx = SpanContext(trace_id=256, span_id=127, parent_id=None, flags=1) span = Span(context=ctx, operation_name='x', tracer=None, start_time=1) carrier = {} codec.inject(span_context=span, carrier=carrier) assert carrier == { 'X-B3-SpanId': format(127, 'x').zfill(16), 'X-B3-TraceId': format(256, 'x').zfill(16), 'X-B3-Flags': '1' }
def _init_tracer(self): service_name = '' try: value = getattr(settings, 'SERVICE_CONF', '') if value: service_name = value.get('NAME', '') except Exception as e: logger.error('SERVICE_CONF:NAME is not set in settings. {}'.format(str(e))) # create a global tracer first tracer = Tracer( one_span_per_rpc=True, service_name=service_name, reporter=NullReporter(), sampler=ConstSampler(decision=True), extra_codecs={Format.HTTP_HEADERS: B3Codec()} ) opentracing.set_global_tracer(tracer)
def test_b3_extract(self): codec = B3Codec() with self.assertRaises(InvalidCarrierException): codec.extract([]) # Implicit case insensitivity testing carrier = { 'X-b3-SpanId': 'a2fb4a1d1a96d312', 'X-B3-ParentSpanId': '0020000000000001', 'X-B3-traceId': '463ac35c9f6413ad48485a3953bb6124', 'X-B3-flags': '1' } span_context = codec.extract(carrier) assert span_context.span_id == int('a2fb4a1d1a96d312', 16) assert span_context.trace_id == int('463ac35c9f6413ad48485a3953bb6124', 16) assert span_context.parent_id == int('0020000000000001', 16) assert span_context.flags == 0x02 # validate that missing parentspanid does not cause an error carrier.pop('X-B3-ParentSpanId') span_context = codec.extract(carrier) assert span_context.parent_id is None carrier.update({'X-b3-sampled': '1'}) span_context = codec.extract(carrier) assert span_context.flags == 0x03 carrier.pop('X-B3-flags') span_context = codec.extract(carrier) assert span_context.flags == 0x01 # validate invalid hex string with self.assertRaises(SpanContextCorruptedException): codec.extract({'x-B3-TraceId': 'a2fb4a1d1a96d312z'}) # validate non-string header with self.assertRaises(SpanContextCorruptedException): codec.extract({'x-B3-traceId': 123})
def init_jaeger_tracer(): config = Config( config={ # usually read from some yaml config 'sampler': { 'type': 'const', 'param': 1, }, 'local_agent': { 'reporting_host': 'jaeger-agent.istio-system', 'reporting_port': '5775', }, 'logging': True }, service_name='frontend.default', validate=True, ) tracer = config.initialize_tracer() if tracer is None: Config._initialized = False tracer = config.initialize_tracer() tracer.codecs = {Format.HTTP_HEADERS: B3Codec()} return tracer
# This example code uses OpenTracing (http://opentracing.io/) to propagate # the 'b3' (zipkin) headers. Using OpenTracing for this is not a requirement. # Using OpenTracing allows you to add application-specific tracing later on, # but you can just manually forward the headers if you prefer. # # The OpenTracing example here is very basic. It only forwards headers. It is # intended as a reference to help people get started, eg how to create spans, # extract/inject context, etc. # A very basic OpenTracing tracer (with null reporter) tracer = Tracer( one_span_per_rpc=True, service_name='productpage', reporter=NullReporter(), sampler=ConstSampler(decision=True), extra_codecs={Format.HTTP_HEADERS: B3Codec()} ) def trace(): ''' Function decorator that creates opentracing span from incoming b3 headers ''' def decorator(f): def wrapper(*args, **kwargs): request = stack.top.request try: # Create a new span context, reading in values (traceid, # spanid, etc) from the incoming x-b3-*** headers. span_ctx = tracer.extract( Format.HTTP_HEADERS,
def __init__(self, sname): self.tracer = Tracer(one_span_per_rpc=True, service_name=sname, reporter=NullReporter(), sampler=ConstSampler(decision=True), extra_codecs={Format.HTTP_HEADERS: B3Codec()})