nixos/prometheus-json-exporter: update modules & tests, add release notes

This commit is contained in:
WilliButz
2021-01-02 13:10:27 +01:00
parent d539517f2b
commit 3f94c66ee1
5 changed files with 120 additions and 75 deletions
@@ -295,6 +295,32 @@
Based on <xref linkend="opt-system.stateVersion" />, existing installations will continue to work. Based on <xref linkend="opt-system.stateVersion" />, existing installations will continue to work.
</para> </para>
</listitem> </listitem>
<listitem>
<para>
The prometheus json exporter is now managed by the prometheus community. Together with additional features
some backwards incompatibilities were introduced.
Most importantly the exporter no longer accepts a fixed command-line parameter to specify the URL of the
endpoint serving JSON. It now expects this URL to be passed as an URL parameter, when scraping the exporter's
<literal>/probe</literal> endpoint.
In the prometheus scrape configuration the scrape target might look like this:
<programlisting>
http://some.json-exporter.host:7979/probe?target=https://example.com/some/json/endpoint
</programlisting>
</para>
<para>
Existing configuration for the exporter needs to be updated, but can partially be re-used.
Documentation is available in the upstream repository and a small example for NixOS is available
in the corresponding NixOS test.
</para>
<para>
These changes also affect <literal>services.prometheus.exporters.rspamd</literal>, which is
just a preconfigured instance of the json exporter.
</para>
<para>
For more information, take a look at the <link xlink:href="https://github.com/prometheus-community/json_exporter">
official documentation</link> of the json_exporter.
</para>
</listitem>
</itemizedlist> </itemizedlist>
</section> </section>
@@ -236,8 +236,6 @@ in
services.prometheus.exporters.minio.minioAddress = mkDefault "http://localhost:9000"; services.prometheus.exporters.minio.minioAddress = mkDefault "http://localhost:9000";
services.prometheus.exporters.minio.minioAccessKey = mkDefault config.services.minio.accessKey; services.prometheus.exporters.minio.minioAccessKey = mkDefault config.services.minio.accessKey;
services.prometheus.exporters.minio.minioAccessSecret = mkDefault config.services.minio.secretKey; services.prometheus.exporters.minio.minioAccessSecret = mkDefault config.services.minio.secretKey;
})] ++ [(mkIf config.services.rspamd.enable {
services.prometheus.exporters.rspamd.url = mkDefault "http://localhost:11334/stat";
})] ++ [(mkIf config.services.prometheus.exporters.rtl_433.enable { })] ++ [(mkIf config.services.prometheus.exporters.rtl_433.enable {
hardware.rtl-sdr.enable = mkDefault true; hardware.rtl-sdr.enable = mkDefault true;
})] ++ [(mkIf config.services.nginx.enable { })] ++ [(mkIf config.services.nginx.enable {
@@ -4,32 +4,42 @@ with lib;
let let
cfg = config.services.prometheus.exporters.json; cfg = config.services.prometheus.exporters.json;
in in
{ {
port = 7979; port = 7979;
extraOpts = { extraOpts = {
url = mkOption {
type = types.str;
description = ''
URL to scrape JSON from.
'';
};
configFile = mkOption { configFile = mkOption {
type = types.path; type = types.path;
description = '' description = ''
Path to configuration file. Path to configuration file.
''; '';
}; };
listenAddress = {}; # not used
}; };
serviceOpts = { serviceOpts = {
serviceConfig = { serviceConfig = {
ExecStart = '' ExecStart = ''
${pkgs.prometheus-json-exporter}/bin/prometheus-json-exporter \ ${pkgs.prometheus-json-exporter}/bin/json_exporter \
--port ${toString cfg.port} \ --config.file ${escapeShellArg cfg.configFile} \
${cfg.url} ${escapeShellArg cfg.configFile} \ --web.listen-address="${cfg.listenAddress}:${toString cfg.port}" \
${concatStringsSep " \\\n " cfg.extraFlags} ${concatStringsSep " \\\n " cfg.extraFlags}
''; '';
}; };
}; };
imports = [
(mkRemovedOptionModule [ "url" ] ''
This option was removed. The URL of the endpoint serving JSON
must now be provided to the exporter by prometheus via the url
parameter <literal>target</literal>.
In prometheus a scrape URL would look like this:
<programlisting>
http://some.json-exporter.host:7979/probe?target=https://example.com/some/json/endpoint
</programlisting>
For more information, take a look at the <link xlink:href="https://github.com/prometheus-community/json_exporter">
official documentation</link> of the json_exporter.
'')
({ options.warnings = options.warnings; options.assertions = options.assertions; })
];
} }
@@ -10,64 +10,55 @@ let
echo '${builtins.toJSON conf}' | ${pkgs.buildPackages.jq}/bin/jq '.' > $out echo '${builtins.toJSON conf}' | ${pkgs.buildPackages.jq}/bin/jq '.' > $out
''; '';
generateConfig = extraLabels: (map (path: { generateConfig = extraLabels: {
name = "rspamd_${replaceStrings [ "." " " ] [ "_" "_" ] path}"; metrics = (map (path: {
path = "$.${path}"; name = "rspamd_${replaceStrings [ "." " " ] [ "_" "_" ] path}";
labels = extraLabels; path = "$.${path}";
}) [ labels = extraLabels;
"actions.'add header'" }) [
"actions.'no action'" "actions.'add header'"
"actions.'rewrite subject'" "actions.'no action'"
"actions.'soft reject'" "actions.'rewrite subject'"
"actions.greylist" "actions.'soft reject'"
"actions.reject" "actions.greylist"
"bytes_allocated" "actions.reject"
"chunks_allocated" "bytes_allocated"
"chunks_freed" "chunks_allocated"
"chunks_oversized" "chunks_freed"
"connections" "chunks_oversized"
"control_connections" "connections"
"ham_count" "control_connections"
"learned" "ham_count"
"pools_allocated" "learned"
"pools_freed" "pools_allocated"
"read_only" "pools_freed"
"scanned" "read_only"
"shared_chunks_allocated" "scanned"
"spam_count" "shared_chunks_allocated"
"total_learns" "spam_count"
]) ++ [{ "total_learns"
name = "rspamd_statfiles"; ]) ++ [{
type = "object"; name = "rspamd_statfiles";
path = "$.statfiles[*]"; type = "object";
labels = recursiveUpdate { path = "$.statfiles[*]";
symbol = "$.symbol"; labels = recursiveUpdate {
type = "$.type"; symbol = "$.symbol";
} extraLabels; type = "$.type";
values = { } extraLabels;
revision = "$.revision"; values = {
size = "$.size"; revision = "$.revision";
total = "$.total"; size = "$.size";
used = "$.used"; total = "$.total";
languages = "$.languages"; used = "$.used";
users = "$.users"; languages = "$.languages";
}; users = "$.users";
}]; };
}];
};
in in
{ {
port = 7980; port = 7980;
extraOpts = { extraOpts = {
listenAddress = {}; # not used
url = mkOption {
type = types.str;
description = ''
URL to the rspamd metrics endpoint.
Defaults to http://localhost:11334/stat when
<option>services.rspamd.enable</option> is true.
'';
};
extraLabels = mkOption { extraLabels = mkOption {
type = types.attrsOf types.str; type = types.attrsOf types.str;
default = { default = {
@@ -84,9 +75,26 @@ in
}; };
}; };
serviceOpts.serviceConfig.ExecStart = '' serviceOpts.serviceConfig.ExecStart = ''
${pkgs.prometheus-json-exporter}/bin/prometheus-json-exporter \ ${pkgs.prometheus-json-exporter}/bin/json_exporter \
--port ${toString cfg.port} \ --config.file ${prettyJSON (generateConfig cfg.extraLabels)} \
${cfg.url} ${prettyJSON (generateConfig cfg.extraLabels)} \ --web.listen-address "${cfg.listenAddress}:${toString cfg.port}" \
${concatStringsSep " \\\n " cfg.extraFlags} ${concatStringsSep " \\\n " cfg.extraFlags}
''; '';
imports = [
(mkRemovedOptionModule [ "url" ] ''
This option was removed. The URL of the rspamd metrics endpoint
must now be provided to the exporter by prometheus via the url
parameter <literal>target</literal>.
In prometheus a scrape URL would look like this:
<programlisting>
http://some.rspamd-exporter.host:7980/probe?target=http://some.rspamd.host:11334/stat
</programlisting>
For more information, take a look at the <link xlink:href="https://github.com/prometheus-community/json_exporter">
official documentation</link> of the json_exporter.
'')
({ options.warnings = options.warnings; options.assertions = options.assertions; })
];
} }
+9 -6
View File
@@ -222,10 +222,11 @@ let
exporterConfig = { exporterConfig = {
enable = true; enable = true;
url = "http://localhost"; url = "http://localhost";
configFile = pkgs.writeText "json-exporter-conf.json" (builtins.toJSON [{ configFile = pkgs.writeText "json-exporter-conf.json" (builtins.toJSON {
name = "json_test_metric"; metrics = [
path = "$.test"; { name = "json_test_metric"; path = "$.test"; }
}]); ];
});
}; };
metricProvider = { metricProvider = {
systemd.services.prometheus-json-exporter.after = [ "nginx.service" ]; systemd.services.prometheus-json-exporter.after = [ "nginx.service" ];
@@ -241,7 +242,9 @@ let
wait_for_open_port(80) wait_for_open_port(80)
wait_for_unit("prometheus-json-exporter.service") wait_for_unit("prometheus-json-exporter.service")
wait_for_open_port(7979) wait_for_open_port(7979)
succeed("curl -sSf localhost:7979/metrics | grep -q 'json_test_metric 1'") succeed(
"curl -sSf 'localhost:7979/probe?target=http://localhost' | grep -q 'json_test_metric 1'"
)
''; '';
}; };
@@ -659,7 +662,7 @@ let
wait_for_open_port(11334) wait_for_open_port(11334)
wait_for_open_port(7980) wait_for_open_port(7980)
wait_until_succeeds( wait_until_succeeds(
"curl -sSf localhost:7980/metrics | grep -q 'rspamd_scanned{host=\"rspamd\"} 0'" "curl -sSf 'localhost:7980/probe?target=http://localhost:11334/stat' | grep -q 'rspamd_scanned{host=\"rspamd\"} 0'"
) )
''; '';
}; };