From be1e7bd758e6ebc60fc161829a049480c8e71a25 Mon Sep 17 00:00:00 2001 From: Stan Rudenko Date: Fri, 18 Aug 2023 18:10:25 -0700 Subject: [PATCH] agent mode support --- roles/prometheus/defaults/main.yml | 5 +++++ roles/prometheus/meta/argument_specs.yml | 10 ++++++++++ roles/prometheus/templates/prometheus.service.j2 | 9 +++++++-- roles/prometheus/templates/prometheus.yml.j2 | 2 ++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/roles/prometheus/defaults/main.yml b/roles/prometheus/defaults/main.yml index 72a85c7e6..930d9139f 100644 --- a/roles/prometheus/defaults/main.yml +++ b/roles/prometheus/defaults/main.yml @@ -25,6 +25,9 @@ prometheus_storage_retention: "30d" # supported: KB, MB, GB, TB, PB. prometheus_storage_retention_size: "0" +# The Agent mode optimizes Prometheus for the remote write use case: https://prometheus.io/blog/2021/11/16/agent/ +prometheus_agent_mode: false + prometheus_config_flags_extra: {} # prometheus_config_flags_extra: # storage.tsdb.retention: 15d @@ -87,6 +90,8 @@ prometheus_scrape_configs: # Alternative config file name, searched in ansible templates path. prometheus_config_file: 'prometheus.yml.j2' +prometheus_rules_enable: true + prometheus_alert_rules_files: - prometheus/rules/*.rules diff --git a/roles/prometheus/meta/argument_specs.yml b/roles/prometheus/meta/argument_specs.yml index 663afa30f..a791711e7 100644 --- a/roles/prometheus/meta/argument_specs.yml +++ b/roles/prometheus/meta/argument_specs.yml @@ -59,6 +59,12 @@ argument_specs: - "Maximum number of bytes that can be stored for blocks." - "Units supported: KB, MB, GB, TB, PB." default: "0" + prometheus_agent_mode: + description: + - "The Agent mode optimizes Prometheus for the remote write use case. It disables querying, alerting, and local storage, and replaces it with a customized TSDB WAL. L(Everything,https://prometheus.io/docs/prometheus/latest/feature_flags/#prometheus-agent) else stays the same." + - "This feature is available starting from Prometheus v2.32.0." + type: bool + default: false prometheus_config_flags_extra: description: - "Additional configuration flags passed to prometheus binary at startup" @@ -121,6 +127,10 @@ argument_specs: prometheus_config_file: description: "Variable used to provide custom prometheus configuration file in form of ansible template" default: "prometheus.yml.j2" + prometheus_rules_enable: + description: "To enable rules via the rule_files field in the Prometheus configuration" + type: bool + default: true prometheus_alert_rules: description: - "Full list of alerting rules which will be copied to C({{ prometheus_config_dir }}/rules/ansible_managed.rules)." diff --git a/roles/prometheus/templates/prometheus.service.j2 b/roles/prometheus/templates/prometheus.service.j2 index 7d2973af8..bbab7f8d7 100644 --- a/roles/prometheus/templates/prometheus.service.j2 +++ b/roles/prometheus/templates/prometheus.service.j2 @@ -13,12 +13,16 @@ User=prometheus Group=prometheus ExecReload=/bin/kill -HUP $MAINPID ExecStart={{ _prometheus_binary_install_dir }}/prometheus \ +{% if not prometheus_agent_mode %} --storage.tsdb.path={{ prometheus_db_dir }} \ -{% if prometheus_version is version('2.7.0', '>=') %} + {% if prometheus_version is version('2.7.0', '>=') %} --storage.tsdb.retention.time={{ prometheus_storage_retention }} \ --storage.tsdb.retention.size={{ prometheus_storage_retention_size }} \ -{% else %} + {% else %} --storage.tsdb.retention={{ prometheus_storage_retention }} \ + {% endif %} +{% else %} + --enable-feature=agent \ {% endif %} {% if prometheus_version is version('2.24.0', '>=') %} --web.config.file={{ prometheus_config_dir }}/web.yml \ @@ -54,6 +58,7 @@ RestrictSUIDSGID=true {% if (ansible_facts.packages.systemd | first).version is version('231', '>=') %} ReadWritePaths={{ prometheus_db_dir }} +WorkingDirectory={{ prometheus_db_dir }} {% for path in prometheus_read_only_dirs %} ReadOnlyPaths={{ path }} {% endfor %} diff --git a/roles/prometheus/templates/prometheus.yml.j2 b/roles/prometheus/templates/prometheus.yml.j2 index a6ed81284..97d6eea1e 100644 --- a/roles/prometheus/templates/prometheus.yml.j2 +++ b/roles/prometheus/templates/prometheus.yml.j2 @@ -17,8 +17,10 @@ remote_read: {{ prometheus_remote_read | to_nice_yaml(indent=2,sort_keys=False) | indent(2, False) }} {% endif %} +{% if prometheus_rules_enable and not prometheus_agent_mode %} rule_files: - {{ prometheus_config_dir }}/rules/*.rules +{% endif %} {% if prometheus_alertmanager_config | length > 0 %} alerting: