コード例 #1
0
    def translate_to_jaeger(self, span_datas):
        """Translate the spans to Jaeger format.

        :type span_datas: list of :class:
            `~opencensus.trace.span_data.SpanData`
        :param span_datas:
            SpanData tuples to emit
        """

        top_span = span_datas[0]

        trace_id = top_span.context.trace_id if top_span.context is not None \
            else None

        jaeger_spans = []

        for span in span_datas:
            start_datetime = _strptime(span.start_time)
            start_microsec = calendar.timegm(start_datetime.timetuple()) \
                * 1e6 \
                + start_datetime.microsecond

            end_datetime = _strptime(span.end_time)
            end_microsec = calendar.timegm(end_datetime.timetuple()) \
                * 1e6 \
                + end_datetime.microsecond

            duration_microsec = end_microsec - start_microsec

            tags = _extract_tags(span.attributes)

            status = span.status
            if status is not None:
                tags.append(
                    jaeger.Tag(key='status.code',
                               vType=jaeger.TagType.LONG,
                               vLong=status.code))

                tags.append(
                    jaeger.Tag(key='status.message',
                               vType=jaeger.TagType.STRING,
                               vStr=status.message))

            refs = _extract_refs_from_span(span)
            logs = _extract_logs_from_span(span)

            context = span.context
            flags = None
            if context is not None:
                flags = int(context.trace_options.trace_options_byte)

            span_id = span.span_id
            parent_span_id = span.parent_span_id

            jaeger_span = jaeger.Span(
                traceIdHigh=_convert_hex_str_to_int(trace_id[0:16]),
                traceIdLow=_convert_hex_str_to_int(trace_id[16:32]),
                spanId=_convert_hex_str_to_int(span_id),
                operationName=span.name,
                startTime=int(round(start_microsec)),
                duration=int(round(duration_microsec)),
                tags=tags,
                logs=logs,
                references=refs,
                flags=flags,
                parentSpanId=_convert_hex_str_to_int(parent_span_id or '0'))

            jaeger_spans.append(jaeger_span)

        return jaeger_spans
コード例 #2
0
    def test_translate_to_jaeger(self):
        self.maxDiff = None
        trace_id_high = '6e0c63257de34c92'
        trace_id_low = 'bf9efcd03927272e'
        trace_id = trace_id_high + trace_id_low
        span_id = '6e0c63257de34c92'
        parent_span_id = '1111111111111111'

        span_attributes = {
            'key_bool': False,
            'key_string': 'hello_world',
            'key_int': 3
        }

        annotation_attributes = {
            'annotation_bool': True,
            'annotation_string': 'annotation_test',
            'key_float': .3
        }

        link_attributes = {'key_bool': True}

        import datetime
        s = '2017-08-15T18:02:26.071158'
        time = datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
        time_events = [
            time_event.TimeEvent(
                timestamp=time,
                annotation=time_event.Annotation(
                    description='First Annotation',
                    attributes=attributes.Attributes(annotation_attributes))),
            time_event.TimeEvent(timestamp=time,
                                 message_event=time_event.MessageEvent(
                                     id='message-event-id',
                                     uncompressed_size_bytes=0,
                                 )),
        ]

        time_events2 = [
            time_event.TimeEvent(timestamp=time,
                                 annotation=time_event.Annotation(
                                     description='First Annotation',
                                     attributes=None)),
            time_event.TimeEvent(timestamp=time,
                                 message_event=time_event.MessageEvent(
                                     id='message-event-id',
                                     uncompressed_size_bytes=0,
                                 )),
        ]

        links = [
            link.Link(trace_id=trace_id,
                      span_id=span_id,
                      type=link.Type.CHILD_LINKED_SPAN,
                      attributes=link_attributes),
            link.Link(trace_id=trace_id,
                      span_id=span_id,
                      type=link.Type.PARENT_LINKED_SPAN,
                      attributes=link_attributes),
            link.Link(trace_id=trace_id,
                      span_id=span_id,
                      type=link.Type.TYPE_UNSPECIFIED,
                      attributes=link_attributes)
        ]

        span_status = status.Status(code=200, message='success')

        start_time = '2017-08-15T18:02:26.071158Z'
        end_time = '2017-08-15T18:02:36.071158Z'

        span_datas = [
            span_data.SpanData(
                name='test1',
                context=span_context.SpanContext(trace_id=trace_id),
                span_id=span_id,
                parent_span_id=parent_span_id,
                attributes=span_attributes,
                start_time=start_time,
                end_time=end_time,
                child_span_count=0,
                stack_trace=None,
                time_events=time_events,
                links=links,
                status=span_status,
                same_process_as_parent_span=None,
                span_kind=0,
            ),
            span_data.SpanData(
                name='test2',
                context=None,
                span_id=span_id,
                parent_span_id=None,
                attributes=None,
                start_time=start_time,
                end_time=end_time,
                child_span_count=None,
                stack_trace=None,
                time_events=time_events2,
                links=None,
                status=None,
                same_process_as_parent_span=None,
                span_kind=None,
            ),
            span_data.SpanData(
                name='test3',
                context=None,
                span_id=span_id,
                parent_span_id=None,
                attributes=None,
                start_time=start_time,
                end_time=end_time,
                child_span_count=None,
                stack_trace=None,
                time_events=None,
                links=None,
                status=None,
                same_process_as_parent_span=None,
                span_kind=None,
            )
        ]

        exporter = jaeger_exporter.JaegerExporter()

        spans = exporter.translate_to_jaeger(span_datas)
        expected_spans = [
            jaeger.Span(
                traceIdHigh=7929822056569588882,
                traceIdLow=-4638992594902767826,
                spanId=7929822056569588882,
                parentSpanId=1229782938247303441,
                operationName='test1',
                startTime=1502820146071158,
                duration=10000000,
                flags=1,
                tags=[
                    jaeger.Tag(key='key_bool',
                               vType=jaeger.TagType.BOOL,
                               vBool=False),
                    jaeger.Tag(key='key_string',
                               vType=jaeger.TagType.STRING,
                               vStr='hello_world'),
                    jaeger.Tag(key='key_int',
                               vType=jaeger.TagType.LONG,
                               vLong=3),
                    jaeger.Tag(key='status.code',
                               vType=jaeger.TagType.LONG,
                               vLong=200),
                    jaeger.Tag(key='status.message',
                               vType=jaeger.TagType.STRING,
                               vStr='success')
                ],
                references=[
                    jaeger.SpanRef(refType=jaeger.SpanRefType.CHILD_OF,
                                   traceIdHigh=7929822056569588882,
                                   traceIdLow=-4638992594902767826,
                                   spanId=7929822056569588882),
                    jaeger.SpanRef(refType=jaeger.SpanRefType.FOLLOWS_FROM,
                                   traceIdHigh=7929822056569588882,
                                   traceIdLow=-4638992594902767826,
                                   spanId=7929822056569588882),
                    jaeger.SpanRef(refType=None,
                                   traceIdHigh=7929822056569588882,
                                   traceIdLow=-4638992594902767826,
                                   spanId=7929822056569588882)
                ],
                logs=[
                    jaeger.Log(timestamp=1502820146071158,
                               fields=[
                                   jaeger.Tag(key='annotation_bool',
                                              vType=jaeger.TagType.BOOL,
                                              vBool=True),
                                   jaeger.Tag(key='annotation_string',
                                              vType=jaeger.TagType.STRING,
                                              vStr='annotation_test'),
                                   jaeger.Tag(key='message',
                                              vType=jaeger.TagType.STRING,
                                              vStr='First Annotation')
                               ])
                ]),
            jaeger.Span(operationName="test2",
                        traceIdHigh=7929822056569588882,
                        traceIdLow=-4638992594902767826,
                        spanId=7929822056569588882,
                        parentSpanId=0,
                        startTime=1502820146071158,
                        duration=10000000,
                        logs=[
                            jaeger.Log(timestamp=1502820146071158,
                                       fields=[
                                           jaeger.Tag(
                                               key='message',
                                               vType=jaeger.TagType.STRING,
                                               vStr='First Annotation')
                                       ])
                        ]),
            jaeger.Span(operationName="test3",
                        traceIdHigh=7929822056569588882,
                        traceIdLow=-4638992594902767826,
                        spanId=7929822056569588882,
                        parentSpanId=0,
                        startTime=1502820146071158,
                        duration=10000000,
                        logs=[])
        ]

        spans_json = [span.format_span_json() for span in spans]
        expected_spans_json = [
            span.format_span_json() for span in expected_spans
        ]
        span = spans_json[0]
        expected_span = expected_spans_json[0]

        try:
            listsEqual = self.assertCountEqual
        except AttributeError:
            listsEqual = self.assertItemsEqual

        log = span.get('logs')[0]
        expected_log = expected_span.get('logs')[0]
        self.assertEqual(log.get('timestamp'), expected_log.get('timestamp'))
        listsEqual(log.get('fields'), expected_log.get('fields'))
        listsEqual(span.get('tags'), expected_span.get('tags'))
        listsEqual(span.get('references'), expected_span.get('references'))
        self.assertEqual(span.get('traceIdHigh'),
                         expected_span.get('traceIdHigh'))
        self.assertEqual(span.get('traceIdLow'),
                         expected_span.get('traceIdLow'))
        self.assertEqual(span.get('spanId'), expected_span.get('spanId'))
        self.assertEqual(span.get('parentSpanId'),
                         expected_span.get('parentSpanId'))
        self.assertEqual(span.get('operationName'),
                         expected_span.get('operationName'))
        self.assertEqual(span.get('startTime'), expected_span.get('startTime'))
        self.assertEqual(span.get('duration'), expected_span.get('duration'))
        self.assertEqual(span.get('flags'), expected_span.get('flags'))
        self.assertEqual(spans_json[1], expected_spans_json[1])

        self.assertEqual(spans_json[2], expected_spans_json[2])