diff --git a/mqtt_exporter.py b/mqtt_exporter.py index e5be9ef..f2b071f 100755 --- a/mqtt_exporter.py +++ b/mqtt_exporter.py @@ -349,10 +349,10 @@ def _mqtt_init(mqtt_config, metrics): def _export_to_prometheus(name, metric, labels): """Export metric and labels to prometheus.""" metric_wrappers = {'gauge': GaugeWrapper, - 'counter': CounterWrapper, - 'counter_absolute': CounterAbsoluteWrapper, - 'summary': SummaryWrapper, - 'histogram': HistogramWrapper} + 'counter': CounterWrapper, + 'counter_absolute': CounterAbsoluteWrapper, + 'summary': SummaryWrapper, + 'histogram': HistogramWrapper} valid_types = metric_wrappers.keys() if metric['type'] not in valid_types: logging.error( @@ -366,14 +366,14 @@ def _export_to_prometheus(name, metric, labels): sorted_labels = _get_sorted_tuple_list(labels) label_names, label_values = list(zip(*sorted_labels)) - prometheus_metric = None if not metric.get('prometheus_metric') or not metric['prometheus_metric'].get('parent'): # parent metric not seen before, create metric additional_parameters = metric.get('parameters', {}) - metric_wrapper = metric_wrappers[metric['type']] - prometheus_metric = metric_wrapper(metric['name'], metric['help'], label_names, **additional_parameters) + metric_wrapper = metric_wrappers[metric['type']] + prometheus_metric = metric_wrapper( + metric['name'], metric['help'], label_names, **additional_parameters) metric['prometheus_metric'] = {} metric['prometheus_metric']['parent'] = prometheus_metric else: @@ -386,6 +386,7 @@ def _export_to_prometheus(name, metric, labels): if logging.DEBUG >= logging.root.level: # log test data only in debugging mode _log_test_data(metric, labels['topic'], value) + def _log_test_data(metric, topic, value): try: base_metric = metric['prometheus_metric']['parent'].metric.collect() @@ -420,6 +421,7 @@ class GaugeWrapper(): """ Wrapper to provide generic interface to Gauge metric """ + def __init__(self, name, help_text, label_names, *args, **kwargs) -> None: self.metric = prometheus.Gauge( name, help_text, list(label_names) @@ -430,10 +432,12 @@ def update(self, label_values, value): child.set(value) return child + class CounterWrapper(): """ Wrapper to provide generic interface to Counter metric """ + def __init__(self, name, help_text, label_names, *args, **kwargs) -> None: self.metric = prometheus.Counter( name, help_text, list(label_names) @@ -444,6 +448,7 @@ def update(self, label_values, value): child.inc(value) return child + class CounterAbsoluteWrapper(): """ Wrapper to provide generic interface to CounterAbsolute metric @@ -459,6 +464,7 @@ def update(self, label_values, value): child.set(value) return child + class SummaryWrapper(): """ Wrapper to provide generic interface to Summary metric @@ -474,6 +480,7 @@ def update(self, label_values, value): child.observe(value) return child + class HistogramWrapper(): """ Wrapper to provide generic interface to Summary metric