From 7acdefd4d8770f9dccfcd9d22cd92583fcc9383b Mon Sep 17 00:00:00 2001 From: Akanksha Gupta Date: Fri, 24 Jul 2026 11:29:12 -0700 Subject: [PATCH] Avoid recreating existing metric descriptors in MetricsCollector Before creating a metric descriptor, the MetricsCollector now checks if it already exists using get_metric_descriptor. If the descriptor is found, creation is skipped to avoid redundant API calls. PiperOrigin-RevId: 953471222 --- .../shared_pathways_service/metrics_collector.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pathwaysutils/experimental/shared_pathways_service/metrics_collector.py b/pathwaysutils/experimental/shared_pathways_service/metrics_collector.py index 01939f9..d00ab4e 100644 --- a/pathwaysutils/experimental/shared_pathways_service/metrics_collector.py +++ b/pathwaysutils/experimental/shared_pathways_service/metrics_collector.py @@ -243,6 +243,15 @@ def _create_metric_descriptor( """Creates a metric descriptor if not already present.""" metric_type = METRIC_PREFIX + name display_name = display_name or name + descriptor_name = ( + f"projects/{self.project_id}/metricDescriptors/{metric_type}" + ) + try: + self.client.get_metric_descriptor(name=descriptor_name) + _logger.debug("Metric descriptor %s already exists.", metric_type) + return + except exceptions.NotFound: + pass try: self.client.create_metric_descriptor(