def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: batch = self.client.batch() context.metrics = batch if self.sample_rate == 1.0 or random() < self.sample_rate: observer = MetricsServerSpanObserver(batch, server_span, self.sample_rate) server_span.register(observer)
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 on_child_span_created(self, span: Span) -> None: observer: SpanObserver if isinstance(span, LocalSpan): observer = MetricsLocalSpanObserver(self.batch, span, self.sample_rate) else: observer = MetricsClientSpanObserver(self.batch, span, self.sample_rate) span.register(observer)
def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: if self.should_sample(server_span): server_span.sampled = True observer = TraceServerSpanObserver( self.service_name, self.hostname, server_span, self.recorder ) server_span.register(observer) else: server_span.sampled = False
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_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 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 get_message(self, server_span: Span) -> Message: """Return a single message. :param server_span: The span. """ child_span = server_span.make_child("kombu.get_message") child_span.set_tag("kind", "consumer") with child_span: messages = self.base_consumer.get_batch(max_items=1, timeout=None) message = messages[0] routing_key = message.delivery_info.get("routing_key", "") child_span.set_tag("routing_key", routing_key) consumer_tag = message.delivery_info.get("consumer_tag", "") child_span.set_tag("consumer_tag", consumer_tag) delivery_tag = message.delivery_info.get("delivery_tag", "") child_span.set_tag("delivery_tag", delivery_tag) exchange = message.delivery_info.get("exchange", "") child_span.set_tag("exchange", exchange) return message
def test_on_child_span_created_for_debug_span(self): child_span = Span("child-id", "test-parent-id", "test-span-id", None, 1, "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 setUp(self): super().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.debug_span = Span("test-id", "test-parent-id", "test-span-id", None, 1, "test", self.mock_context) self.test_span_observer = TraceSpanObserver("test-service", "test-hostname", self.span, self.recorder) self.test_debug_span_observer = TraceSpanObserver( "test-service", "test-hostname", self.debug_span, self.recorder)
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 on_child_span_created(self, span: Span) -> None: """Perform tracing-related actions for child spans creation. Register new TraceSpanObserver for the child span being created so span start and finish get properly recorded. """ trace_observer: TraceSpanObserver if isinstance(span, LocalSpan): trace_observer = TraceLocalSpanObserver( self.service_name, typing.cast(str, span.component_name), self.hostname, span, self.recorder, ) else: trace_observer = TraceSpanObserver(self.service_name, self.hostname, span, self.recorder) span.register(trace_observer)
def test_observer(observer_cls, name): batch = FakeBatch() span = Span( trace_id=1234, parent_id=2345, span_id=3456, sampled=None, flags=None, name="fancy.span", context=RequestContext({}), ) allow_list = {"client", "endpoint", "tag2"} sample_rate = 0.3 observer = observer_cls(batch, span, allow_list, sample_rate=0.3) observer.on_start() observer.on_incr_tag("my.tag", 2) observer.on_set_tag("tag1", "foo") observer.on_set_tag("tag2", "bar") observer.on_finish(None) assert batch.timers timer = batch.timers.pop() assert batch.timers == [] assert timer["sample_rate"] == sample_rate assert timer["name"] == f"baseplate.{name}.latency" if name in ("server", "local"): assert timer["tags"].pop("endpoint") == "fancy.span" else: assert timer["tags"].pop("client") == "fancy" assert timer["tags"].pop("endpoint") == "span" assert timer["tags"].pop("tag2") == "bar" assert timer["tags"] == {} for _ in range(2): counter = batch.counters.pop() assert counter["sample_rate"] == sample_rate if counter["name"] == f"baseplate.{name}.rate": assert counter["delta"] == 1 assert counter["tags"].pop("success") is True elif counter["name"] == "my.tag": assert counter["delta"] == 2 else: raise Exception(f"unexpected counter: {counter}") if name in ("server", "local"): assert counter["tags"].pop("endpoint") == "fancy.span" else: assert counter["tags"].pop("client") == "fancy" assert counter["tags"].pop("endpoint") == "span" assert counter["tags"].pop("tag2") == "bar" assert counter["tags"] == {}
def get_batch(self, server_span: Span, max_items: int, timeout: Optional[float]) -> Sequence[Message]: """Return a batch of messages. :param server_span: The span. :param max_items: The maximum batch size. :param timeout: The maximum time to wait in seconds, or ``None`` for no timeout. """ child_span = server_span.make_child("kombu.get_batch") child_span.set_tag("kind", "consumer") with child_span: messages = self.base_consumer.get_batch(max_items, timeout) child_span.set_tag("message_count", len(messages)) return messages
def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: observer = SentryServerSpanObserver(self.raven, server_span) server_span.register(observer) context.sentry = self.raven
def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: sentry_hub = sentry_sdk.Hub.current observer = _SentryServerSpanObserver(sentry_hub, server_span) server_span.register(observer) context.sentry = sentry_hub
def on_server_span_created(self, context: RequestContext, server_span: Span) -> None: batch = self.client.batch() context.metrics = batch observer = MetricsServerSpanObserver(batch, server_span) server_span.register(observer)
def make_object_for_context(self, name: str, span: Span) -> Session: engine = super().make_object_for_context(name, span) session = Session(bind=engine) span.register(SQLAlchemySessionSpanObserver(session)) return session
def _on_execute_failed(exc: BaseException, span: Span, event: Event) -> None: try: exc_info = (type(exc), exc, None) span.finish(exc_info=exc_info) finally: event.set()
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])