docstr: A class to record and export metrics.
  
  The class exports metrics in 2 different ways.
  - Directly record and export metrics using OpenCensus.
  - Proxy metrics from other core components
      (e.g., raylet, GCS, core workers).
  
  This class is thread-safe.
docstr_tok: ["A","class","to","record","and","export","metrics",".","The","class","exports","metrics","in","2","different","ways",".","-","Directly","record","and","export","metrics","using","OpenCensus",".","-","Proxy","metrics","from","other","core","components","(","e.g.",",","raylet",",","GCS",",","core","workers",")",".","This","class","is","thread-safe","."]
code: def __init__(
      self,
      view_manager: ViewManager,
      stats_recorder: StatsRecorder,
      stats_exporter: StatsExporter = None,
  ):
      """A class to record and export metrics.
  
      The class exports metrics in 2 different ways.
      - Directly record and export metrics using OpenCensus.
      - Proxy metrics from other core components
          (e.g., raylet, GCS, core workers).
  
      This class is thread-safe.
      """
      # Lock required because gRPC server uses
      # multiple threads to process requests.
      self._lock = threading.Lock()
  
      #
      # Opencensus components to record metrics.
      #
  
      # Managing views to export metrics
      # If the stats_exporter is None, we disable all metrics export.
      self.view_manager = view_manager
      # A class that's used to record metrics
      # emitted from the current process.
      self.stats_recorder = stats_recorder
      # A class to export metrics.
      self.stats_exporter = stats_exporter
      # -- A Prometheus custom collector to proxy export metrics --
      # `None` if the prometheus server is not started.
      self.proxy_exporter_collector = None
  
      if self.stats_exporter is None:
          # If the exporter is not given,
          # we disable metrics collection.
          self.view_manager = None
      else:
          self.view_manager.register_exporter(stats_exporter)
          self.proxy_exporter_collector = OpenCensusProxyCollector(
              self.stats_exporter.options.namespace,
              component_timeout_s=int(
                  os.getenv(RAY_WORKER_TIMEOUT_S, 120)
              ),
          )
  
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 1 to create a new MetricsAgent object  with example: obj = MetricsAgent(view_manager, stats_recorder, stats_exporter)