Beispiel #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))
Beispiel #2
0
    def test_add_attribute_value(self):
        pb_span = trace_pb2.Span()

        utils.add_proto_attribute_value(pb_span.attributes, 'int_key', 42)
        utils.add_proto_attribute_value(pb_span.attributes, 'bool_key', True)
        utils.add_proto_attribute_value(pb_span.attributes, 'string_key',
                                        'value')
        utils.add_proto_attribute_value(pb_span.attributes, 'unicode_key',
                                        u'uvalue')
        utils.add_proto_attribute_value(pb_span.attributes, 'dict_key',
                                        {"a": "b"})

        self.assertEqual(len(pb_span.attributes.attribute_map), 5)
        self.assertEqual(pb_span.attributes.attribute_map['int_key'].int_value,
                         42)
        self.assertEqual(
            pb_span.attributes.attribute_map['bool_key'].bool_value, True)
        self.assertEqual(
            pb_span.attributes.attribute_map['string_key'].string_value.value,
            'value')
        self.assertEqual(
            pb_span.attributes.attribute_map['unicode_key'].string_value.value,
            'uvalue')
        self.assertEqual(
            pb_span.attributes.attribute_map['dict_key'].string_value.value,
            "{'a': 'b'}")
Beispiel #3
0
    def test_set_annotation_without_attributes(self):
        pb_span = trace_pb2.Span()
        pb_event0 = pb_span.time_events.time_event.add()
        pb_event1 = pb_span.time_events.time_event.add()

        annotation0 = time_event_module.Annotation(description="hi there0")
        annotation1 = time_event_module.Annotation(
            description="hi there1", attributes=attributes_module.Attributes())

        utils.set_proto_annotation(pb_event0.annotation, annotation0)
        utils.set_proto_annotation(pb_event1.annotation, annotation1)

        self.assertEqual(pb_event0.annotation.description.value, "hi there0")
        self.assertEqual(pb_event1.annotation.description.value, "hi there1")
        self.assertEqual(len(pb_event0.annotation.attributes.attribute_map), 0)
        self.assertEqual(len(pb_event1.annotation.attributes.attribute_map), 0)
Beispiel #4
0
    def test_set_proto_event(self):
        pb_span = trace_pb2.Span()
        pb_event = pb_span.time_events.time_event.add()

        message_event = time_event_module.MessageEvent(
            id=0,
            type=time_event_module.Type.SENT,
            uncompressed_size_bytes=10,
            compressed_size_bytes=1)

        utils.set_proto_message_event(pb_event.message_event, message_event)

        self.assertEqual(pb_event.message_event.id, 0)
        self.assertEqual(pb_event.message_event.type, 1)
        self.assertEqual(pb_event.message_event.uncompressed_size, 10)
        self.assertEqual(pb_event.message_event.compressed_size, 1)
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
Beispiel #6
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