Skip to content

Commit

Permalink
fix: crash when metric_name contains invalid chars
Browse files Browse the repository at this point in the history
Prometheus metric name must match the regex [a-zA-Z_:][a-zA-Z0-9_:]*
Before this commit, an invalid metric name was causing a crash.

https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
  • Loading branch information
kpetremann committed Jun 25, 2023
1 parent 068c59e commit 83424af
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions mqtt_exporter/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ def _create_prometheus_metric(prom_metric_name):
if settings.MQTT_EXPOSE_CLIENT_ID:
labels.append("client_id")

prom_metrics[prom_metric_name] = Gauge(
prom_metric_name, "metric generated from MQTT message.", labels
)
LOG.info("creating prometheus metric: %s", prom_metric_name)
try:
prom_metrics[prom_metric_name] = Gauge(
prom_metric_name, "metric generated from MQTT message.", labels
)
LOG.info("creating prometheus metric: %s", prom_metric_name)
except ValueError as error:
LOG.error("unable to create prometheus metric '%s': %s", prom_metric_name, error)


def _add_prometheus_sample(topic, prom_metric_name, metric_value, client_id):
Expand Down

0 comments on commit 83424af

Please sign in to comment.