示例#1
0
    def test_add_message_event(self):
        from opencensus.trace.time_event import MessageEvent

        span_name = 'test_span_name'
        span = self._make_one(span_name)
        message_event = mock.Mock()

        message_event = MessageEvent(datetime.datetime.utcnow(), mock.Mock())
        span.add_message_event(message_event)

        self.assertEqual(len(span.message_events), 1)
示例#2
0
    def test_do_not_crash(self):
        span_id = 'test_span_id'
        span_name = 'test_span_name'

        patch = mock.patch('opencensus.trace.blank_span.generate_span_id',
                           return_value=span_id)

        with patch:
            span = self._make_one(span_name)

        self.assertEqual(span.name, span_name)
        self.assertEqual(span.span_id, span_id)
        self.assertIsNotNone(span.parent_span)
        self.assertIsNotNone(span.parent_span.span())
        self.assertEqual(span.attributes, {})
        self.assertIsNone(span.start_time)
        self.assertIsNone(span.end_time)
        self.assertEqual(span.children, [])
        self.assertIsNone(span.context_tracer)

        span.add_attribute('attribute_key', 'attribute_value')
        span.add_annotation('This is a test', name='blank-span')

        link = Link(span_id='1234', trace_id='4567')
        span.add_link(link)

        status = Status(0, 'Ok', {'details': 'ok'})
        span.set_status(status)

        message_event = mock.Mock()
        message_event = MessageEvent(datetime.datetime.utcnow(), mock.Mock())
        span.add_message_event(message_event)

        span_iter_list = list(iter(span))
        self.assertEqual(span_iter_list, [span])

        expected_span_json = {
            'spanId': 'test_span_id',
            'startTime': None,
            'endTime': None,
            'displayName': {
                'truncated_byte_count': 0,
                'value': 'test_span_name'
            },
            'childSpanCount': 0,
        }
        span_json = format_span_json(span)
        self.assertEqual(span_json, expected_span_json)

        span.start()
        span.finish()
示例#3
0
    def test_format_span_json_with_parent_span(self, annotation_mock,
                                               message_event_mock, status_mock,
                                               stack_trace_mock):

        from opencensus.trace.link import Link
        from opencensus.trace.span import format_span_json

        name = 'test span'
        span_id = 1234
        trace_id = '3456'
        attributes = {
            'http.status_code': '200',
            'component': 'HTTP load balancer',
            'none_key': None
        }

        links = {
            'link': [
                {
                    'trace_id': trace_id,
                    'span_id': span_id,
                    'type': 0
                },
            ],
        }

        start_time = '2017-06-25'
        end_time = '2017-06-26'
        parent_span = mock.Mock()
        parent_span_id = 5678
        parent_span.span_id = parent_span_id

        span = mock.Mock()
        span.parent_span = parent_span
        span.name = name
        span.attributes = attributes
        span.span_id = span_id
        span.start_time = start_time
        span.end_time = end_time
        span._child_spans = []
        mock_time = mock.Mock()
        annotation = Annotation(datetime.datetime.utcnow(), mock.Mock())
        annotation.timestamp = mock_time
        span.annotations = [annotation]
        message_event = MessageEvent(datetime.datetime.utcnow(), mock.Mock())
        message_event.timestamp = mock_time
        span.message_events = [message_event]
        span.stack_trace = StackTrace()
        span.status = Status(code='200', message='test')
        span.links = [Link(trace_id, span_id)]
        span.same_process_as_parent_span = True

        mock_stack_trace = 'stack trace'
        mock_status = 'status'
        mock_annotation = 'annotation'
        mock_message_event = 'message_event'

        stack_trace_mock.return_value = mock_stack_trace
        status_mock.return_value = mock_status
        annotation_mock.return_value = mock_annotation
        message_event_mock.return_value = mock_message_event

        expected_span_json = {
            'spanId': span_id,
            'parentSpanId': parent_span_id,
            'startTime': start_time,
            'endTime': end_time,
            'attributes': {
                'attributeMap': {
                    'component': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': 'HTTP load balancer'
                        }
                    },
                    'http.status_code': {
                        'string_value': {
                            'truncated_byte_count': 0,
                            'value': '200'
                        }
                    }
                }
            },
            'links': links,
            'stackTrace': mock_stack_trace,
            'status': mock_status,
            'timeEvents': {
                'timeEvent': [{
                    'annotation': mock_annotation,
                    'time': mock_time,
                }, {
                    'message_event': mock_message_event,
                    'time': mock_time,
                }]
            },
            'displayName': {
                'truncated_byte_count': 0,
                'value': 'test span'
            },
            'childSpanCount': 0,
            'sameProcessAsParentSpan': True
        }

        span_json = format_span_json(span)
        self.assertEqual(span_json, expected_span_json)