1{
2 config,
3 lib,
4 pkgs,
5 options,
6 ...
7}:
8
9let
10 cfg = config.services.prometheus.exporters.mongodb;
11 inherit (lib)
12 mkOption
13 types
14 optionalString
15 getExe
16 length
17 concatStringsSep
18 concatMapStringsSep
19 escapeShellArgs
20 ;
21in
22{
23 port = 9216;
24 extraOpts = {
25 uri = mkOption {
26 type = types.str;
27 default = "mongodb://localhost:27017/test";
28 example = "mongodb://localhost:27017/test";
29 description = "MongoDB URI to connect to.";
30 };
31 collStats = mkOption {
32 type = types.listOf types.str;
33 default = [ ];
34 example = [
35 "db1.coll1"
36 "db2"
37 ];
38 description = ''
39 List of comma separared databases.collections to get $collStats
40 '';
41 };
42 indexStats = mkOption {
43 type = types.listOf types.str;
44 default = [ ];
45 example = [
46 "db1.coll1"
47 "db2"
48 ];
49 description = ''
50 List of comma separared databases.collections to get $indexStats
51 '';
52 };
53 collector = mkOption {
54 type = types.listOf types.str;
55 default = [ ];
56 example = [
57 "diagnosticdata"
58 "replicasetstatus"
59 "dbstats"
60 "topmetrics"
61 "currentopmetrics"
62 "indexstats"
63 "dbstats"
64 "profile"
65 ];
66 description = "Enabled collectors";
67 };
68 collectAll = mkOption {
69 type = types.bool;
70 default = false;
71 description = ''
72 Enable all collectors. Same as specifying all --collector.<name>
73 '';
74 };
75 telemetryPath = mkOption {
76 type = types.str;
77 default = "/metrics";
78 example = "/metrics";
79 description = "Metrics expose path";
80 };
81 };
82 serviceOpts = {
83 serviceConfig = {
84 RuntimeDirectory = "prometheus-mongodb-exporter";
85 ExecStart = ''
86 ${getExe pkgs.prometheus-mongodb-exporter} \
87 --mongodb.uri="${cfg.uri}" \
88 ${
89 if cfg.collectAll then
90 "--collect-all"
91 else
92 concatMapStringsSep " " (x: "--collect.${x}") cfg.collector
93 } \
94 ${
95 optionalString (
96 length cfg.collStats > 0
97 ) "--mongodb.collstats-colls=${concatStringsSep "," cfg.collStats}"
98 } \
99 ${
100 optionalString (
101 length cfg.indexStats > 0
102 ) "--mongodb.indexstats-colls=${concatStringsSep "," cfg.indexStats}"
103 } \
104 --web.listen-address="${cfg.listenAddress}:${toString cfg.port}" \
105 --web.telemetry-path="${cfg.telemetryPath}" \
106 ${escapeShellArgs cfg.extraFlags}
107 '';
108 };
109 };
110}