From 3fa55e48dc6de01170d1c1fe4566373555b2274f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Gervais?= Date: Thu, 23 May 2019 10:33:09 +0200 Subject: [PATCH] :bug: Handle missing metrics from metadata. --- qdb_prometheus_exporter/collector.py | 67 +++++++++++++++------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/qdb_prometheus_exporter/collector.py b/qdb_prometheus_exporter/collector.py index c20d94f..5c86188 100644 --- a/qdb_prometheus_exporter/collector.py +++ b/qdb_prometheus_exporter/collector.py @@ -31,38 +31,41 @@ def collect_metric(self, metric_type: MetricType, metric_key: str): def collect(self): for statistic in self._available_statistics: - metadata = get_metrics_metadata(statistic) - name = self.metric_name(statistic) - - logging.info("Collecting metric {}.", statistic) - - if metadata['type'] == MetricType.COUNTER: - metric = CounterMetricFamily(name=name, - documentation=metadata['description'], - labels=["node", "host"], - unit=metadata['unit']) - metric.add_metric(labels=[self._node_id, self._hostname], - value=self.collect_metric(metric_type=metadata['type'], metric_key=statistic)) - yield metric - - if metadata['type'] == MetricType.GAUGE: - metric = GaugeMetricFamily(name=name, - documentation=metadata['description'], - labels=["node", "host"], - unit=metadata['unit']) - metric.add_metric(labels=[self._node_id, self._hostname], - value=self.collect_metric(metric_type=metadata['type'], metric_key=statistic)) - yield metric - - if metadata['type'] == MetricType.INFO: - metric = InfoMetricFamily(name=name, - documentation=metadata['description'], - labels=["node", "host"]) - metric.add_metric(labels=[self._node_id, self._hostname], - value={ - statistic: self.collect_metric(metric_type=metadata['type'], metric_key=statistic) - }) - yield metric + try: + metadata = get_metrics_metadata(statistic) + name = self.metric_name(statistic) + + logging.info("Collecting metric %s.", statistic) + + if metadata['type'] == MetricType.COUNTER: + metric = CounterMetricFamily(name=name, + documentation=metadata['description'], + labels=["node", "host"], + unit=metadata['unit']) + metric.add_metric(labels=[self._node_id, self._hostname], + value=self.collect_metric(metric_type=metadata['type'], metric_key=statistic)) + yield metric + + if metadata['type'] == MetricType.GAUGE: + metric = GaugeMetricFamily(name=name, + documentation=metadata['description'], + labels=["node", "host"], + unit=metadata['unit']) + metric.add_metric(labels=[self._node_id, self._hostname], + value=self.collect_metric(metric_type=metadata['type'], metric_key=statistic)) + yield metric + + if metadata['type'] == MetricType.INFO: + metric = InfoMetricFamily(name=name, + documentation=metadata['description'], + labels=["node", "host"]) + metric.add_metric(labels=[self._node_id, self._hostname], + value={ + statistic: self.collect_metric(metric_type=metadata['type'], metric_key=statistic) + }) + yield metric + except KeyError as e: + logging.error("Key do not exists: %s.", e.args[0]) def metric_name(self, metric_key: str): return "{}_{}".format(self._prefix, metric_key.replace('.', '_'))