def test_non_sampled_span_thrift(recorder): mock_connection = MockConnection() mock_connection.open() non_sampled_span = BasicSpan( lightstep.tracer._LightstepTracer(False, recorder, None), operation_name="non_sampled", context=SpanContext(trace_id=1, span_id=1, sampled=False), start_time=time.time(), ) non_sampled_span.finish() sampled_span = BasicSpan( lightstep.tracer._LightstepTracer(False, recorder, None), operation_name="sampled", context=SpanContext(trace_id=1, span_id=2, sampled=True), start_time=time.time(), ) sampled_span.finish() recorder.record_span(non_sampled_span) recorder.record_span(sampled_span) recorder.flush(mock_connection) if recorder.use_thrift: for span_record in mock_connection.reports[0].span_records: assert span_record.span_name == "sampled" else: for span in mock_connection.reports[0].spans: assert span.operation_name == "sampled"
def dummy_basic_span(recorder, i): span = BasicSpan( lightstep.tracer._LightstepTracer(False, recorder, None), operation_name=str(i), context=SpanContext(trace_id=1000 + i, span_id=2000 + i), start_time=time.time() - 100, ) span.finish() return span
def test_exception_formatting(recorder): mock_connection = MockConnection() mock_connection.open() assert len(recorder._span_records) == 0 span = BasicSpan( lightstep.tracer._LightstepTracer(False, recorder, None), operation_name="exception span", context=SpanContext(trace_id=1000, span_id=2000), start_time=time.time() - 100, ) span.log_kv({ERROR_KIND: AttributeError}) span.finish() assert len(recorder._span_records) == 1 assert recorder.flush(mock_connection) spans = recorder.converter.get_span_records(mock_connection.reports[0]) if hasattr(spans[0], "log_records"): assert len(spans[0].log_records) == 1 assert len(spans[0].log_records[0].fields) == 1 assert spans[0].log_records[0].fields[0] == ttypes.KeyValue( Key="error.kind", Value="AttributeError") else: assert len(spans[0].logs) == 1 assert len(spans[0].logs[0].fields) == 1 assert spans[0].logs[0].fields[0].key == "error.kind" assert spans[0].logs[0].fields[0].string_value == "AttributeError" span = BasicSpan( lightstep.tracer._LightstepTracer(False, recorder, None), operation_name="exception span", context=SpanContext(trace_id=1000, span_id=2000), start_time=time.time() - 100, ) try: raise Exception except Exception: # pylint: disable=broad-except exc_type, exc_value, exc_tb = sys.exc_info() span.log_kv({ STACK: exc_tb, ERROR_KIND: exc_type, ERROR_OBJECT: exc_value }) span.finish() assert len(recorder._span_records) == 1 assert recorder.flush(mock_connection) spans = recorder.converter.get_span_records(mock_connection.reports[1]) if hasattr(spans[0], "log_records"): assert len(spans[0].log_records) == 1 assert len(spans[0].log_records[0].fields) == 3 for field in spans[0].log_records[0].fields: if field.Key == "stack": assert "Traceback (most recent call last):" in field.Value elif field.Key == "error.kind": assert field.Value == "Exception" elif field.Key == "error.object": assert field.Value == "" else: raise AttributeError("unexpected field: %s".format(field.Key)) else: assert len(spans[0].logs) == 1 assert len(spans[0].logs[0].fields) == 3 for field in spans[0].logs[0].fields: if field.key == "stack": assert "Traceback (most recent call last):" in field.string_value elif field.key == "error.kind": assert field.string_value == "Exception" elif field.key == "error.object": assert field.string_value == "" else: raise AttributeError("unexpected field: %s".format(field.key))