def setUp(self): super().setUp() self.recorder = NullRecorder() self.mock_context = mock.Mock() self.span = ServerSpan("test-id", "test-parent-id", "test-span-id", None, 0, "test", self.mock_context) self.test_server_span_observer = TraceServerSpanObserver( "test-service", "test-hostname", self.span, self.recorder)
def test_no_tracing_without_sampling(self): client = make_client("test-service", sample_rate=0) baseplate_observer = TraceBaseplateObserver(client) context_mock = mock.Mock() span = ServerSpan("test-id", "test-parent-id", "test-span-id", False, 0, "test", self.mock_context) baseplate_observer.on_server_span_created(context_mock, span) self.assertEqual(len(span.observers), 0)
def test_register_server_span_observer(self): client = make_client("test-service") baseplate_observer = TraceBaseplateObserver(client) context_mock = mock.Mock() span = ServerSpan("test-id", "test-parent-id", "test-span-id", True, 0, "test", self.mock_context) baseplate_observer.on_server_span_created(context_mock, span) self.assertEqual(len(span.observers), 1) self.assertEqual(type(span.observers[0]), TraceServerSpanObserver)
def test_make_local_span_copies_context(self, mock_getrandbits): mock_getrandbits.return_value = 0xCAFE mock_observer = mock.Mock(spec=ServerSpanObserver) mock_context = mock.Mock() mock_cloned_context = mock.Mock() mock_context.clone.return_value = mock_cloned_context server_span = ServerSpan("trace", "parent", "id", None, 0, "name", mock_context) server_span.register(mock_observer) local_span = server_span.make_child("test_op", local=True, component_name="test_component") self.assertNotEqual(local_span.context, mock_context)
def test_make_child(self, mock_getrandbits): mock_getrandbits.return_value = 0xCAFE mock_observer = mock.Mock(spec=ServerSpanObserver) mock_context = mock.Mock() server_span = ServerSpan("trace", "parent", "id", None, 0, "name", mock_context) server_span.register(mock_observer) child_span = server_span.make_child("child_name") self.assertEqual(child_span.name, "child_name") self.assertEqual(child_span.id, 0xCAFE) self.assertEqual(child_span.trace_id, "trace") self.assertEqual(child_span.parent_id, "id") self.assertEqual(mock_observer.on_child_span_created.call_count, 1) self.assertEqual(mock_observer.on_child_span_created.call_args, mock.call(child_span))
def test_nested(): batch = FakeBatch() span = ServerSpan( trace_id=1234, parent_id=2345, span_id=3456, sampled=None, flags=None, name="fancy.span", context=RequestContext({}), ) observer = TaggedMetricsLocalSpanObserver(batch, span, {"client", "endpoint"}) span.register(observer) with span.make_child("foo", local=True, component_name="foo") as child_span: assert len(child_span.observers) == 1
def test_make_local_span(self, mock_getrandbits): mock_getrandbits.return_value = 0xCAFE mock_observer = mock.Mock(spec=ServerSpanObserver) mock_context = mock.Mock() mock_cloned_context = mock.Mock() mock_context.clone.return_value = mock_cloned_context server_span = ServerSpan("trace", "parent", "id", None, 0, "name", mock_context) server_span.register(mock_observer) local_span = server_span.make_child("test_op", local=True, component_name="test_component") self.assertEqual(local_span.name, "test_op") self.assertEqual(local_span.id, 0xCAFE) self.assertEqual(local_span.trace_id, "trace") self.assertEqual(local_span.parent_id, "id") self.assertEqual(mock_observer.on_child_span_created.call_count, 1) self.assertEqual(mock_observer.on_child_span_created.call_args, mock.call(local_span))
def make_test_server_span(context=None): if not context: context = mock.Mock() return ServerSpan(1, 2, 3, None, 0, "name", context)
def setUp(self): super().setUp() self.recorder = NullRecorder() self.mock_context = mock.Mock() self.span = ServerSpan("test-id", "test-parent-id", "test-span-id", None, 0, "test", self.mock_context)