Skip to content

Commit

Permalink
Merge pull request #333 from dpavle/main
Browse files Browse the repository at this point in the history
enhancement(prometheus): support prometheus2 .yml rule file format
  • Loading branch information
gardar authored Oct 31, 2024
2 parents 2f9a30f + e04558d commit e4384eb
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion roles/prometheus/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ prometheus_scrape_configs:
prometheus_config_file: 'prometheus.yml.j2'

prometheus_alert_rules_files:
- prometheus/rules/*.rules
- prometheus/rules/*.yml
- prometheus/rules/*.yaml

prometheus_static_targets_files:
- prometheus/targets/*.yml
Expand Down
9 changes: 5 additions & 4 deletions roles/prometheus/meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,20 @@ argument_specs:
default: "prometheus.yml.j2"
prometheus_alert_rules:
description:
- "Full list of alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/ansible_managed.rules)."
- "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules/) which have C(*.rules) extension"
- "Full list of alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/ansible_managed.yml)."
- "Alerting rules can be also provided by other files located in C({{ prometheus_config_dir }}/rules/) which have a C(*.yml) or C(*.yaml) extension"
- "Please see default values in role defaults/main.yml"
type: "list"
elements: "dict"
prometheus_alert_rules_files:
description:
- "List of folders where ansible will look for files containing alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/)."
- "Files must have C(*.rules) extension"
- "Files must have a C(*.yml) or C(*.yaml) extension"
type: "list"
elements: "str"
default:
- "prometheus/rules/*.rules"
- "prometheus/rules/*.yml"
- "prometheus/rules/*.yaml"
prometheus_static_targets_files:
description:
- "List of folders where ansible will look for files containing custom static target configuration files which will be copied to C({{ prometheus_config_dir }}/file_sd/)."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_directories(host, dirs):
"files",
[
"/opt/prom/etc/prometheus.yml",
"/opt/prom/etc/rules/ansible_managed.rules",
"/opt/prom/etc/rules/ansible_managed.yml",
"/opt/prom/etc/file_sd/node.yml",
"/opt/prom/etc/file_sd/docker.yml",
"/opt/prom/etc/scrape_configs/empty_scrapes.yml",
Expand Down
2 changes: 1 addition & 1 deletion roles/prometheus/molecule/default/tests/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_files(host, files):


@pytest.mark.parametrize("files", [
"/etc/prometheus/rules/ansible_managed.rules"
"/etc/prometheus/rules/ansible_managed.yml"
])
def test_absent(host, files):
f = host.file(files)
Expand Down
4 changes: 2 additions & 2 deletions roles/prometheus/tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@

- name: Alerting rules file
ansible.builtin.template:
src: "alert.rules.j2"
dest: "{{ prometheus_config_dir }}/rules/ansible_managed.rules"
src: "ansible_managed.yml.j2"
dest: "{{ prometheus_config_dir }}/rules/ansible_managed.yml"
owner: "{{ prometheus_system_user }}"
group: "{{ prometheus_system_group }}"
mode: 0640
Expand Down
11 changes: 11 additions & 0 deletions roles/prometheus/tasks/preflight.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@
- prometheus_alertmanager_config == []
- prometheus_alert_rules != []

- name: Alert when alert rules files are found in the old .rules format
ansible.builtin.debug:
msg: >
prometheus_alert_rules_files contains rules in the old .rules file format.
This format has been deprecated in favour of the new YAML format since Prometheus v2.x.
You can update your rules using promtool: promtool update rules <filenames>
If your rules are already in the YAML format but with the .rules extension, you can safely ignore this message,
or rename the files to <filename>.yaml or <filename>.yml to get rid of it.
when: prometheus_alert_rules_files | select('search', '.rules$') | list | length > 0

- name: Discover latest version
ansible.builtin.set_fact:
prometheus_version: "{{ (lookup('url', 'https://api.github.com/repos/{{ _prometheus_repo}}/releases/latest', headers=_github_api_headers,
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions roles/prometheus/templates/prometheus.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ remote_read:

{% if not prometheus_agent_mode and prometheus_alert_rules_files != [] %}
rule_files:
- {{ prometheus_config_dir }}/rules/*.yml
- {{ prometheus_config_dir }}/rules/*.yaml
- {{ prometheus_config_dir }}/rules/*.rules
{% endif %}

Expand Down

0 comments on commit e4384eb

Please sign in to comment.