canonical / prometheus-k8s-operator

https://charmhub.io/prometheus-k8s
Apache License 2.0
21 stars 35 forks source link

Empty alert rules files errors out #364

Closed sabaini closed 2 years ago

sabaini commented 2 years ago

Bug Description

The AlertRules._from_file() method errors out when adding an empty (zero-size) alert rule file, see below for traceback

To Reproduce

  1. Deploy a client of the prometheus_scrape library
  2. Create an empty alert.rules file: touch alert.rules
  3. Trigger reloading alert rule parsing

Environment

Platform-indep.

Relevant log output

Traceback (most recent call last):
  File "/home/peter/src/cos-lite/prometheus-k8s-operator/tests/unit/test_prometheus_rules_provider.py", line 161, in test_reload_with_empty_rules
    self.harness.charm.rules_provider._reinitialize_alert_rules()
  File "/home/peter/src/cos-lite/prometheus-k8s-operator/lib/charms/prometheus_k8s/v0/prometheus_scrape.py", line 1684, in _reinitialize_alert_rules
    self._update_relation_data(None)
  File "/home/peter/src/cos-lite/prometheus-k8s-operator/lib/charms/prometheus_k8s/v0/prometheus_scrape.py", line 1692, in _update_relation_data
    alert_rules.add_path(self.dir_path, recursive=self._recursive)
  File "/home/peter/src/cos-lite/prometheus-k8s-operator/lib/charms/prometheus_k8s/v0/prometheus_scrape.py", line 762, in add_path
    self.alert_groups.extend(self._from_dir(path, recursive))
  File "/home/peter/src/cos-lite/prometheus-k8s-operator/lib/charms/prometheus_k8s/v0/prometheus_scrape.py", line 740, in _from_dir
    alert_groups_from_file = self._from_file(dir_path, file_path)
  File "/home/peter/src/cos-lite/prometheus-k8s-operator/lib/charms/prometheus_k8s/v0/prometheus_scrape.py", line 644, in _from_file
    if _is_official_alert_rule_format(rule_file):
  File "/home/peter/src/cos-lite/prometheus-k8s-operator/lib/charms/prometheus_k8s/v0/prometheus_scrape.py", line 563, in _is_official_alert_rule_format
    return "groups" in rules_dict
TypeError: argument of type 'NoneType' is not iterable

Additional context

No response