def on_server_span_created(self, context: RequestContext, server_span: ServerSpan) -> None: timeout = self.config.by_endpoint.get(server_span.name, self.config.default) if timeout is not config.InfiniteTimespan: observer = TimeoutServerSpanObserver( server_span, timeout.total_seconds(), self.config.debug ) server_span.register(observer)
def on_server_span_created(self, context: RequestContext, server_span: ServerSpan) -> None: timeout = self.config.by_endpoint.get(server_span.name, self.config.default) observer = TimeoutServerSpanObserver(server_span, timeout, self.config.debug) server_span.register(observer)
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 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_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 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 on_server_span_created(self, context: RequestContext, server_span: ServerSpan) -> None: timeout = self.config.by_endpoint.get(server_span.name, self.config.default) min_timeout = None if timeout is not config.InfiniteTimespan: min_timeout = timeout.total_seconds() try: deadline_budget = context.deadline_budget if deadline_budget: if not min_timeout or deadline_budget < min_timeout: min_timeout = deadline_budget except AttributeError: # no deadline budget in request header pass if min_timeout: observer = TimeoutServerSpanObserver(server_span, min_timeout, self.config.debug) server_span.register(observer)
def make_test_server_span(context=None): if not context: context = mock.Mock() return ServerSpan(1, 2, 3, None, 0, "name", context)
def on_server_span_created(self, context: RequestContext, server_span: ServerSpan) -> None: observer = _ActiveRequestsServerSpanObserver(self, server_span.trace_id) server_span.register(observer)
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)