1{ config, lib, pkgs, options }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.exporters.varnish;
7in
8{
9 port = 9131;
10 extraOpts = {
11 noExit = mkOption {
12 type = types.bool;
13 default = false;
14 description = lib.mdDoc ''
15 Do not exit server on Varnish scrape errors.
16 '';
17 };
18 withGoMetrics = mkOption {
19 type = types.bool;
20 default = false;
21 description = lib.mdDoc ''
22 Export go runtime and http handler metrics.
23 '';
24 };
25 verbose = mkOption {
26 type = types.bool;
27 default = false;
28 description = lib.mdDoc ''
29 Enable verbose logging.
30 '';
31 };
32 raw = mkOption {
33 type = types.bool;
34 default = false;
35 description = lib.mdDoc ''
36 Enable raw stdout logging without timestamps.
37 '';
38 };
39 varnishStatPath = mkOption {
40 type = types.str;
41 default = "varnishstat";
42 description = lib.mdDoc ''
43 Path to varnishstat.
44 '';
45 };
46 instance = mkOption {
47 type = types.nullOr types.str;
48 default = config.services.varnish.stateDir;
49 defaultText = lib.literalExpression "config.services.varnish.stateDir";
50 description = lib.mdDoc ''
51 varnishstat -n value.
52 '';
53 };
54 healthPath = mkOption {
55 type = types.nullOr types.str;
56 default = null;
57 description = lib.mdDoc ''
58 Path under which to expose healthcheck. Disabled unless configured.
59 '';
60 };
61 telemetryPath = mkOption {
62 type = types.str;
63 default = "/metrics";
64 description = lib.mdDoc ''
65 Path under which to expose metrics.
66 '';
67 };
68 };
69 serviceOpts = {
70 path = [ config.services.varnish.package ];
71 serviceConfig = {
72 RestartSec = mkDefault 1;
73 DynamicUser = false;
74 ExecStart = ''
75 ${pkgs.prometheus-varnish-exporter}/bin/prometheus_varnish_exporter \
76 --web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
77 --web.telemetry-path ${cfg.telemetryPath} \
78 --varnishstat-path ${escapeShellArg cfg.varnishStatPath} \
79 ${concatStringsSep " \\\n " (cfg.extraFlags
80 ++ optional (cfg.healthPath != null) "--web.health-path ${cfg.healthPath}"
81 ++ optional (cfg.instance != null) "-n ${escapeShellArg cfg.instance}"
82 ++ optional cfg.noExit "--no-exit"
83 ++ optional cfg.withGoMetrics "--with-go-metrics"
84 ++ optional cfg.verbose "--verbose"
85 ++ optional cfg.raw "--raw")}
86 '';
87 };
88 };
89}