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 test_stress_logs(self): recorder = self.create_test_recorder() for i in range(1000): recorder.record_span(self.dummy_basic_span(recorder, i)) self.assertTrue(recorder.flush(self.mock_connection)) self.assertEqual(len(self.mock_connection.reports[0].span_records), 1000) self.check_spans(self.mock_connection.reports[0].span_records)
def test_stress_spans(recorder): mock_connection = MockConnection() mock_connection.open() for i in range(1000): dummy_basic_span(recorder, i) assert recorder.flush(mock_connection) assert recorder.converter.num_span_records( mock_connection.reports[0]) == 1000 check_spans(recorder.converter, mock_connection.reports[0])
def test_buffer_limits(recorder): mock_connection = MockConnection() mock_connection.open() recorder._max_span_records = 88 assert len(recorder._span_records) == 0 for i in range(0, 100): dummy_basic_span(recorder, i) assert len(recorder._span_records) == 88 assert recorder.flush(mock_connection)
def test_send_spans_after_shutdown(self): recorder = self.create_test_recorder() # Send 10 spans for i in range(10): recorder.record_span(self.dummy_basic_span(recorder, i)) self.assertTrue(recorder.flush(self.mock_connection)) # Check 10 spans self.check_spans(self.mock_connection.reports[0].span_records) # Delete current logs and shutdown runtime self.mock_connection.clear() recorder.shutdown() # Send 10 spans, though none should get through for i in range(10): recorder.record_span(self.dummy_basic_span(recorder, i)) self.assertFalse(recorder.flush(self.mock_connection)) self.assertEqual(len(self.mock_connection.reports), 0)
def test_buffer_limits(self): self.runtime_args.update({ 'max_span_records': 88, }) recorder = self.create_test_recorder() self.assertEqual(len(recorder._span_records), 0) for i in range(0, 10000): recorder.record_span(self.dummy_basic_span(recorder, i)) self.assertEqual(len(recorder._span_records), 88) self.assertTrue(recorder.flush(self.mock_connection))
def test_send_spans_after_shutdown(recorder): mock_connection = MockConnection() mock_connection.open() # Send 10 spans for i in range(10): dummy_basic_span(recorder, i) assert recorder.flush(mock_connection) # Check 10 spans check_spans(recorder.converter, mock_connection.reports[0]) # Delete current logs and shutdown runtime mock_connection.clear() recorder.shutdown() # Send 10 spans, though none should get through for i in range(10): recorder.record_span(dummy_basic_span(recorder, i)) assert not recorder.flush(mock_connection) assert len(mock_connection.reports) == 0
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))