From 8362b0d21959e0d1b7596e83fbcaecc3280cbc03 Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Fri, 21 Apr 2023 16:03:52 +0000
Subject: [PATCH] Common - Method Wrappers:

- Improved label specification while getting meters
---
 src/common/method_wrappers/Decorator.py | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/common/method_wrappers/Decorator.py b/src/common/method_wrappers/Decorator.py
index a5db54125..197378dad 100644
--- a/src/common/method_wrappers/Decorator.py
+++ b/src/common/method_wrappers/Decorator.py
@@ -15,7 +15,7 @@
 import grpc, json, logging, threading
 from enum import Enum
 from prettytable import PrettyTable
-from typing import Any, Dict, List, Set, Tuple
+from typing import Any, Dict, List, Optional, Set, Tuple
 from prometheus_client import Counter, Histogram
 from prometheus_client.metrics import MetricWrapperBase, INF
 from common.tools.grpc.Tools import grpc_message_to_json_string
@@ -75,18 +75,21 @@ class MetricsPool:
             return MetricsPool.metrics[metric_name]
 
     def get_metrics(
-        self, method : str
-    ) -> Tuple[MetricWrapperBase, MetricWrapperBase, MetricWrapperBase, MetricWrapperBase]:
+        self, method : str, labels : Optional[Dict[str, str]] = None
+    ) -> Tuple[Histogram, Counter, Counter, Counter]:
         histogram_duration : Histogram = self.get_or_create(method, MetricTypeEnum.HISTOGRAM_DURATION)
         counter_started    : Counter   = self.get_or_create(method, MetricTypeEnum.COUNTER_STARTED)
         counter_completed  : Counter   = self.get_or_create(method, MetricTypeEnum.COUNTER_COMPLETED)
         counter_failed     : Counter   = self.get_or_create(method, MetricTypeEnum.COUNTER_FAILED)
 
-        if len(self._labels) > 0:
-            histogram_duration = histogram_duration.labels(**(self._labels))
-            counter_started    = counter_started.labels(**(self._labels))
-            counter_completed  = counter_completed.labels(**(self._labels))
-            counter_failed     = counter_failed.labels(**(self._labels))
+        if labels is None and len(self._labels) > 0:
+            labels = self._labels
+
+        if labels is not None and len(labels) > 0:
+            histogram_duration = histogram_duration.labels(**labels)
+            counter_started    = counter_started.labels(**labels)
+            counter_completed  = counter_completed.labels(**labels)
+            counter_failed     = counter_failed.labels(**labels)
 
         return histogram_duration, counter_started, counter_completed, counter_failed
 
-- 
GitLab