コード例 #1
0
    def test_set_annotation_with_attributes(self):
        pb_span = trace_pb2.Span()
        pb_event = pb_span.time_events.time_event.add()

        annotation = time_event_module.Annotation(
            description="hi there",
            attributes=attributes_module.Attributes(
                attributes={
                    'test_str_key': 'test_str_value',
                    'test_int_key': 1,
                    'test_bool_key': False,
                    'test_double_key': 567.89
                }))

        utils.set_proto_annotation(pb_event.annotation, annotation)

        self.assertEqual(pb_event.annotation.description.value, "hi there")
        self.assertEqual(len(pb_event.annotation.attributes.attribute_map), 4)
        self.assertEqual(
            pb_event.annotation.attributes.attribute_map['test_str_key'],
            trace_pb2.AttributeValue(string_value=trace_pb2.TruncatableString(
                value='test_str_value')))
        self.assertEqual(
            pb_event.annotation.attributes.attribute_map['test_int_key'],
            trace_pb2.AttributeValue(int_value=1))
        self.assertEqual(
            pb_event.annotation.attributes.attribute_map['test_bool_key'],
            trace_pb2.AttributeValue(bool_value=False))
        self.assertEqual(
            pb_event.annotation.attributes.attribute_map['test_double_key'],
            trace_pb2.AttributeValue(double_value=567.89))
コード例 #2
0
def translate_to_collector(spans: Sequence[ReadableSpan]):
    collector_spans = []
    for span in spans:
        status = None
        if span.status is not None:
            status = trace_pb2.Status(
                code=span.status.status_code.value,
                message=span.status.description,
            )

        collector_span = trace_pb2.Span(
            name=trace_pb2.TruncatableString(value=span.name),
            kind=utils.get_collector_span_kind(span.kind),
            trace_id=span.context.trace_id.to_bytes(16, "big"),
            span_id=span.context.span_id.to_bytes(8, "big"),
            start_time=utils.proto_timestamp_from_time_ns(span.start_time),
            end_time=utils.proto_timestamp_from_time_ns(span.end_time),
            status=status,
        )

        parent_id = 0
        if span.parent is not None:
            parent_id = span.parent.span_id

        collector_span.parent_span_id = parent_id.to_bytes(8, "big")

        if span.context.trace_state is not None:
            for (key, value) in span.context.trace_state.items():
                collector_span.tracestate.entries.add(key=key, value=value)

        if span.attributes:
            for (key, value) in span.attributes.items():
                utils.add_proto_attribute_value(collector_span.attributes, key,
                                                value)

        if span.events:
            for event in span.events:

                collector_annotation = trace_pb2.Span.TimeEvent.Annotation(
                    description=trace_pb2.TruncatableString(value=event.name))

                if event.attributes:
                    for (key, value) in event.attributes.items():
                        utils.add_proto_attribute_value(
                            collector_annotation.attributes, key, value)

                collector_span.time_events.time_event.add(
                    time=utils.proto_timestamp_from_time_ns(event.timestamp),
                    annotation=collector_annotation,
                )

        if span.links:
            for link in span.links:
                collector_span_link = collector_span.links.link.add()
                collector_span_link.trace_id = link.context.trace_id.to_bytes(
                    16, "big")
                collector_span_link.span_id = link.context.span_id.to_bytes(
                    8, "big")

                collector_span_link.type = (
                    trace_pb2.Span.Link.Type.TYPE_UNSPECIFIED)
                if span.parent is not None:
                    if (link.context.span_id == span.parent.span_id
                            and link.context.trace_id == span.parent.trace_id):
                        collector_span_link.type = (
                            trace_pb2.Span.Link.Type.PARENT_LINKED_SPAN)

                if link.attributes:
                    for (key, value) in link.attributes.items():
                        utils.add_proto_attribute_value(
                            collector_span_link.attributes, key, value)

        collector_spans.append(collector_span)
    return collector_spans
コード例 #3
0
    def test_translate_to_collector(self):
        trace_id = 0x6E0C63257DE34C926F9EFCD03927272E
        span_id = 0x34BF92DEEFC58C92
        parent_id = 0x1111111111111111
        base_time = 683647322 * 10**9  # in ns
        start_times = (
            base_time,
            base_time + 150 * 10**6,
            base_time + 300 * 10**6,
        )
        durations = (50 * 10**6, 100 * 10**6, 200 * 10**6)
        end_times = (
            start_times[0] + durations[0],
            start_times[1] + durations[1],
            start_times[2] + durations[2],
        )
        span_context = trace_api.SpanContext(
            trace_id,
            span_id,
            is_remote=False,
            trace_flags=TraceFlags(TraceFlags.SAMPLED),
            trace_state=trace_api.TraceState({"testKey": "testValue"}),
        )
        parent_context = trace_api.SpanContext(trace_id,
                                               parent_id,
                                               is_remote=False)
        other_context = trace_api.SpanContext(trace_id,
                                              span_id,
                                              is_remote=False)
        event_attributes = {
            "annotation_bool": True,
            "annotation_string": "annotation_test",
            "key_float": 0.3,
        }
        event_timestamp = base_time + 50 * 10**6
        event = trace.Event(
            name="event0",
            timestamp=event_timestamp,
            attributes=event_attributes,
        )
        link_attributes = {"key_bool": True}
        link_1 = trace_api.Link(context=other_context,
                                attributes=link_attributes)
        link_2 = trace_api.Link(context=parent_context,
                                attributes=link_attributes)
        span_1 = trace.Span(
            name="test1",
            context=span_context,
            parent=parent_context,
            events=(event, ),
            links=(link_1, ),
            kind=trace_api.SpanKind.CLIENT,
        )
        span_2 = trace.Span(
            name="test2",
            context=parent_context,
            parent=None,
            kind=trace_api.SpanKind.SERVER,
        )
        span_3 = trace.Span(
            name="test3",
            context=other_context,
            links=(link_2, ),
            parent=span_2.get_context(),
        )
        otel_spans = [span_1, span_2, span_3]
        otel_spans[0].start(start_time=start_times[0])
        otel_spans[0].set_attribute("key_bool", False)
        otel_spans[0].set_attribute("key_string", "hello_world")
        otel_spans[0].set_attribute("key_float", 111.22)
        otel_spans[0].set_attribute("key_int", 333)
        otel_spans[0].set_status(
            trace_api.Status(
                trace_api.status.StatusCanonicalCode.INTERNAL,
                "test description",
            ))
        otel_spans[0].end(end_time=end_times[0])
        otel_spans[1].start(start_time=start_times[1])
        otel_spans[1].set_status(
            trace_api.Status(
                trace_api.status.StatusCanonicalCode.INTERNAL,
                {"test", "val"},
            ))
        otel_spans[1].end(end_time=end_times[1])
        otel_spans[2].start(start_time=start_times[2])
        otel_spans[2].end(end_time=end_times[2])
        output_spans = translate_to_collector(otel_spans)

        self.assertEqual(len(output_spans), 3)
        self.assertEqual(output_spans[0].trace_id,
                         b"n\x0cc%}\xe3L\x92o\x9e\xfc\xd09''.")
        self.assertEqual(output_spans[0].span_id,
                         b"4\xbf\x92\xde\xef\xc5\x8c\x92")
        self.assertEqual(output_spans[0].name,
                         trace_pb2.TruncatableString(value="test1"))
        self.assertEqual(output_spans[1].name,
                         trace_pb2.TruncatableString(value="test2"))
        self.assertEqual(output_spans[2].name,
                         trace_pb2.TruncatableString(value="test3"))
        self.assertEqual(
            output_spans[0].start_time.seconds,
            int(start_times[0] / 1000000000),
        )
        self.assertEqual(output_spans[0].end_time.seconds,
                         int(end_times[0] / 1000000000))
        self.assertEqual(output_spans[0].kind, trace_api.SpanKind.CLIENT.value)
        self.assertEqual(output_spans[1].kind, trace_api.SpanKind.SERVER.value)

        self.assertEqual(output_spans[0].parent_span_id,
                         b"\x11\x11\x11\x11\x11\x11\x11\x11")
        self.assertEqual(output_spans[2].parent_span_id,
                         b"\x11\x11\x11\x11\x11\x11\x11\x11")
        self.assertEqual(
            output_spans[0].status.code,
            trace_api.status.StatusCanonicalCode.INTERNAL.value,
        )
        self.assertEqual(output_spans[0].status.message, "test description")
        self.assertEqual(len(output_spans[0].tracestate.entries), 1)
        self.assertEqual(output_spans[0].tracestate.entries[0].key, "testKey")
        self.assertEqual(output_spans[0].tracestate.entries[0].value,
                         "testValue")

        self.assertEqual(
            output_spans[0].attributes.attribute_map["key_bool"].bool_value,
            False,
        )
        self.assertEqual(
            output_spans[0].attributes.attribute_map["key_string"].
            string_value.value,
            "hello_world",
        )
        self.assertEqual(
            output_spans[0].attributes.attribute_map["key_float"].double_value,
            111.22,
        )
        self.assertEqual(
            output_spans[0].attributes.attribute_map["key_int"].int_value, 333)

        self.assertEqual(
            output_spans[0].time_events.time_event[0].time.seconds, 683647322)
        self.assertEqual(
            output_spans[0].time_events.time_event[0].annotation.description.
            value,
            "event0",
        )
        self.assertEqual(
            output_spans[0].time_events.time_event[0].annotation.attributes.
            attribute_map["annotation_bool"].bool_value,
            True,
        )
        self.assertEqual(
            output_spans[0].time_events.time_event[0].annotation.attributes.
            attribute_map["annotation_string"].string_value.value,
            "annotation_test",
        )
        self.assertEqual(
            output_spans[0].time_events.time_event[0].annotation.attributes.
            attribute_map["key_float"].double_value,
            0.3,
        )

        self.assertEqual(
            output_spans[0].links.link[0].trace_id,
            b"n\x0cc%}\xe3L\x92o\x9e\xfc\xd09''.",
        )
        self.assertEqual(
            output_spans[0].links.link[0].span_id,
            b"4\xbf\x92\xde\xef\xc5\x8c\x92",
        )
        self.assertEqual(
            output_spans[0].links.link[0].type,
            trace_pb2.Span.Link.Type.TYPE_UNSPECIFIED,
        )
        self.assertEqual(
            output_spans[1].status.code,
            trace_api.status.StatusCanonicalCode.INTERNAL.value,
        )
        self.assertEqual(
            output_spans[2].links.link[0].type,
            trace_pb2.Span.Link.Type.PARENT_LINKED_SPAN,
        )
        self.assertEqual(
            output_spans[0].links.link[0].attributes.attribute_map["key_bool"].
            bool_value,
            True,
        )
コード例 #4
0
    def test_basic_span_translation(self):
        hex_encoder = codecs.getencoder('hex')

        span_data = span_data_module.SpanData(
            name="name",
            context=span_context_module.SpanContext(
                trace_id='6e0c63257de34c92bf9efcd03927272e'),
            span_id='6e0c63257de34c92',
            parent_span_id='6e0c63257de34c93',
            attributes={
                'test_str_key': 'test_str_value',
                'test_int_key': 1,
                'test_bool_key': False,
                'test_double_key': 567.89
            },
            start_time='2017-08-15T18:02:26.071158Z',
            end_time='2017-08-15T18:02:36.071158Z',
            child_span_count=None,
            stack_trace=None,
            time_events=None,
            links=None,
            status=None,
            same_process_as_parent_span=None,
            span_kind=0)

        pb_span = utils.translate_to_trace_proto(span_data)

        self.assertEqual(pb_span.name.value, "name")
        self.assertEqual(
            hex_encoder(pb_span.trace_id)[0],
            b'6e0c63257de34c92bf9efcd03927272e')
        self.assertEqual(hex_encoder(pb_span.span_id)[0], b'6e0c63257de34c92')
        self.assertEqual(
            hex_encoder(pb_span.parent_span_id)[0], b'6e0c63257de34c93')
        self.assertEqual(pb_span.kind, 0)

        self.assertEqual(len(pb_span.attributes.attribute_map), 4)
        self.assertEqual(
            pb_span.attributes.attribute_map['test_str_key'],
            trace_pb2.AttributeValue(string_value=trace_pb2.TruncatableString(
                value='test_str_value')))

        self.assertEqual(pb_span.attributes.attribute_map['test_int_key'],
                         trace_pb2.AttributeValue(int_value=1))
        self.assertEqual(pb_span.attributes.attribute_map['test_bool_key'],
                         trace_pb2.AttributeValue(bool_value=False))
        self.assertEqual(pb_span.attributes.attribute_map['test_double_key'],
                         trace_pb2.AttributeValue(double_value=567.89))

        self.assertEqual(pb_span.start_time.ToJsonString(),
                         '2017-08-15T18:02:26.071158Z')
        self.assertEqual(pb_span.end_time.ToJsonString(),
                         '2017-08-15T18:02:36.071158Z')

        self.assertEqual(pb_span.child_span_count.value, 0)
        self.assertEqual(pb_span.same_process_as_parent_span.value, False)
        self.assertEqual(len(pb_span.time_events.time_event), 0)

        self.assertEqual(pb_span.status.code, 0)
        self.assertFalse(pb_span.status.message)

        self.assertEqual(len(pb_span.links.link), 0)
        self.assertEqual(len(pb_span.tracestate.entries), 0)
コード例 #5
0
    def test_translate_time_events(self):

        annotation0_ts = datetime.utcnow() + timedelta(seconds=-10)
        annotation1_ts = datetime.utcnow() + timedelta(seconds=-9)
        message0_ts = datetime.utcnow() + timedelta(seconds=-8)
        message1_ts = datetime.utcnow() + timedelta(seconds=-7)
        message2_ts = datetime.utcnow() + timedelta(seconds=-6)

        span_data = span_data_module.SpanData(
            context=span_context_module.SpanContext(
                trace_id='6e0c63257de34c92bf9efcd03927272e'),
            span_id='6e0c63257de34c92',
            time_events=[
                time_event_module.TimeEvent(
                    timestamp=annotation0_ts,
                    annotation=time_event_module.Annotation(
                        description="hi there0",
                        attributes=attributes_module.Attributes(
                            attributes={
                                'test_str_key': 'test_str_value',
                                'test_int_key': 1,
                                'test_bool_key': False,
                                'test_double_key': 567.89
                            }))),
                time_event_module.TimeEvent(
                    timestamp=annotation1_ts,
                    annotation=time_event_module.Annotation(
                        description="hi there1")),
                time_event_module.TimeEvent(
                    timestamp=message0_ts,
                    message_event=time_event_module.MessageEvent(
                        id=0,
                        type=time_event_module.Type.SENT,
                        uncompressed_size_bytes=10,
                        compressed_size_bytes=1)),
                time_event_module.TimeEvent(
                    timestamp=message1_ts,
                    message_event=time_event_module.MessageEvent(
                        id=1,
                        type=time_event_module.Type.RECEIVED,
                        uncompressed_size_bytes=20,
                        compressed_size_bytes=2)),
                time_event_module.TimeEvent(
                    timestamp=message2_ts,
                    message_event=time_event_module.MessageEvent(
                        id=2,
                        type=time_event_module.Type.TYPE_UNSPECIFIED,
                        uncompressed_size_bytes=30,
                        compressed_size_bytes=3))
            ],
            span_kind=span_module.SpanKind.SERVER,
            status=None,
            start_time=None,
            end_time=None,
            child_span_count=None,
            name=None,
            parent_span_id=None,
            attributes=None,
            same_process_as_parent_span=False,
            stack_trace=None,
            links=None)

        pb_span = utils.translate_to_trace_proto(span_data)

        self.assertEqual(len(pb_span.time_events.time_event), 5)

        event0 = pb_span.time_events.time_event[0]
        event1 = pb_span.time_events.time_event[1]
        event2 = pb_span.time_events.time_event[2]
        event3 = pb_span.time_events.time_event[3]
        event4 = pb_span.time_events.time_event[4]
        self.assertEqual(event0.time.ToDatetime(), annotation0_ts)
        self.assertEqual(event1.time.ToDatetime(), annotation1_ts)
        self.assertEqual(event2.time.ToDatetime(), message0_ts)
        self.assertEqual(event3.time.ToDatetime(), message1_ts)
        self.assertEqual(event4.time.ToDatetime(), message2_ts)

        self.assertEqual(event0.annotation.description.value, "hi there0")
        self.assertEqual(event1.annotation.description.value, "hi there1")

        self.assertEqual(len(event0.annotation.attributes.attribute_map), 4)
        self.assertEqual(len(event1.annotation.attributes.attribute_map), 0)

        self.assertEqual(
            event0.annotation.attributes.attribute_map['test_str_key'],
            trace_pb2.AttributeValue(string_value=trace_pb2.TruncatableString(
                value='test_str_value')))

        self.assertEqual(
            event0.annotation.attributes.attribute_map['test_int_key'],
            trace_pb2.AttributeValue(int_value=1))
        self.assertEqual(
            event0.annotation.attributes.attribute_map['test_bool_key'],
            trace_pb2.AttributeValue(bool_value=False))
        self.assertEqual(
            event0.annotation.attributes.attribute_map['test_double_key'],
            trace_pb2.AttributeValue(double_value=567.89))

        self.assertEqual(event2.message_event.id, 0)
        self.assertEqual(event3.message_event.id, 1)
        self.assertEqual(event4.message_event.id, 2)

        self.assertEqual(event2.message_event.uncompressed_size, 10)
        self.assertEqual(event3.message_event.uncompressed_size, 20)
        self.assertEqual(event4.message_event.uncompressed_size, 30)

        self.assertEqual(event2.message_event.compressed_size, 1)
        self.assertEqual(event3.message_event.compressed_size, 2)
        self.assertEqual(event4.message_event.compressed_size, 3)

        self.assertEqual(event2.message_event.type, 1)
        self.assertEqual(event3.message_event.type, 2)
        self.assertEqual(event4.message_event.type, 0)
コード例 #6
0
    def test_translate_links(self):
        hex_encoder = codecs.getencoder('hex')

        span_data = span_data_module.SpanData(
            context=span_context_module.SpanContext(
                trace_id='6e0c63257de34c92bf9efcd03927272e'),
            span_id='6e0c63257de34c92',
            links=[
                link_module.Link(trace_id='0e0c63257de34c92bf9efcd03927272e',
                                 span_id='0e0c63257de34c92',
                                 type=link_module.Type.TYPE_UNSPECIFIED,
                                 attributes=attributes_module.Attributes(
                                     attributes={
                                         'test_str_key': 'test_str_value',
                                         'test_int_key': 1,
                                         'test_bool_key': False,
                                         'test_double_key': 567.89
                                     })),
                link_module.Link(trace_id='1e0c63257de34c92bf9efcd03927272e',
                                 span_id='1e0c63257de34c92',
                                 type=link_module.Type.CHILD_LINKED_SPAN),
                link_module.Link(trace_id='2e0c63257de34c92bf9efcd03927272e',
                                 span_id='2e0c63257de34c92',
                                 type=link_module.Type.PARENT_LINKED_SPAN)
            ],
            span_kind=span_module.SpanKind.SERVER,
            status=None,
            start_time=None,
            end_time=None,
            child_span_count=None,
            name=None,
            parent_span_id=None,
            attributes=None,
            same_process_as_parent_span=False,
            stack_trace=None,
            time_events=None)

        pb_span = utils.translate_to_trace_proto(span_data)

        self.assertEqual(len(pb_span.links.link), 3)
        self.assertEqual(
            hex_encoder(pb_span.links.link[0].trace_id)[0],
            b'0e0c63257de34c92bf9efcd03927272e')
        self.assertEqual(
            hex_encoder(pb_span.links.link[1].trace_id)[0],
            b'1e0c63257de34c92bf9efcd03927272e')
        self.assertEqual(
            hex_encoder(pb_span.links.link[2].trace_id)[0],
            b'2e0c63257de34c92bf9efcd03927272e')

        self.assertEqual(
            hex_encoder(pb_span.links.link[0].span_id)[0], b'0e0c63257de34c92')
        self.assertEqual(
            hex_encoder(pb_span.links.link[1].span_id)[0], b'1e0c63257de34c92')
        self.assertEqual(
            hex_encoder(pb_span.links.link[2].span_id)[0], b'2e0c63257de34c92')

        self.assertEqual(pb_span.links.link[0].type, 0)
        self.assertEqual(pb_span.links.link[1].type, 1)
        self.assertEqual(pb_span.links.link[2].type, 2)

        self.assertEqual(len(pb_span.links.link[0].attributes.attribute_map),
                         4)
        self.assertEqual(len(pb_span.links.link[1].attributes.attribute_map),
                         0)
        self.assertEqual(len(pb_span.links.link[2].attributes.attribute_map),
                         0)

        self.assertEqual(
            pb_span.links.link[0].attributes.attribute_map['test_str_key'],
            trace_pb2.AttributeValue(string_value=trace_pb2.TruncatableString(
                value='test_str_value')))

        self.assertEqual(
            pb_span.links.link[0].attributes.attribute_map['test_int_key'],
            trace_pb2.AttributeValue(int_value=1))
        self.assertEqual(
            pb_span.links.link[0].attributes.attribute_map['test_bool_key'],
            trace_pb2.AttributeValue(bool_value=False))
        self.assertEqual(
            pb_span.links.link[0].attributes.attribute_map['test_double_key'],
            trace_pb2.AttributeValue(double_value=567.89))
コード例 #7
0
def translate_to_trace_proto(span_data):
    """Translates the opencensus spans to ocagent proto spans.

    :type span_data: :class:`~opencensus.trace.span_data.SpanData`
    :param span_data: SpanData tuples to convert to protobuf spans

    :rtype: :class:`~opencensus.proto.trace.Span`
    :returns: Protobuf format span.
    """

    if not span_data:
        return None

    pb_span = trace_pb2.Span(
        name=trace_pb2.TruncatableString(value=span_data.name),
        kind=span_data.span_kind,
        trace_id=hex_str_to_bytes_str(span_data.context.trace_id),
        span_id=hex_str_to_bytes_str(span_data.span_id),
        parent_span_id=hex_str_to_bytes_str(span_data.parent_span_id)
        if span_data.parent_span_id is not None else None,
        start_time=ocagent_utils.proto_ts_from_datetime_str(
            span_data.start_time),
        end_time=ocagent_utils.proto_ts_from_datetime_str(span_data.end_time),
        status=trace_pb2.Status(
            code=span_data.status.canonical_code,
            message=span_data.status.description,
        ) if span_data.status is not None else None,
        same_process_as_parent_span=BoolValue(
            value=span_data.same_process_as_parent_span)
        if span_data.same_process_as_parent_span is not None else None,
        child_span_count=UInt32Value(value=span_data.child_span_count)
        if span_data.child_span_count is not None else None)

    # attributes
    if span_data.attributes is not None:
        for attribute_key, attribute_value \
                in span_data.attributes.items():
            add_proto_attribute_value(pb_span.attributes, attribute_key,
                                      attribute_value)

    # annotations
    if span_data.annotations is not None:
        for annotation in span_data.annotations:
            pb_event = pb_span.time_events.time_event.add()
            pb_event.time.FromJsonString(annotation.timestamp)
            set_proto_annotation(pb_event.annotation, annotation)

    # message events
    if span_data.message_events is not None:
        for message_event in span_data.message_events:
            pb_event = pb_span.time_events.time_event.add()
            pb_event.time.FromJsonString(message_event.timestamp)
            set_proto_message_event(pb_event.message_event, message_event)

    # links
    if span_data.links is not None:
        for link in span_data.links:
            pb_link = pb_span.links.link.add(
                trace_id=hex_str_to_bytes_str(link.trace_id),
                span_id=hex_str_to_bytes_str(link.span_id),
                type=link.type)

            if link.attributes is not None and \
                    link.attributes.attributes is not None:
                for attribute_key, attribute_value \
                        in link.attributes.attributes.items():
                    add_proto_attribute_value(pb_link.attributes,
                                              attribute_key, attribute_value)

    # tracestate
    if span_data.context.tracestate is not None:
        for (key, value) in span_data.context.tracestate.items():
            pb_span.tracestate.entries.add(key=key, value=value)

    return pb_span