예제 #1
0
    def test_format_span_json_no_parent_span(self):
        from opencensus.trace.span import format_span_json

        name = 'test span'
        span_id = 1234
        start_time = '2017-06-25'
        end_time = '2017-06-26'

        span = mock.Mock()
        span.name = name
        span.span_id = span_id
        span.start_time = start_time
        span.end_time = end_time
        span.parent_span = None
        span.attributes = None
        span.stack_trace = None
        span.status = None
        span._child_spans = []
        span.time_events = []
        span.links = []
        span.same_process_as_parent_span = None

        expected_span_json = {
            'spanId': span_id,
            'startTime': start_time,
            'endTime': end_time,
            'displayName': {
                'truncated_byte_count': 0,
                'value': 'test span'
            },
            'childSpanCount': 0,
        }

        span_json = format_span_json(span)
        self.assertEqual(span_json, expected_span_json)
예제 #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)

        time_event = mock.Mock()

        with self.assertRaises(TypeError):
            span.add_time_event(time_event)

        time_event = TimeEvent(datetime.datetime.now())
        span.add_time_event(time_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 get_trace_json(self, span):
        """Get the JSON format trace."""
        span_tree = list(iter(span))
        span_tree_list = [
            trace_span.format_span_json(span) for span in span_tree
        ]

        trace = {
            'traceId': self.trace_id,
            'spans': span_tree_list,
        }

        return trace
예제 #4
0
    def test_format_span_json_with_parent_span(self, time_event_mock,
                                               status_mock, stack_trace_mock):
        import datetime

        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 = []
        span.time_events = [TimeEvent(datetime.datetime.now())]
        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_time_event = 'time event'

        stack_trace_mock.return_value = mock_stack_trace
        status_mock.return_value = mock_status
        time_event_mock.return_value = mock_time_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': [mock_time_event]
            },
            'displayName': {
                'truncated_byte_count': 0,
                'value': 'test span'
            },
            'childSpanCount': 0,
            'sameProcessAsParentSpan': True
        }

        span_json = format_span_json(span)

        print(span_json)

        print(expected_span_json)
        self.assertEqual(span_json, expected_span_json)