def translate_to_collector(
    metric_records: Sequence[MetricRecord], ) -> Sequence[metrics_pb2.Metric]:
    collector_metrics = []
    for metric_record in metric_records:

        label_values = []
        label_keys = []
        for label_tuple in metric_record.labels:
            label_keys.append(metrics_pb2.LabelKey(key=label_tuple[0]))
            label_values.append(
                metrics_pb2.LabelValue(has_value=label_tuple[1] is not None,
                                       value=label_tuple[1]))

        metric_descriptor = metrics_pb2.MetricDescriptor(
            name=metric_record.metric.name,
            description=metric_record.metric.description,
            unit=metric_record.metric.unit,
            type=get_collector_metric_type(metric_record.metric),
            label_keys=label_keys,
        )

        timeseries = metrics_pb2.TimeSeries(
            label_values=label_values,
            points=[get_collector_point(metric_record)],
        )
        collector_metrics.append(
            metrics_pb2.Metric(metric_descriptor=metric_descriptor,
                               timeseries=[timeseries]))
    return collector_metrics
def translate_to_collector(
    export_records: Sequence[ExportRecord],
    exporter_start_timestamp: Timestamp,
) -> Sequence[metrics_pb2.Metric]:
    collector_metrics = []
    for export_record in export_records:

        label_values = []
        label_keys = []
        for label_tuple in export_record.labels:
            label_keys.append(metrics_pb2.LabelKey(key=label_tuple[0]))
            label_values.append(
                metrics_pb2.LabelValue(
                    has_value=label_tuple[1] is not None,
                    value=str(label_tuple[1]),
                )
            )

        metric_descriptor = metrics_pb2.MetricDescriptor(
            name=export_record.instrument.name,
            description=export_record.instrument.description,
            unit=export_record.instrument.unit,
            type=get_collector_metric_type(export_record.instrument),
            label_keys=label_keys,
        )

        # If cumulative and stateful, explicitly set the start_timestamp to
        # exporter start time.
        if export_record.instrument.meter.processor.stateful:
            start_timestamp = exporter_start_timestamp
        else:
            start_timestamp = None

        timeseries = metrics_pb2.TimeSeries(
            label_values=label_values,
            points=[get_collector_point(export_record)],
            start_timestamp=start_timestamp,
        )
        collector_metrics.append(
            metrics_pb2.Metric(
                metric_descriptor=metric_descriptor,
                timeseries=[timeseries],
                resource=get_resource(export_record),
            )
        )
    return collector_metrics
def _get_metric_proto(metric):
    return metrics_pb2.Metric(
        metric_descriptor=_get_metric_descriptor_proto(metric.descriptor),
        timeseries=_get_time_series_list_proto(metric.time_series))