Fix non-existing gauge duplicate
In Elasticsearch 7.x hits,total field
is not integer, but dictionary causing
failure to create proper gauge,
which is registered in collector
causing duplicate error.
For other cases - reregister Gauge collector
Change-Id: Ied8cc69a4ea4c1482b3670e2fab9a66ff0d386e7
Related-bug: PROD-32451
diff --git a/prometheus_es_exporter/__init__.py b/prometheus_es_exporter/__init__.py
index 6d8c233..2b5f15b 100644
--- a/prometheus_es_exporter/__init__.py
+++ b/prometheus_es_exporter/__init__.py
@@ -68,6 +68,11 @@
return metric_dict
+def unregister_collector_by_metric(registry, metric_name):
+ collector = registry._names_to_collectors[metric_name]
+ registry.unregister(collector)
+
+
def update_gauges(metrics):
metric_dict = group_metrics(metrics)
@@ -76,7 +81,11 @@
(old_label_values_set, gauge) = gauges[metric_name]
else:
old_label_values_set = set()
- gauge = Gauge(metric_name, '', label_keys)
+ try:
+ gauge = Gauge(metric_name, '', label_keys, registry=REGISTRY)
+ except ValueError:
+ unregister_collector_by_metric(REGISTRY, metric_name)
+ gauge = Gauge(metric_name, '', label_keys, registry=REGISTRY)
new_label_values_set = set(value_dict.keys())
diff --git a/prometheus_es_exporter/parser.py b/prometheus_es_exporter/parser.py
index 9ec59fe..f978cf2 100644
--- a/prometheus_es_exporter/parser.py
+++ b/prometheus_es_exporter/parser.py
@@ -80,7 +80,10 @@
result = []
if not response['timed_out']:
- result.append((metric + ['hits'], {}, response['hits']['total']))
+ hits_total = response['hits']['total']
+ if isinstance(hits_total, dict):
+ hits_total = hits_total.get('value', 0)
+ result.append((metric + ['hits'], {}, hits_total))
result.append((metric + ['took', 'milliseconds'], {}, response['took']))
if 'aggregations' in response.keys():