def test_baggage_as_unicode_strings_with_httplib(httpserver): if six.PY2: import urllib2 urllib_under_test = urllib2 else: import urllib.request urllib_under_test = urllib.request # httpserver is provided by pytest-localserver httpserver.serve_content(content='Hello', code=200, headers=None) tracer = Tracer( service_name='test', reporter=InMemoryReporter(), # don't sample to avoid logging baggage to the span sampler=ConstSampler(False), ) tracer.codecs[Format.TEXT_MAP] = TextCodec(url_encoding=True) baggage = [(b'key1', b'value'), (u'key2', b'value'), ('key3', u'value'), (b'key4', bytes(chr(255)) if six.PY2 else bytes([255])), (u'key5', u'\U0001F47E')] for b in baggage: span = tracer.start_span('test') span.set_baggage_item(b[0], b[1]) headers = {} tracer.inject(span_context=span.context, format=Format.TEXT_MAP, carrier=headers) # make sure httplib doesn't blow up request = urllib_under_test.Request(httpserver.url, None, headers) response = urllib_under_test.urlopen(request) assert response.read() == b'Hello' response.close()
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', }
def test_non_ascii_baggage_with_httplib(httpserver): # httpserver is provided by pytest-localserver httpserver.serve_content(content='Hello', code=200, headers=None) tracer = Tracer( service_name='test', reporter=InMemoryReporter(), # don't sample to avoid logging baggage to the span sampler=ConstSampler(False), ) tracer.codecs[Format.TEXT_MAP] = TextCodec(url_encoding=True) baggage = [ (b'key', b'value'), (u'key', b'value'), (b'key', bytes(chr(255))), (u'caf\xe9', 'caf\xc3\xa9'), ('caf\xc3\xa9', 'value'), ] for b in baggage: span = tracer.start_span('test') span.set_baggage_item(b[0], b[1]) headers = {} tracer.inject(span_context=span.context, format=Format.TEXT_MAP, carrier=headers) # make sure httplib doesn't blow up request = urllib2.Request(httpserver.url, None, headers) response = urllib2.urlopen(request) assert response.read() == 'Hello' response.close()
def tracer(): tracer = Tracer(service_name='test-tracer', sampler=ConstSampler(True), reporter=InMemoryReporter(), scope_manager=scope_manager() if scope_manager else None) try: yield tracer finally: tracer.close()
def tracer(): reporter = InMemoryReporter() report_func = reporter.report_span def log_and_report(span): print 'Reporting span %s' % span report_func(span) reporter.report_span = log_and_report tracer = Tracer( service_name='test-tracer', sampler=ConstSampler(True), reporter=reporter, ) try: yield tracer finally: tracer.close()
def tracer(): tracer = Tracer( service_name='test-tracer', sampler=ConstSampler(True), reporter=InMemoryReporter(), ) try: yield tracer finally: tracer.close()
def tracer(): reporter = InMemoryReporter() report_func = reporter.report_span def log_and_report(span): print 'Reporting span %s' % span report_func(span) reporter.report_span = log_and_report tracer = Tracer( service_name='test-tracer', sampler=ConstSampler(True), reporter=reporter, ) try: yield tracer finally: tracer.close()
def tracer(): tracer = Tracer(service_name='test-tracer', sampler=ConstSampler(True), reporter=InMemoryReporter(), scope_manager=TornadoScopeManager()) opentracing.set_global_tracer(tracer) try: yield tracer finally: opentracing._reset_global_tracer() tracer.close()
def test_round_trip(tracer, fmt, carrier): tracer_128bit = Tracer(service_name='test', reporter=InMemoryReporter(), sampler=ConstSampler(True), generate_128bit_trace_id=True) for tracer1, tracer2 in product([tracer, tracer_128bit], repeat=2): span = tracer1.start_span('test-%s' % fmt) tracer1.inject(span, fmt, carrier) context = tracer2.extract(fmt, carrier) span2 = tracer2.start_span('test-%s' % fmt, child_of=context) assert span.trace_id == span2.trace_id
def init(): config = Config(service_name='test', config={ 'trace_id_header': 'Trace_ID', 'baggage_header_prefix': 'Trace-Attr-', }) reporter = InMemoryReporter() tracer = config.create_tracer( reporter=reporter, sampler=ConstSampler(True), ) tracing = FlaskTracing(tracer, True, app)
def tracer(): reporter = InMemoryReporter() report_func = reporter.report_span def log_and_report(span): print(('Reporting span %s' % span)) print(('Span type %s' % type(span))) print(('SpanContext type %s' % type(span.context))) report_func(span) reporter.report_span = log_and_report tracer = Tracer( service_name='test-tracer', sampler=ConstSampler(True), reporter=reporter, scope_manager=TornadoScopeManager() ) opentracing.set_global_tracer(tracer) try: yield tracer finally: opentracing._reset_global_tracer() tracer.close()
def test_non_ascii_baggage_with_httplib(httpserver): # TODO this test requires `futurize`. Unfortunately, that also changes # how the test works under Py2. # Some observation: # - In Py2, the httplib does not like unicode strings, maybe we need to convert everything to bytes. # - Not sure yet what's the story with httplib in Py3, it seems not to like raw bytes. if six.PY3: raise ValueError('this test does not work with Py3') # httpserver is provided by pytest-localserver httpserver.serve_content(content='Hello', code=200, headers=None) tracer = Tracer( service_name='test', reporter=InMemoryReporter(), # don't sample to avoid logging baggage to the span sampler=ConstSampler(False), ) tracer.codecs[Format.TEXT_MAP] = TextCodec(url_encoding=True) baggage = [ (b'key', b'value'), (u'key', b'value'), (b'key', byte255), (u'caf\xe9', 'caf\xc3\xa9'), ('caf\xc3\xa9', 'value'), ] for b in baggage: span = tracer.start_span('test') span.set_baggage_item(b[0], b[1]) headers = {} tracer.inject(span_context=span.context, format=Format.TEXT_MAP, carrier=headers) # make sure httplib doesn't blow up import urllib2 request = urllib2.Request(httpserver.url, None, headers) response = urllib2.urlopen(request) assert response.read() == b'Hello' response.close()
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'
def test_inject_with_128bit_trace_id(tracer, fmt, carrier, get_trace_id): tracer_128bit = Tracer(service_name='test', reporter=InMemoryReporter(), sampler=ConstSampler(True), generate_128bit_trace_id=True) for tracer in [tracer, tracer_128bit]: length = tracer.max_trace_id_bits / 4 trace_id = (1 << 64) - 1 if length == 16 else (1 << 128) - 1 ctx = SpanContext(trace_id=trace_id, span_id=127, parent_id=None, flags=1) span = Span(ctx, operation_name='test-%s' % fmt, tracer=None, start_time=1) tracer.inject(span, fmt, carrier) assert len(get_trace_id(carrier)) == length # test if the trace_id arrived on wire remains same even if # the tracer is configured for 64bit ids or 128bit ids ctx = SpanContext(trace_id=(1 << 128) - 1, span_id=127, parent_id=None, flags=0) span = tracer.start_span('test-%s' % fmt, child_of=ctx) carrier = dict() tracer.inject(span, fmt, carrier) assert len(get_trace_id(carrier)) == 32 ctx = SpanContext(trace_id=(1 << 64) - 1, span_id=127, parent_id=None, flags=0) span = tracer.start_span('test-%s' % fmt, child_of=ctx) carrier = dict() tracer.inject(span, fmt, carrier) assert len(get_trace_id(carrier)) == 16
def reporter(): reporter = InMemoryReporter() with mock.patch('jaeger_client.config.Reporter', return_value=reporter): yield reporter
def reporter(): reporter = InMemoryReporter() opentracing.tracer._tracer.reporter = reporter return reporter