Skip to content

Commit

Permalink
🐛 Handle missing metrics from metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelgervais committed May 23, 2019
1 parent a52cb01 commit 3fa55e4
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions qdb_prometheus_exporter/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('.', '_'))
Expand Down

0 comments on commit 3fa55e4

Please sign in to comment.