def test_context(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, "name") span._register(mock_observer) with span: self.assertEqual(mock_observer.on_start.call_count, 1) self.assertEqual(mock_observer.on_stop.call_count, 1)
def test_should_sample_utilizes_force_sampling(self): client = make_client("test-service", sample_rate=0) baseplate_observer = TraceBaseplateObserver(client) span_with_forced = Span('test-id', 'test-parent', 'test-span-id', False, 1, "test", self.mock_context) span_without_forced = Span('test-id', 'test-parent', 'test-span-id', False, 0, "test", self.mock_context) self.assertTrue(baseplate_observer.should_sample(span_with_forced)) self.assertFalse(baseplate_observer.should_sample(span_without_forced))
def test_context(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, "name") span.register(mock_observer) with span: self.assertEqual(mock_observer.on_start.call_count, 1) self.assertEqual(mock_observer.on_finish.call_count, 1)
def test_should_sample_utilizes_force_sampling(self): baseplate_observer = TraceBaseplateObserver('test-service', sample_rate=0) span_with_forced = Span('test-id', 'test-parent', 'test-span-id', False, 1, "test") span_without_forced = Span('test-id', 'test-parent', 'test-span-id', False, 0, "test") self.assertTrue(baseplate_observer.should_sample(span_with_forced)) self.assertFalse(baseplate_observer.should_sample(span_without_forced))
def test_force_sampling(self): span_with_debug_flag = Span('test-id', 'test-parent', 'test-span-id', True, 1, "test") span_without_debug_flag = Span('test-id', 'test-parent', 'test-span-id', True, 0, "test") self.assertTrue( TraceBaseplateObserver.force_sampling(span_with_debug_flag)) self.assertFalse( TraceBaseplateObserver.force_sampling(span_without_debug_flag))
def test_force_sampling(self): span_with_debug_flag = Span("test-id", "test-parent", "test-span-id", True, 1, "test", self.mock_context) span_without_debug_flag = Span("test-id", "test-parent", "test-span-id", True, 0, "test", self.mock_context) self.assertTrue( TraceBaseplateObserver.force_sampling(span_with_debug_flag)) self.assertFalse( TraceBaseplateObserver.force_sampling(span_without_debug_flag))
def test_events(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, "name") span.register(mock_observer) span.start() self.assertEqual(mock_observer.on_start.call_count, 1) span.annotate("key", "value") mock_observer.on_annotate("key", "value") span.stop() mock_observer.on_stop(error=None)
def test_context_with_exception(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, "name") span.register(mock_observer) class TestException(Exception): pass exc = TestException() with self.assertRaises(TestException): with span: raise exc self.assertEqual(mock_observer.on_stop.call_count, 1) self.assertEqual(mock_observer.on_stop.call_args, mock.call(error=exc))
def test_context_with_exception(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, "name") span._register(mock_observer) class TestException(Exception): pass exc = TestException() with self.assertRaises(TestException): with span: raise exc self.assertEqual(mock_observer.on_stop.call_count, 1) self.assertEqual(mock_observer.on_stop.call_args, mock.call(error=exc))
def setUp(self): self.recorder = NullRecorder() self.span = Span('test-id', 'test-parent-id', 'test-span-id', None, 0, 'test') self.test_span_observer = TraceSpanObserver('test-service', 'test-hostname', self.span, self.recorder)
def test_should_sample_utilizes_sample_rate(self): baseplate_observer = TraceBaseplateObserver('test-service', sample_rate=1) span = Span('test-id', 'test-parent', 'test-span-id', None, 0, "test") self.assertTrue(baseplate_observer.should_sample(span)) baseplate_observer.sample_rate = 0 self.assertFalse(baseplate_observer.should_sample(span))
def test_should_sample_utilizes_sampled_setting(self): baseplate_observer = TraceBaseplateObserver('test-service', sample_rate=0) span_with_sampled_flag = Span('test-id', 'test-parent', 'test-span-id', True, 0, "test") self.assertTrue( baseplate_observer.should_sample(span_with_sampled_flag))
def test_context_with_exception(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, None, 0, "name") span.register(mock_observer) class TestException(Exception): pass exc = TestException() with self.assertRaises(TestException): with span: raise exc self.assertEqual(mock_observer.on_finish.call_count, 1) _, captured_exc, _ = mock_observer.on_finish.call_args[0][0] self.assertEqual(captured_exc, exc)
def make_test_span(context=None, local=False): if not context: context = mock.Mock() span = Span(1, 2, 3, None, 0, "name", context) if local: span = LocalSpan(1, 2, 3, None, 0, "name", context) return span
def test_should_sample_utilizes_sampled_setting(self): client = make_client("test-service", sample_rate=0) baseplate_observer = TraceBaseplateObserver(client) span_with_sampled_flag = Span('test-id', 'test-parent', 'test-span-id', True, 0, "test", self.mock_context) self.assertTrue( baseplate_observer.should_sample(span_with_sampled_flag))
def test_context_with_exception(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, "name") span.register(mock_observer) class TestException(Exception): pass exc = TestException() with self.assertRaises(TestException): with span: raise exc self.assertEqual(mock_observer.on_finish.call_count, 1) _, captured_exc, _ = mock_observer.on_finish.call_args[0][0] self.assertEqual(captured_exc, exc)
def test_should_sample_utilizes_sample_rate(self): client = make_client("test-service", sample_rate=1) baseplate_observer = TraceBaseplateObserver(client) span = Span('test-id', 'test-parent', 'test-span-id', None, 0, "test", self.mock_context) self.assertTrue(baseplate_observer.should_sample(span)) baseplate_observer.sample_rate = 0 self.assertFalse(baseplate_observer.should_sample(span))
def test_on_child_span_created(self): child_span = Span("child-id", "test-parent-id", "test-span-id", None, 0, "test-child", self.mock_context) self.test_server_span_observer.on_child_span_created(child_span) # Make sure new trace observer is added in the child span # and the trace observer's span is that child span self.assertEqual(len(child_span.observers), 1) self.assertEqual(child_span.observers[0].span, child_span)
def test_events(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, "name") span._register(mock_observer) span.start() self.assertEqual(mock_observer.on_start.call_count, 1) span.annotate("key", "value") mock_observer.on_annotate("key", "value") span.stop() mock_observer.on_stop(error=None)
def test_null_recorder_flush(self): span = Span('test-id', 'test-parent-id', 'test-span-id', None, 0, 'test', self.mock_context) self.recorder.flush_func([span])
def setUp(self): super(TraceSpanObserverTests, self).setUp() self.recorder = NullRecorder() self.mock_context = mock.Mock() self.span = Span("test-id", "test-parent-id", "test-span-id", None, 0, "test", self.mock_context) self.test_span_observer = TraceSpanObserver("test-service", "test-hostname", self.span, self.recorder)
def test_events(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, "name") span.register(mock_observer) span.start() self.assertEqual(mock_observer.on_start.call_count, 1) span.set_tag("key", "value") mock_observer.on_set_tag("key", "value") span.log("name", "payload") mock_observer.on_log("name", "payload") span.finish() mock_observer.on_finish(exc_info=None)
def test_null_recorder_flush(self): span = Span("test-id", "test-parent-id", "test-span-id", None, 0, "test", self.mock_context) self.recorder.flush_func([span])
def test_events(self): mock_observer = mock.Mock(spec=SpanObserver) span = Span(1, 2, 3, None, 0, "name") span.register(mock_observer) span.start() self.assertEqual(mock_observer.on_start.call_count, 1) span.set_tag("key", "value") mock_observer.on_set_tag("key", "value") span.log("name", "payload") mock_observer.on_log("name", "payload") span.finish() mock_observer.on_finish(exc_info=None)