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