def main(): # Setup BasicTracer to log to console logger = logging.getLogger('span_logging') logger.setLevel(logging.DEBUG) hndlr = logging.StreamHandler(sys.stdout) hndlr.setFormatter( logging.Formatter("[%(levelname)-8s] %(name)s: %(message)s")) logger.addHandler(hndlr) recorder = LogSpanRecorder(logger) tracer = BasicTracer(recorder=recorder) tracer.register_required_propagators() # Use tracer to create spans span = tracer.start_span(operation_name='main') for i in range(10): child_span = tracer.start_span(operation_name='loop', child_of=span) child_span.set_tag('i', i) child_span.log_kv({'message': "Sleeping for 1 second"}) time.sleep(1) child_span.finish() span.finish()
def test_start_span(): """ Test in process child span creation.""" tracer = BasicTracer() tracer.register_required_propagators() sp = tracer.start_span(operation_name='test') sp.set_baggage_item('foo', 'bar') child = tracer.start_span(operation_name='child', child_of=sp.context) assert child.context.trace_id == sp.context.trace_id assert child.context.sampled == sp.context.sampled assert child.context.baggage == sp.context.baggage assert child.parent_id == sp.context.span_id
def test_start_span(): """ Test in process child span creation.""" tracer = BasicTracer() tracer.register_required_propagators() sp = tracer.start_span(operation_name='test') sp.set_baggage_item('foo', 'bar') child = tracer.start_span( operation_name='child', child_of=sp.context) assert child.context.trace_id == sp.context.trace_id assert child.context.sampled == sp.context.sampled assert child.context.baggage == sp.context.baggage assert child.parent_id == sp.context.span_id
def test_propagation(): tracer = BasicTracer() tracer.register_required_propagators() sp = tracer.start_span(operation_name='test') sp.context.sampled = False sp.set_baggage_item('foo', 'bar') # Test invalid types with pytest.raises(UnsupportedFormatException): tracer.inject(sp.context, 'invalid', {}) with pytest.raises(UnsupportedFormatException): tracer.extract('invalid', {}) tests = [(Format.BINARY, bytearray()), (Format.TEXT_MAP, {})] for format, carrier in tests: tracer.inject(sp.context, format, carrier) extracted_ctx = tracer.extract(format, carrier) assert extracted_ctx.trace_id == sp.context.trace_id assert extracted_ctx.span_id == sp.context.span_id assert extracted_ctx.sampled == sp.context.sampled assert extracted_ctx.baggage == sp.context.baggage # Test string value of sampled field headers = {} tracer.inject(sp.context, Format.HTTP_HEADERS, headers) headers['ot-tracer-sampled'] = '0' span_ctx0 = tracer.extract(Format.HTTP_HEADERS, headers) assert not span_ctx0.sampled headers['ot-tracer-sampled'] = '1' span_ctx1 = tracer.extract(Format.HTTP_HEADERS, headers) assert span_ctx1.sampled
def test_propagation(): tracer = BasicTracer() tracer.register_required_propagators() sp = tracer.start_span(operation_name='test') sp.context.sampled = False sp.set_baggage_item('foo', 'bar') # Test invalid types with pytest.raises(UnsupportedFormatException): tracer.inject(sp.context, 'invalid', {}) with pytest.raises(UnsupportedFormatException): tracer.extract('invalid', {}) tests = [(Format.BINARY, bytearray()), (Format.TEXT_MAP, {})] for format, carrier in tests: tracer.inject(sp.context, format, carrier) extracted_ctx = tracer.extract(format, carrier) assert extracted_ctx.trace_id == sp.context.trace_id assert extracted_ctx.span_id == sp.context.span_id assert extracted_ctx.sampled == sp.context.sampled assert extracted_ctx.baggage == sp.context.baggage # Test string value of sampled field headers = {} tracer.inject(sp.context, Format.HTTP_HEADERS, headers) headers['ot-tracer-sampled'] = '0' span_ctx0 = tracer.extract(Format.HTTP_HEADERS, headers) assert not span_ctx0.sampled headers['ot-tracer-sampled'] = '1' span_ctx1 = tracer.extract(Format.HTTP_HEADERS, headers) assert span_ctx1.sampled
def start_span(self, *args, **kwargs): span = BasicTracer.start_span(self, *args, **kwargs) if span.parent_id is None: span.context.trace_id = '1-%x-%s' % (int( time.time()), binascii.b2a_hex(os.urandom(12)).decode('ascii')) span.context.span_id = binascii.b2a_hex(os.urandom(8)).decode('ascii') return span
def test_propagation(): tracer = BasicTracer() sp = tracer.start_span(operation_name="test") sp.set_baggage_item("foo", "bar") opname = 'op' tests = [(Format.BINARY, bytearray()), (Format.TEXT_MAP, {})] for format, carrier in tests: tracer.inject(sp, format, carrier) child = tracer.join(opname, format, carrier) assert child.raw.context.trace_id == sp.raw.context.trace_id assert child.raw.context.parent_id == sp.raw.context.span_id assert child.raw.context.sampled == sp.raw.context.sampled assert child.raw.baggage == sp.raw.baggage
def test_span_log_kv(): recorder = InMemoryRecorder() tracer = BasicTracer(recorder=recorder) span = tracer.start_span('x') span.log_kv({ 'foo': 'bar', 'baz': 42, }) span.finish() finished_spans = recorder.get_spans() assert len(finished_spans) == 1 assert len(finished_spans[0].logs) == 1 assert len(finished_spans[0].logs[0].key_values) == 2 assert finished_spans[0].logs[0].key_values['foo'] == 'bar' assert finished_spans[0].logs[0].key_values['baz'] == 42
def test_span_log_kv(): recorder = InMemoryRecorder() tracer = BasicTracer(recorder=recorder) span = tracer.start_span('x') span.log_kv({ 'foo': 'bar', 'baz': 42, }) span.finish() finished_spans = recorder.get_spans() assert len(finished_spans) == 1 assert len(finished_spans[0].logs) == 1 assert len(finished_spans[0].logs[0].key_values) == 2 assert finished_spans[0].logs[0].key_values['foo'] == 'bar' assert finished_spans[0].logs[0].key_values['baz'] == 42
def test_propagation(): tracer = BasicTracer() sp = tracer.start_span(operation_name="test") sp.set_baggage_item("foo", "bar") opname = 'op' tests = [(Format.BINARY, bytearray()), (Format.TEXT_MAP, {})] for format, carrier in tests: tracer.inject(sp, format, carrier) child = tracer.join(opname, format, carrier) assert child.raw.context.trace_id == sp.raw.context.trace_id assert child.raw.context.parent_id == sp.raw.context.span_id assert child.raw.context.sampled == sp.raw.context.sampled assert child.raw.baggage == sp.raw.baggage
def test_span_sampling_priority(): recorder = InMemoryRecorder() tracer = BasicTracer(recorder=recorder) span = tracer.start_span('x') assert span.context.sampled is True span.set_tag(tags.SAMPLING_PRIORITY, 0) assert span.context.sampled is False span.finish() assert len(recorder.get_spans()) == 1 def get_sampled_spans(): return [span for span in recorder.get_spans() if span.context.sampled] assert len(get_sampled_spans()) == 0
def test_span_sampling_priority(): recorder = InMemoryRecorder() tracer = BasicTracer(recorder=recorder) span = tracer.start_span('x') assert span.context.sampled is True span.set_tag(tags.SAMPLING_PRIORITY, 0) assert span.context.sampled is False span.finish() assert len(recorder.get_spans()) == 1 def get_sampled_spans(): return [span for span in recorder.get_spans() if span.context.sampled] assert len(get_sampled_spans()) == 0
def test_propagation(): tracer = BasicTracer() tracer.register_required_propagators() sp = tracer.start_span(operation_name='test') sp.context.sampled = False sp.set_baggage_item('foo', 'bar') # Test invalid types with pytest.raises(UnsupportedFormatException): tracer.inject(sp.context, 'invalid', {}) with pytest.raises(UnsupportedFormatException): tracer.extract('invalid', {}) tests = [(Format.BINARY, bytearray()), (Format.TEXT_MAP, {})] for format, carrier in tests: tracer.inject(sp.context, format, carrier) extracted_ctx = tracer.extract(format, carrier) assert extracted_ctx.trace_id == sp.context.trace_id assert extracted_ctx.span_id == sp.context.span_id assert extracted_ctx.sampled == sp.context.sampled assert extracted_ctx.baggage == sp.context.baggage