docstr: to_metric translate the data that OpenCensus create
  to Prometheus format, using Prometheus Metric object.
  
  This method is from Opencensus Prometheus Exporter.
  
  Args:
      metric_name: Name of the metric.
      metric_description: Description of the metric.
      label_keys: The fixed label keys of the metric.
      metric_units: Units of the metric.
      label_values: The values of `label_keys`.
      agg_data: `opencensus.stats.aggregation_data.AggregationData` object.
          Aggregated data that needs to be converted as Prometheus samples
  
  Returns:
      A Prometheus metric object
docstr_tok: ["to_metric","translate","the","data","that","OpenCensus","create","to","Prometheus","format",",","using","Prometheus","Metric","object",".","This","method","is","from","Opencensus","Prometheus","Exporter",".","Args",":","metric_name",":","Name","of","the","metric",".","metric_description",":","Description","of","the","metric",".","label_keys",":","The","fixed","label","keys","of","the","metric",".","metric_units",":","Units","of","the","metric",".","label_values",":","The","values","of","`","label_keys","`",".","agg_data",":","`","opencensus.stats.aggregation_data.AggregationData","`","object",".","Aggregated","data","that","needs","to","be","converted","as","Prometheus","samples","Returns",":","A","Prometheus","metric","object"]
code: def to_metric(
      self,
      metric_name: str,
      metric_description: str,
      label_keys: List[str],
      metric_units: str,
      label_values: Tuple[tag_value_module.TagValue],
      agg_data: Any,
      metrics_map: Dict[str, PrometheusMetric],
  ) -> PrometheusMetric:
      """to_metric translate the data that OpenCensus create
      to Prometheus format, using Prometheus Metric object.
  
      This method is from Opencensus Prometheus Exporter.
  
      Args:
          metric_name: Name of the metric.
          metric_description: Description of the metric.
          label_keys: The fixed label keys of the metric.
          metric_units: Units of the metric.
          label_values: The values of `label_keys`.
          agg_data: `opencensus.stats.aggregation_data.AggregationData` object.
              Aggregated data that needs to be converted as Prometheus samples
  
      Returns:
          A Prometheus metric object
      """
      assert self._components_lock.locked()
      metric_name = f"{self._namespace}_{metric_name}"
      assert len(label_values) == len(label_keys), (
          label_values,
          label_keys,
      )
      # Prometheus requires that all tag values be strings hence
      # the need to cast none to the empty string before exporting. See
      # https://github.com/census-instrumentation/opencensus-python/issues/480
      label_values = [tv if tv else "" for tv in label_values]
  
      if isinstance(agg_data, CountAggregationData):
          metric = metrics_map.get(metric_name)
          if not metric:
              metric = CounterMetricFamily(
                  name=metric_name,
                  documentation=metric_description,
                  unit=metric_units,
                  labels=label_keys,
              )
              metrics_map[metric_name] = metric
          metric.add_metric(
              labels=label_values, value=agg_data.count_data
          )
          return metric
  
      elif isinstance(agg_data, DistributionAggregationData):
          assert agg_data.bounds == sorted(agg_data.bounds)
          # buckets are a list of buckets. Each bucket is another list with
          # a pair of bucket name and value, or a triple of bucket name,
          # value, and exemplar. buckets need to be in order.
          buckets = []
          cum_count = 0  # Prometheus buckets expect cumulative count.
          for ii, bound in enumerate(agg_data.bounds):
              cum_count += agg_data.counts_per_bucket[ii]
              bucket = [str(bound), cum_count]
              buckets.append(bucket)
          # Prometheus requires buckets to be sorted, and +Inf present.
          # In OpenCensus we don't have +Inf in the bucket bonds so need to
          # append it here.
          buckets.append(["+Inf", agg_data.count_data])
          metric = metrics_map.get(metric_name)
          if not metric:
              metric = HistogramMetricFamily(
                  name=metric_name,
                  documentation=metric_description,
                  labels=label_keys,
              )
              metrics_map[metric_name] = metric
          metric.add_metric(
              labels=label_values,
              buckets=buckets,
              sum_value=agg_data.sum,
          )
          return metric
  
      elif isinstance(agg_data, LastValueAggregationData):
          metric = metrics_map.get(metric_name)
          if not metric:
              metric = GaugeMetricFamily(
                  name=metric_name,
                  documentation=metric_description,
                  labels=label_keys,
              )
              metrics_map[metric_name] = metric
          metric.add_metric(labels=label_values, value=agg_data.value)
          return metric
  
      else:
          raise ValueError(
              f"unsupported aggregation type {type(agg_data)}"
          )
  
imports: import json
  import logging
  import os
  import re
  import threading
  import time
  import traceback
  from collections import namedtuple
  from typing import List, Tuple, Any, Dict
  from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily, HistogramMetricFamily
  from opencensus.metrics.export.value import ValueDouble
  from opencensus.stats import aggregation
  from opencensus.stats import measure
  from opencensus.stats.view_manager import ViewManager
  from opencensus.stats.stats_recorder import StatsRecorder
  from opencensus.stats.base_exporter import StatsExporter
  from prometheus_client.core import Metric
  from opencensus.stats.aggregation_data import CountAggregationData, DistributionAggregationData, LastValueAggregationData
  from opencensus.stats.view import View
  from opencensus.tags import tag_key
  from opencensus.tags import tag_map
  from opencensus.tags import tag_value
  import ray
  from ray._raylet import GcsClient
  from ray.core.generated.metrics_pb2 import Metric
label_desc: Use image node_id 4 for calling the OpenCensusProxyCollector obj's underlying member method code with example usage: obj.to_metric(metric_name, metric_description, label_keys, metric_units, label_values, agg_data, metrics_map) and returns: metric, metric, metric