Skip to content

Commit

Permalink
Fix duplicated timeseries in CollectorRegistry for derived metrics (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: poggenpower <[email protected]>
  • Loading branch information
poggenpower and poggenpower authored Aug 2, 2021
1 parent 47838c6 commit 24ba7c6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions mqtt_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,14 @@ def _update_metrics(metrics, msg):

labels = finalize_labels(labels)

# Add derived metric for when the message was last received (timestamp in milliseconds)
derived_metric = {
'name': f"{metric['name']}_last_received",
'help': f"Last received message for '{metric['name']}'",
'type': 'gauge'
}
derived_metric = metric.setdefault('derived_metric',
# Add derived metric for when the message was last received (timestamp in milliseconds)
{
'name': f"{metric['name']}_last_received",
'help': f"Last received message for '{metric['name']}'",
'type': 'gauge'
}
)
derived_labels = {'topic': metric['topic'],
'value': int(round(time.time() * 1000))}

Expand Down Expand Up @@ -365,7 +367,7 @@ def gauge(label_names, label_values, metric, name, value):

def get_prometheus_metric(label_names, label_values, metric, name, buckets=None):
key = ':'.join([''.join(label_names), ''.join(label_values)])
if 'prometheus_metric' not in metric or not metric['prometheus_metric']:
if not metric.get('prometheus_metric') or not metric['prometheus_metric'].get('base'):
metric['prometheus_metric'] = {}
prometheus_metric_types = {'gauge': prometheus.Gauge,
'counter': prometheus.Counter,
Expand All @@ -375,7 +377,7 @@ def get_prometheus_metric(label_names, label_values, metric, name, buckets=None)
metric_type = metric['type'].lower()
if metric_type == 'histogram' and buckets:
metric['prometheus_metric']['base'] = prometheus_metric_types[metric_type](name, metric['help'],
list(label_names), buckets)
list(label_names), buckets)
else:
metric['prometheus_metric']['base'] = prometheus_metric_types[metric_type](name, metric['help'],
list(label_names))
Expand Down

0 comments on commit 24ba7c6

Please sign in to comment.