Ejemplo n.º 1
0
def get_collector_point(metric_record: MetricRecord) -> metrics_pb2.Point:
    # TODO: horrible hack to get original list of keys to then get the bound
    # instrument
    point = metrics_pb2.Point(timestamp=utils.proto_timestamp_from_time_ns(
        metric_record.aggregator.last_update_timestamp))
    if metric_record.instrument.value_type == int:
        point.int64_value = metric_record.aggregator.checkpoint
    elif metric_record.instrument.value_type == float:
        point.double_value = metric_record.aggregator.checkpoint
    else:
        raise TypeError("Unsupported metric type: {}".format(
            metric_record.instrument.value_type))
    return point
Ejemplo n.º 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
 def test_proto_timestamp_from_time_ns(self):
     result = utils.proto_timestamp_from_time_ns(12345)
     self.assertIsInstance(result, Timestamp)
     self.assertEqual(result.nanos, 12345)