Refactors Prometheus exporter to group metrics correctly by name 25/6925/1
authorBenjamin Diaz <bdiaz@whitestack.com>
Tue, 20 Nov 2018 14:56:43 +0000 (11:56 -0300)
committerBenjamin Diaz <bdiaz@whitestack.com>
Tue, 20 Nov 2018 14:56:43 +0000 (11:56 -0300)
Metric names were appearing duplicated in the exporter which generated issues
integrating with external tools such as Metricbeat and Kibana.

Signed-off-by: Benjamin Diaz <bdiaz@whitestack.com>
Jenkinsfile
osm_mon/collector/backends/prometheus.py

index 6afef69..635a5e6 100644 (file)
@@ -5,7 +5,7 @@ properties([
         string(defaultValue: env.GERRIT_REFSPEC, description: '', name: 'GERRIT_REFSPEC'),
         string(defaultValue: env.GERRIT_PATCHSET_REVISION, description: '', name: 'GERRIT_PATCHSET_REVISION'),
         string(defaultValue: 'https://osm.etsi.org/gerrit', description: '', name: 'PROJECT_URL_PREFIX'),
-        booleanParam(defaultValue: false, description: '', name: 'TEST_INSTALL'),
+        booleanParam(defaultValue: true, description: '', name: 'TEST_INSTALL'),
         string(defaultValue: 'artifactory-osm', description: '', name: 'ARTIFACTORY_SERVER'),
     ])
 ])
index 1bc3ff4..31f3122 100644 (file)
@@ -43,16 +43,17 @@ class PrometheusBackend(BaseBackend):
     def handle(self, metrics: List[Metric]):
         log.debug('handle')
         log.debug('metrics: %s', metrics)
-        prometheus_metrics = []
+        prometheus_metrics = {}
         for metric in metrics:
-            prometheus_metric = GaugeMetricFamily(
-                OSM_METRIC_PREFIX + metric.name,
-                'OSM metric',
-                labels=['ns_id', 'vnf_member_index', 'vdu_name']
-            )
-            prometheus_metric.add_metric([metric.nsr_id, metric.vnf_member_index, metric.vdur_name], metric.value)
-            prometheus_metrics.append(prometheus_metric)
-        self.custom_collector.metrics = prometheus_metrics
+            if metric.name not in prometheus_metrics:
+                prometheus_metrics[metric.name] = GaugeMetricFamily(
+                    OSM_METRIC_PREFIX + metric.name,
+                    'OSM metric',
+                    labels=['ns_id', 'vnf_member_index', 'vdu_name']
+                )
+            prometheus_metrics[metric.name].add_metric([metric.nsr_id, metric.vnf_member_index, metric.vdur_name],
+                                                       metric.value)
+        self.custom_collector.metrics = prometheus_metrics.values()
 
     def _start_exporter(self, port):
         log.debug('_start_exporter')