def _get_time_series_list_proto(series_list): return [ metrics_pb2.TimeSeries( start_timestamp=utils.proto_ts_from_datetime_str( series.start_timestamp), label_values=_get_label_values_proto(series.label_values), points=_get_points_proto(series.points)) for series in series_list ]
def test_datetime_str_to_proto_ts_conversion(self): now = datetime.utcnow() delta = now - datetime(1970, 1, 1) expected_seconds = int(delta.total_seconds()) expected_nanos = delta.microseconds * 1000 proto_ts = utils.proto_ts_from_datetime_str( common_utils.to_iso_str(now)) self.assertEqual(proto_ts.seconds, int(expected_seconds)) self.assertEqual(proto_ts.nanos, expected_nanos)
def _get_exemplar_proto(exemplar): return metrics_pb2.DistributionValue.Exemplar( value=exemplar.value, timestamp=utils.proto_ts_from_datetime_str(exemplar.timestamp), attachments=exemplar.attachments)
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
def test_datetime_str_to_proto_ts_conversion_invalid(self): proto_ts = utils.proto_ts_from_datetime_str('2018 08 22 T 11:53') self.assertEqual(proto_ts.seconds, 0) self.assertEqual(proto_ts.nanos, 0)
def test_datetime_str_to_proto_ts_conversion_empty(self): proto_ts = utils.proto_ts_from_datetime_str('') self.assertEqual(proto_ts.seconds, 0) self.assertEqual(proto_ts.nanos, 0)
def test_datetime_str_to_proto_ts_conversion_none(self): proto_ts = utils.proto_ts_from_datetime_str(None) self.assertEquals(proto_ts.seconds, 0) self.assertEquals(proto_ts.nanos, 0)