Beispiel #1
0
 def test_finished(self):
     # a Context is finished if all spans inside are finished
     ctx = Context()
     span = Span(tracer=None, name='fake_span')
     ctx.add_span(span)
     ctx.close_span(span)
     assert ctx.is_finished()
    def test_partial_flush_too_few(self):
        """
        When calling `Context.get`
        When partial flushing is enabled
        When we do not have enough finished spans to flush
        We return no spans
        """
        tracer = get_dummy_tracer()
        ctx = Context()

        # Create a root span with 5 children, all of the children are finished, the root is not
        root = Span(tracer=tracer, name='root')
        ctx.add_span(root)
        for i in range(5):
            child = Span(tracer=tracer, name='child_{}'.format(i), trace_id=root.trace_id, parent_id=root.span_id)
            child._parent = root
            child.finished = True
            ctx.add_span(child)
            ctx.close_span(child)

        # Test with having 1 too few spans for partial flush
        with self.override_partial_flush(ctx, enabled=True, min_spans=6):
            trace, sampled = ctx.get()

        self.assertIsNone(trace)
        self.assertIsNone(sampled)

        self.assertEqual(len(ctx._trace), 6)
        self.assertEqual(
            set(['root', 'child_0', 'child_1', 'child_2', 'child_3', 'child_4']),
            set([span.name for span in ctx._trace])
        )
Beispiel #3
0
 def test_finished(self):
     # a Context is finished if all spans inside are finished
     ctx = Context()
     span = Span(tracer=None, name='fake_span')
     ctx.add_span(span)
     ctx.close_span(span)
     ok_(ctx.is_finished())
Beispiel #4
0
 def test_close_span(self):
     # it should keep track of closed spans, moving
     # the current active to its parent
     ctx = Context()
     span = Span(tracer=None, name="fake_span")
     ctx.add_span(span)
     ctx.close_span(span)
     assert ctx.get_current_span() is None
Beispiel #5
0
 def test_close_span(self):
     # it should keep track of closed spans, moving
     # the current active to it's parent
     ctx = Context()
     span = Span(tracer=None, name='fake_span')
     ctx.add_span(span)
     ctx.close_span(span)
     eq_(1, ctx._finished_spans)
     ok_(ctx.get_current_span() is None)
Beispiel #6
0
 def test_close_span(self):
     # it should keep track of closed spans, moving
     # the current active to it's parent
     ctx = Context()
     span = Span(tracer=None, name='fake_span')
     ctx.add_span(span)
     ctx.close_span(span)
     eq_(1, ctx._finished_spans)
     ok_(ctx.get_current_span() is None)
    def test_get_report_hostname_default(self, get_hostname):
        get_hostname.return_value = 'test-hostname'

        # Create a context and add a span and finish it
        ctx = Context()
        span = Span(tracer=None, name='fake_span')
        ctx.add_span(span)
        ctx.close_span(span)

        # Assert that we have not added the tag to the span yet
        assert span.get_tag(HOSTNAME_KEY) is None

        # Assert that retrieving the trace does not set the tag
        trace, _ = ctx.get()
        assert trace[0].get_tag(HOSTNAME_KEY) is None
        assert span.get_tag(HOSTNAME_KEY) is None
Beispiel #8
0
 def test_get_trace(self):
     # it should return the internal trace structure
     # if the context is finished
     ctx = Context()
     span = Span(tracer=None, name='fake_span')
     ctx.add_span(span)
     ctx.close_span(span)
     trace, sampled = ctx.get()
     eq_(1, len(trace))
     eq_(span, trace[0])
     ok_(sampled is True)
     # the context should be empty
     eq_(0, len(ctx._trace))
     eq_(0, ctx._finished_spans)
     ok_(ctx._current_span is None)
     ok_(ctx._sampled is True)
Beispiel #9
0
 def test_get_trace(self):
     # it should return the internal trace structure
     # if the context is finished
     ctx = Context()
     span = Span(tracer=None, name='fake_span')
     ctx.add_span(span)
     ctx.close_span(span)
     trace, sampled = ctx.get()
     eq_(1, len(trace))
     eq_(span, trace[0])
     ok_(sampled is True)
     # the context should be empty
     eq_(0, len(ctx._trace))
     eq_(0, ctx._finished_spans)
     ok_(ctx._current_span is None)
     ok_(ctx._sampled is False)
Beispiel #10
0
 def test_get_trace(self):
     # it should return the internal trace structure
     # if the context is finished
     ctx = Context()
     span = Span(tracer=None, name='fake_span')
     ctx.add_span(span)
     ctx.close_span(span)
     trace, sampled = ctx.get()
     assert 1 == len(trace)
     assert span == trace[0]
     assert sampled is True
     # the context should be empty
     assert 0 == len(ctx._trace)
     assert 0 == ctx._finished_spans
     assert ctx._current_span is None
     assert ctx._sampled is True
    def test_get_report_hostname_enabled(self, get_hostname):
        get_hostname.return_value = 'test-hostname'

        with self.override_global_config(dict(report_hostname=True)):
            # Create a context and add a span and finish it
            ctx = Context()
            span = Span(tracer=None, name='fake_span')
            ctx.add_span(span)
            ctx.close_span(span)

            # Assert that we have not added the tag to the span yet
            assert span.get_tag(HOSTNAME_KEY) is None

            # Assert that retrieving the trace sets the tag
            trace, _ = ctx.get()
            assert trace[0].get_tag(HOSTNAME_KEY) == 'test-hostname'
            assert span.get_tag(HOSTNAME_KEY) == 'test-hostname'
Beispiel #12
0
    def test_partial_flush_remaining(self):
        """
        When calling `Context.get`
            When partial flushing is enabled
            When we have some unfinished spans
                We keep the unfinished spans around
        """
        tracer = get_dummy_tracer()
        ctx = Context()

        # Create a root span with 5 children, all of the children are finished, the root is not
        root = Span(tracer=tracer, name='root')
        ctx.add_span(root)
        for i in range(10):
            child = Span(tracer=tracer,
                         name='child_{}'.format(i),
                         trace_id=root.trace_id,
                         parent_id=root.span_id)
            child._parent = root
            ctx.add_span(child)

            # CLose the first 5 only
            if i < 5:
                child._finished = True
                ctx.close_span(child)

        with self.override_partial_flush(ctx, enabled=True, min_spans=5):
            trace, sampled = ctx.get()

        # Assert partially flushed spans
        self.assertTrue(len(trace), 5)
        self.assertIsNotNone(sampled)
        self.assertEqual(
            set(['child_0', 'child_1', 'child_2', 'child_3', 'child_4']),
            set([span.name for span in trace]))

        # Assert remaining unclosed spans
        self.assertEqual(len(ctx._trace), 6)
        self.assertEqual(ctx._finished_spans, 0)
        self.assertEqual(
            set([
                'root', 'child_5', 'child_6', 'child_7', 'child_8', 'child_9'
            ]),
            set([span.name for span in ctx._trace]),
        )
Beispiel #13
0
    def test_context_sampled(self):
        # a context is sampled if the spans are sampled
        ctx = Context()
        span = Span(tracer=None, name="fake_span")
        ctx.add_span(span)
        span.finished = True
        trace, sampled = ctx.close_span(span)

        assert sampled is True
        assert ctx.sampling_priority is None
Beispiel #14
0
    def test_partial_flush_too_many(self):
        """
        When calling `Context.get`
            When partial flushing is enabled
            When we have more than the minimum number of spans needed to flush
                We return the finished spans
        """
        tracer = get_dummy_tracer()
        ctx = Context()

        # Create a root span with 5 children, all of the children are finished, the root is not
        root = Span(tracer=tracer, name='root')
        ctx.add_span(root)
        for i in range(5):
            child = Span(tracer=tracer,
                         name='child_{}'.format(i),
                         trace_id=root.trace_id,
                         parent_id=root.span_id)
            child._parent = root
            child._finished = True
            ctx.add_span(child)
            ctx.close_span(child)

        with self.override_partial_flush(ctx, enabled=True, min_spans=1):
            trace, sampled = ctx.get()

        self.assertIsNotNone(trace)
        self.assertIsNotNone(sampled)

        self.assertEqual(len(trace), 5)
        self.assertEqual(
            set(['child_0', 'child_1', 'child_2', 'child_3', 'child_4']),
            set([span.name for span in trace]))

        # Ensure we clear/reset internal stats as expected
        self.assertEqual(ctx._finished_spans, 0)
        self.assertEqual(ctx._trace, [root])
        with self.override_partial_flush(ctx, enabled=True, min_spans=5):
            trace, sampled = ctx.get()
            self.assertIsNone(trace)
            self.assertIsNone(sampled)
Beispiel #15
0
 def test_get_trace(self):
     # it should return the internal trace structure
     # if the context is finished
     ctx = Context()
     span = Span(tracer=None, name="fake_span")
     ctx.add_span(span)
     span.finished = True
     trace, sampled = ctx.close_span(span)
     assert [span] == trace
     assert sampled is True
     # the context should be empty
     assert 0 == len(ctx._trace)
     assert ctx._current_span is None
Beispiel #16
0
 def test_context_priority(self):
     # a context is sampled if the spans are sampled
     ctx = Context()
     for priority in [USER_REJECT, AUTO_REJECT, AUTO_KEEP, USER_KEEP, None, 999]:
         ctx.sampling_priority = priority
         span = Span(tracer=None, name=("fake_span_%s" % repr(priority)))
         ctx.add_span(span)
         span.finished = True
         # It's "normal" to have sampled be true even when priority sampling is
         # set to 0 or -1. It would stay false even even with priority set to 2.
         # The only criteria to send (or not) the spans to the agent should be
         # this "sampled" attribute, as it's tightly related to the trace weight.
         assert priority == ctx.sampling_priority
         trace, sampled = ctx.close_span(span)
         assert sampled is True, "priority has no impact on sampled status"
Beispiel #17
0
    def test_get_report_hostname_disabled(self, get_hostname):
        get_hostname.return_value = "test-hostname"

        with self.override_global_config(dict(report_hostname=False)):
            # Create a context and add a span and finish it
            ctx = Context()
            span = Span(tracer=None, name="fake_span")
            ctx.add_span(span)
            span.finished = True

            # Assert that we have not added the tag to the span yet
            assert span.get_tag(HOSTNAME_KEY) is None

            # Assert that retrieving the trace does not set the tag
            trace, _ = ctx.close_span(span)
            assert trace[0].get_tag(HOSTNAME_KEY) is None
            assert span.get_tag(HOSTNAME_KEY) is None