Example #1
0
 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)
Example #2
0
 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))
Example #3
0
 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)
Example #4
0
 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
Example #5
0
 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))
Example #6
0
 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))
Example #7
0
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
Example #8
0
    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
Example #9
0
 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)
Example #10
0
    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)
Example #11
0
 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))
Example #12
0
    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)
Example #13
0
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"] == {}
Example #14
0
    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
Example #15
0
 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
Example #16
0
 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
Example #17
0
 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)
Example #18
0
 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
Example #19
0
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()
Example #20
0
 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])