1 import ../make-test-python.nix ({ lib, pkgs, ... }:
4 name = "prometheus-config-reload";
7 prometheus = { config, pkgs, ... }: {
8 environment.systemPackages = [ pkgs.jq ];
10 networking.firewall.allowedTCPPorts = [ config.services.prometheus.port ];
12 services.prometheus = {
15 globalConfig.scrape_interval = "2s";
18 job_name = "prometheus";
22 "prometheus:${toString config.services.prometheus.port}"
31 "prometheus-config-change" = {
33 environment.systemPackages = [ pkgs.yq ];
35 # This configuration just adds a new prometheus job
36 # to scrape the node_exporter metrics of the s3 machine.
37 services.prometheus = {
43 targets = [ "node:${toString config.services.prometheus.exporters.node.port}" ];
56 prometheus.wait_for_unit("prometheus")
57 prometheus.wait_for_open_port(9090)
59 # Check if switching to a NixOS configuration that changes the prometheus
60 # configuration reloads (instead of restarts) prometheus before the switch
61 # finishes successfully:
62 with subtest("config change reloads prometheus"):
64 # We check if prometheus has finished reloading by looking for the message
65 # "Completed loading of configuration file" in the journal between the start
66 # and finish of switching to the new NixOS configuration.
68 # To mark the start we record the journal cursor before starting the switch:
69 cursor_before_switching = json.loads(
70 prometheus.succeed("journalctl -n1 -o json --output-fields=__CURSOR")
74 prometheus_config_change = prometheus.succeed(
75 "readlink /run/current-system/specialisation/prometheus-config-change"
77 prometheus.succeed(prometheus_config_change + "/bin/switch-to-configuration test")
79 # Next we retrieve all logs since the start of switching:
80 logs_after_starting_switching = prometheus.succeed(
82 journalctl --after-cursor='{cursor_before_switching}' -o json --output-fields=MESSAGE
84 cursor_before_switching=cursor_before_switching
88 # Finally we check if the message "Completed loading of configuration file"
89 # occurs before the "finished switching to system configuration" message:
90 finished_switching_msg = (
91 "finished switching to system configuration " + prometheus_config_change
93 reloaded_before_switching_finished = False
94 finished_switching = False
95 for log_line in logs_after_starting_switching.split("\n"):
96 msg = json.loads(log_line)["MESSAGE"]
97 if "Completed loading of configuration file" in msg:
98 reloaded_before_switching_finished = True
99 if msg == finished_switching_msg:
100 finished_switching = True
103 assert reloaded_before_switching_finished
104 assert finished_switching
106 # Check if the reloaded config includes the new node job:
109 curl -sf http://127.0.0.1:9090/api/v1/status/config \
111 | yq '.scrape_configs | any(.job_name == "node")' \