git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #60500 from basvandijk/thanos-init

thanos: init at 0.6.0 & NixOS module

commit9ff408a2a4cb784160afef9a43c73a7a37ba38c9 authorBas van Dijk <v.dijk.bas@gmail.com> dateSun 28 Jul 2019, 19:14 (7 years ago) parentd3433f70a59be7
6 files changed+1033−21
nixos/modules/module-list.nix+1 nixos/modules/services/monitoring/thanos.nix+801 nixos/tests/prometheus-2.nix+190−18 pkgs/development/go-modules/generic/default.nix+6−3 pkgs/servers/monitoring/thanos/default.nix+33 pkgs/top-level/all-packages.nix+2
nixos/modules/module-list.nix+1
@@ -519,6 +519,7 @@
519519 ./services/monitoring/systemhealth.nix
520520 ./services/monitoring/teamviewer.nix
521521 ./services/monitoring/telegraf.nix
522+ ./services/monitoring/thanos.nix
522523 ./services/monitoring/ups.nix
523524 ./services/monitoring/uptime.nix
524525 ./services/monitoring/vnstat.nix
nixos/modules/services/monitoring/thanos.nixnew file+801
@@ -0,0 +1,801 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.thanos;
7+
8+ nullOpt = type: description: mkOption {
9+ type = types.nullOr type;
10+ default = null;
11+ inherit description;
12+ };
13+
14+ optionToArgs = opt: v : optional (v != null) ''--${opt}="${toString v}"'';
15+ flagToArgs = opt: v : optional v ''--${opt}'';
16+ listToArgs = opt: vs : map (v: ''--${opt}="${v}"'') vs;
17+ attrsToArgs = opt: kvs: mapAttrsToList (k: v: ''--${opt}=${k}=\"${v}\"'') kvs;
18+
19+ mkParamDef = type: default: description: mkParam type (description + ''
20+
21+ Defaults to <literal>${toString default}</literal> in Thanos
22+ when set to <literal>null</literal>.
23+ '');
24+
25+ mkParam = type: description: {
26+ toArgs = optionToArgs;
27+ option = nullOpt type description;
28+ };
29+
30+ mkFlagParam = description: {
31+ toArgs = flagToArgs;
32+ option = mkOption {
33+ type = types.bool;
34+ default = false;
35+ inherit description;
36+ };
37+ };
38+
39+ mkListParam = opt: description: {
40+ toArgs = _opt: listToArgs opt;
41+ option = mkOption {
42+ type = types.listOf types.str;
43+ default = [];
44+ inherit description;
45+ };
46+ };
47+
48+ mkAttrsParam = opt: description: {
49+ toArgs = _opt: attrsToArgs opt;
50+ option = mkOption {
51+ type = types.attrsOf types.str;
52+ default = {};
53+ inherit description;
54+ };
55+ };
56+
57+ mkStateDirParam = opt: default: description: {
58+ toArgs = _opt: stateDir: optionToArgs opt "/var/lib/${stateDir}";
59+ option = mkOption {
60+ type = types.str;
61+ inherit default;
62+ inherit description;
63+ };
64+ };
65+
66+ toYAML = name: attrs: pkgs.runCommandNoCC name {
67+ preferLocalBuild = true;
68+ json = builtins.toFile "${name}.json" (builtins.toJSON attrs);
69+ nativeBuildInputs = [ pkgs.remarshal ];
70+ } ''json2yaml -i $json -o $out'';
71+
72+ thanos = cmd: "${cfg.package}/bin/thanos ${cmd}" +
73+ (let args = cfg."${cmd}".arguments;
74+ in optionalString (length args != 0) (" \\\n " +
75+ concatStringsSep " \\\n " args));
76+
77+ argumentsOf = cmd: concatLists (collect isList
78+ (flip mapParamsRecursive params."${cmd}" (path: param:
79+ let opt = concatStringsSep "." path;
80+ v = getAttrFromPath path cfg."${cmd}";
81+ in param.toArgs opt v)));
82+
83+ mkArgumentsOption = cmd: mkOption {
84+ type = types.listOf types.str;
85+ default = argumentsOf cmd;
86+ description = ''
87+ Arguments to the <literal>thanos ${cmd}</literal> command.
88+
89+ Defaults to a list of arguments formed by converting the structured
90+ options of <option>services.thanos.${cmd}</option> to a list of arguments.
91+
92+ Overriding this option will cause none of the structured options to have
93+ any effect. So only set this if you know what you're doing!
94+ '';
95+ };
96+
97+ mapParamsRecursive =
98+ let noParam = attr: !(attr ? "toArgs" && attr ? "option");
99+ in mapAttrsRecursiveCond noParam;
100+
101+ paramsToOptions = mapParamsRecursive (_path: param: param.option);
102+
103+ params = {
104+
105+ log = {
106+
107+ log.level = mkParamDef (types.enum ["debug" "info" "warn" "error" "fatal"]) "info" ''
108+ Log filtering level.
109+ '';
110+
111+ log.format = mkParam types.str ''
112+ Log format to use.
113+ '';
114+ };
115+
116+ tracing = cfg: {
117+ tracing.config-file = {
118+ toArgs = _opt: path: optionToArgs "tracing.config-file" path;
119+ option = mkOption {
120+ type = with types; nullOr str;
121+ default = if cfg.tracing.config == null then null
122+ else toString (toYAML "tracing.yaml" cfg.tracing.config);
123+ defaultText = ''
124+ if config.services.thanos.<cmd>.tracing.config == null then null
125+ else toString (toYAML "tracing.yaml" config.services.thanos.<cmd>.tracing.config);
126+ '';
127+ description = ''
128+ Path to YAML file that contains tracing configuration.
129+ '';
130+ };
131+ };
132+
133+ tracing.config =
134+ {
135+ toArgs = _opt: _attrs: [];
136+ option = nullOpt types.attrs ''
137+ Tracing configuration.
138+
139+ When not <literal>null</literal> the attribute set gets converted to
140+ a YAML file and stored in the Nix store. The option
141+ <option>tracing.config-file</option> will default to its path.
142+
143+ If <option>tracing.config-file</option> is set this option has no effect.
144+ '';
145+ };
146+ };
147+
148+ common = cfg: params.log // params.tracing cfg // {
149+
150+ http-address = mkParamDef types.str "0.0.0.0:10902" ''
151+ Listen <literal>host:port</literal> for HTTP endpoints.
152+ '';
153+
154+ grpc-address = mkParamDef types.str "0.0.0.0:10901" ''
155+ Listen <literal>ip:port</literal> address for gRPC endpoints (StoreAPI).
156+
157+ Make sure this address is routable from other components.
158+ '';
159+
160+ grpc-server-tls-cert = mkParam types.str ''
161+ TLS Certificate for gRPC server, leave blank to disable TLS
162+ '';
163+
164+ grpc-server-tls-key = mkParam types.str ''
165+ TLS Key for the gRPC server, leave blank to disable TLS
166+ '';
167+
168+ grpc-server-tls-client-ca = mkParam types.str ''
169+ TLS CA to verify clients against.
170+
171+ If no client CA is specified, there is no client verification on server side.
172+ (tls.NoClientCert)
173+ '';
174+ };
175+
176+ objstore = cfg: {
177+
178+ objstore.config-file = {
179+ toArgs = _opt: path: optionToArgs "objstore.config-file" path;
180+ option = mkOption {
181+ type = with types; nullOr str;
182+ default = if cfg.objstore.config == null then null
183+ else toString (toYAML "objstore.yaml" cfg.objstore.config);
184+ defaultText = ''
185+ if config.services.thanos.<cmd>.objstore.config == null then null
186+ else toString (toYAML "objstore.yaml" config.services.thanos.<cmd>.objstore.config);
187+ '';
188+ description = ''
189+ Path to YAML file that contains object store configuration.
190+ '';
191+ };
192+ };
193+
194+ objstore.config =
195+ {
196+ toArgs = _opt: _attrs: [];
197+ option = nullOpt types.attrs ''
198+ Object store configuration.
199+
200+ When not <literal>null</literal> the attribute set gets converted to
201+ a YAML file and stored in the Nix store. The option
202+ <option>objstore.config-file</option> will default to its path.
203+
204+ If <option>objstore.config-file</option> is set this option has no effect.
205+ '';
206+ };
207+ };
208+
209+ sidecar = params.common cfg.sidecar // params.objstore cfg.sidecar // {
210+
211+ prometheus.url = mkParamDef types.str "http://localhost:9090" ''
212+ URL at which to reach Prometheus's API.
213+
214+ For better performance use local network.
215+ '';
216+
217+ tsdb.path = {
218+ toArgs = optionToArgs;
219+ option = mkOption {
220+ type = types.str;
221+ default = "/var/lib/${config.services.prometheus2.stateDir}/data";
222+ defaultText = "/var/lib/\${config.services.prometheus2.stateDir}/data";
223+ description = ''
224+ Data directory of TSDB.
225+ '';
226+ };
227+ };
228+
229+ reloader.config-file = mkParam types.str ''
230+ Config file watched by the reloader.
231+ '';
232+
233+ reloader.config-envsubst-file = mkParam types.str ''
234+ Output file for environment variable substituted config file.
235+ '';
236+
237+ reloader.rule-dirs = mkListParam "reloader.rule-dir" ''
238+ Rule directories for the reloader to refresh.
239+ '';
240+
241+ };
242+
243+ store = params.common cfg.store // params.objstore cfg.store // {
244+
245+ stateDir = mkStateDirParam "data-dir" "thanos-store" ''
246+ Data directory relative to <literal>/var/lib</literal>
247+ in which to cache remote blocks.
248+ '';
249+
250+ index-cache-size = mkParamDef types.str "250MB" ''
251+ Maximum size of items held in the index cache.
252+ '';
253+
254+ chunk-pool-size = mkParamDef types.str "2GB" ''
255+ Maximum size of concurrently allocatable bytes for chunks.
256+ '';
257+
258+ store.grpc.series-sample-limit = mkParamDef types.int 0 ''
259+ Maximum amount of samples returned via a single Series call.
260+
261+ <literal>0</literal> means no limit.
262+
263+ NOTE: for efficiency we take 120 as the number of samples in chunk (it
264+ cannot be bigger than that), so the actual number of samples might be
265+ lower, even though the maximum could be hit.
266+ '';
267+
268+ store.grpc.series-max-concurrency = mkParamDef types.int 20 ''
269+ Maximum number of concurrent Series calls.
270+ '';
271+
272+ sync-block-duration = mkParamDef types.str "3m" ''
273+ Repeat interval for syncing the blocks between local and remote view.
274+ '';
275+
276+ block-sync-concurrency = mkParamDef types.int 20 ''
277+ Number of goroutines to use when syncing blocks from object storage.
278+ '';
279+ };
280+
281+ query = params.common cfg.query // {
282+
283+ grpc-client-tls-secure = mkFlagParam ''
284+ Use TLS when talking to the gRPC server
285+ '';
286+
287+ grpc-client-tls-cert = mkParam types.str ''
288+ TLS Certificates to use to identify this client to the server
289+ '';
290+
291+ grpc-client-tls-key = mkParam types.str ''
292+ TLS Key for the client's certificate
293+ '';
294+
295+ grpc-client-tls-ca = mkParam types.str ''
296+ TLS CA Certificates to use to verify gRPC servers
297+ '';
298+
299+ grpc-client-server-name = mkParam types.str ''
300+ Server name to verify the hostname on the returned gRPC certificates.
301+ See <link xlink:href="https://tools.ietf.org/html/rfc4366#section-3.1"/>
302+ '';
303+
304+ web.route-prefix = mkParam types.str ''
305+ Prefix for API and UI endpoints.
306+
307+ This allows thanos UI to be served on a sub-path. This option is
308+ analogous to <option>web.route-prefix</option> of Promethus.
309+ '';
310+
311+ web.external-prefix = mkParam types.str ''
312+ Static prefix for all HTML links and redirect URLs in the UI query web
313+ interface.
314+
315+ Actual endpoints are still served on / or the
316+ <option>web.route-prefix</option>. This allows thanos UI to be served
317+ behind a reverse proxy that strips a URL sub-path.
318+ '';
319+
320+ web.prefix-header = mkParam types.str ''
321+ Name of HTTP request header used for dynamic prefixing of UI links and
322+ redirects.
323+
324+ This option is ignored if the option
325+ <literal>web.external-prefix</literal> is set.
326+
327+ Security risk: enable this option only if a reverse proxy in front of
328+ thanos is resetting the header.
329+
330+ The setting <literal>web.prefix-header="X-Forwarded-Prefix"</literal>
331+ can be useful, for example, if Thanos UI is served via Traefik reverse
332+ proxy with <literal>PathPrefixStrip</literal> option enabled, which
333+ sends the stripped prefix value in <literal>X-Forwarded-Prefix</literal>
334+ header. This allows thanos UI to be served on a sub-path.
335+ '';
336+
337+ query.timeout = mkParamDef types.str "2m" ''
338+ Maximum time to process query by query node.
339+ '';
340+
341+ query.max-concurrent = mkParamDef types.int 20 ''
342+ Maximum number of queries processed concurrently by query node.
343+ '';
344+
345+ query.replica-label = mkParam types.str ''
346+ Label to treat as a replica indicator along which data is
347+ deduplicated.
348+
349+ Still you will be able to query without deduplication using
350+ <literal>dedup=false</literal> parameter.
351+ '';
352+
353+ selector-labels = mkAttrsParam "selector-label" ''
354+ Query selector labels that will be exposed in info endpoint.
355+ '';
356+
357+ store.addresses = mkListParam "store" ''
358+ Addresses of statically configured store API servers.
359+
360+ The scheme may be prefixed with <literal>dns+</literal> or
361+ <literal>dnssrv+</literal> to detect store API servers through
362+ respective DNS lookups.
363+ '';
364+
365+ store.sd-files = mkListParam "store.sd-files" ''
366+ Path to files that contain addresses of store API servers. The path
367+ can be a glob pattern.
368+ '';
369+
370+ store.sd-interval = mkParamDef types.str "5m" ''
371+ Refresh interval to re-read file SD files. It is used as a resync fallback.
372+ '';
373+
374+ store.sd-dns-interval = mkParamDef types.str "30s" ''
375+ Interval between DNS resolutions.
376+ '';
377+
378+ store.unhealthy-timeout = mkParamDef types.str "5m" ''
379+ Timeout before an unhealthy store is cleaned from the store UI page.
380+ '';
381+
382+ query.auto-downsampling = mkFlagParam ''
383+ Enable automatic adjustment (step / 5) to what source of data should
384+ be used in store gateways if no
385+ <literal>max_source_resolution</literal> param is specified.
386+ '';
387+
388+ query.partial-response = mkFlagParam ''
389+ Enable partial response for queries if no
390+ <literal>partial_response</literal> param is specified.
391+ '';
392+
393+ query.default-evaluation-interval = mkParamDef types.str "1m" ''
394+ Set default evaluation interval for sub queries.
395+ '';
396+
397+ store.response-timeout = mkParamDef types.str "0ms" ''
398+ If a Store doesn't send any data in this specified duration then a
399+ Store will be ignored and partial data will be returned if it's
400+ enabled. <literal>0</literal> disables timeout.
401+ '';
402+ };
403+
404+ rule = params.common cfg.rule // params.objstore cfg.rule // {
405+
406+ labels = mkAttrsParam "label" ''
407+ Labels to be applied to all generated metrics.
408+
409+ Similar to external labels for Prometheus,
410+ used to identify ruler and its blocks as unique source.
411+ '';
412+
413+ stateDir = mkStateDirParam "data-dir" "thanos-rule" ''
414+ Data directory relative to <literal>/var/lib</literal>.
415+ '';
416+
417+ rule-files = mkListParam "rule-file" ''
418+ Rule files that should be used by rule manager. Can be in glob format.
419+ '';
420+
421+ eval-interval = mkParamDef types.str "30s" ''
422+ The default evaluation interval to use.
423+ '';
424+
425+ tsdb.block-duration = mkParamDef types.str "2h" ''
426+ Block duration for TSDB block.
427+ '';
428+
429+ tsdb.retention = mkParamDef types.str "48h" ''
430+ Block retention time on local disk.
431+ '';
432+
433+ alertmanagers.urls = mkListParam "alertmanagers.url" ''
434+ Alertmanager replica URLs to push firing alerts.
435+
436+ Ruler claims success if push to at least one alertmanager from
437+ discovered succeeds. The scheme may be prefixed with
438+ <literal>dns+</literal> or <literal>dnssrv+</literal> to detect
439+ Alertmanager IPs through respective DNS lookups. The port defaults to
440+ <literal>9093</literal> or the SRV record's value. The URL path is
441+ used as a prefix for the regular Alertmanager API path.
442+ '';
443+
444+ alertmanagers.send-timeout = mkParamDef types.str "10s" ''
445+ Timeout for sending alerts to alertmanager.
446+ '';
447+
448+ alert.query-url = mkParam types.str ''
449+ The external Thanos Query URL that would be set in all alerts 'Source' field.
450+ '';
451+
452+ alert.label-drop = mkListParam "alert.label-drop" ''
453+ Labels by name to drop before sending to alertmanager.
454+
455+ This allows alert to be deduplicated on replica label.
456+
457+ Similar Prometheus alert relabelling
458+ '';
459+
460+ web.route-prefix = mkParam types.str ''
461+ Prefix for API and UI endpoints.
462+
463+ This allows thanos UI to be served on a sub-path.
464+
465+ This option is analogous to <literal>--web.route-prefix</literal> of Promethus.
466+ '';
467+
468+ web.external-prefix = mkParam types.str ''
469+ Static prefix for all HTML links and redirect URLs in the UI query web
470+ interface.
471+
472+ Actual endpoints are still served on / or the
473+ <option>web.route-prefix</option>. This allows thanos UI to be served
474+ behind a reverse proxy that strips a URL sub-path.
475+ '';
476+
477+ web.prefix-header = mkParam types.str ''
478+ Name of HTTP request header used for dynamic prefixing of UI links and
479+ redirects.
480+
481+ This option is ignored if the option
482+ <option>web.external-prefix</option> is set.
483+
484+ Security risk: enable this option only if a reverse proxy in front of
485+ thanos is resetting the header.
486+
487+ The header <literal>X-Forwarded-Prefix</literal> can be useful, for
488+ example, if Thanos UI is served via Traefik reverse proxy with
489+ <literal>PathPrefixStrip</literal> option enabled, which sends the
490+ stripped prefix value in <literal>X-Forwarded-Prefix</literal>
491+ header. This allows thanos UI to be served on a sub-path.
492+ '';
493+
494+ query.addresses = mkListParam "query" ''
495+ Addresses of statically configured query API servers.
496+
497+ The scheme may be prefixed with <literal>dns+</literal> or
498+ <literal>dnssrv+</literal> to detect query API servers through
499+ respective DNS lookups.
500+ '';
501+
502+ query.sd-files = mkListParam "query.sd-files" ''
503+ Path to file that contain addresses of query peers.
504+ The path can be a glob pattern.
505+ '';
506+
507+ query.sd-interval = mkParamDef types.str "5m" ''
508+ Refresh interval to re-read file SD files. (used as a fallback)
509+ '';
510+
511+ query.sd-dns-interval = mkParamDef types.str "30s" ''
512+ Interval between DNS resolutions.
513+ '';
514+ };
515+
516+ compact = params.log // params.tracing cfg.compact // params.objstore cfg.compact // {
517+
518+ http-address = mkParamDef types.str "0.0.0.0:10902" ''
519+ Listen <literal>host:port</literal> for HTTP endpoints.
520+ '';
521+
522+ stateDir = mkStateDirParam "data-dir" "thanos-compact" ''
523+ Data directory relative to <literal>/var/lib</literal>
524+ in which to cache blocks and process compactions.
525+ '';
526+
527+ consistency-delay = mkParamDef types.str "30m" ''
528+ Minimum age of fresh (non-compacted) blocks before they are being
529+ processed. Malformed blocks older than the maximum of consistency-delay
530+ and 30m0s will be removed.
531+ '';
532+
533+ retention.resolution-raw = mkParamDef types.str "0d" ''
534+ How long to retain raw samples in bucket.
535+
536+ <literal>0d</literal> - disables this retention
537+ '';
538+
539+ retention.resolution-5m = mkParamDef types.str "0d" ''
540+ How long to retain samples of resolution 1 (5 minutes) in bucket.
541+
542+ <literal>0d</literal> - disables this retention
543+ '';
544+
545+ retention.resolution-1h = mkParamDef types.str "0d" ''
546+ How long to retain samples of resolution 2 (1 hour) in bucket.
547+
548+ <literal>0d</literal> - disables this retention
549+ '';
550+
551+ startAt = {
552+ toArgs = _opt: startAt: flagToArgs "wait" (startAt == null);
553+ option = nullOpt types.str ''
554+ When this option is set to a <literal>systemd.time</literal>
555+ specification the Thanos compactor will run at the specified period.
556+
557+ When this option is <literal>null</literal> the Thanos compactor service
558+ will run continuously. So it will not exit after all compactions have
559+ been processed but wait for new work.
560+ '';
561+ };
562+
563+ block-sync-concurrency = mkParamDef types.int 20 ''
564+ Number of goroutines to use when syncing block metadata from object storage.
565+ '';
566+
567+ compact.concurrency = mkParamDef types.int 1 ''
568+ Number of goroutines to use when compacting groups.
569+ '';
570+ };
571+
572+ downsample = params.log // params.tracing cfg.downsample // params.objstore cfg.downsample // {
573+
574+ stateDir = mkStateDirParam "data-dir" "thanos-downsample" ''
575+ Data directory relative to <literal>/var/lib</literal>
576+ in which to cache blocks and process downsamplings.
577+ '';
578+
579+ };
580+
581+ receive = params.common cfg.receive // params.objstore cfg.receive // {
582+
583+ remote-write.address = mkParamDef types.str "0.0.0.0:19291" ''
584+ Address to listen on for remote write requests.
585+ '';
586+
587+ stateDir = mkStateDirParam "tsdb.path" "thanos-receive" ''
588+ Data directory relative to <literal>/var/lib</literal> of TSDB.
589+ '';
590+
591+ labels = mkAttrsParam "labels" ''
592+ External labels to announce.
593+
594+ This flag will be removed in the future when handling multiple tsdb
595+ instances is added.
596+ '';
597+
598+ tsdb.retention = mkParamDef types.str "15d" ''
599+ How long to retain raw samples on local storage.
600+
601+ <literal>0d</literal> - disables this retention
602+ '';
603+ };
604+
605+ };
606+
607+ assertRelativeStateDir = cmd: {
608+ assertions = [
609+ {
610+ assertion = !hasPrefix "/" cfg."${cmd}".stateDir;
611+ message =
612+ "The option services.thanos.${cmd}.stateDir should not be an absolute directory." +
613+ " It should be a directory relative to /var/lib.";
614+ }
615+ ];
616+ };
617+
618+in {
619+
620+ options.services.thanos = {
621+
622+ package = mkOption {
623+ type = types.package;
624+ default = pkgs.thanos;
625+ defaultText = "pkgs.thanos";
626+ description = ''
627+ The thanos package that should be used.
628+ '';
629+ };
630+
631+ sidecar = paramsToOptions params.sidecar // {
632+ enable = mkEnableOption
633+ "the Thanos sidecar for Prometheus server";
634+ arguments = mkArgumentsOption "sidecar";
635+ };
636+
637+ store = paramsToOptions params.store // {
638+ enable = mkEnableOption
639+ "the Thanos store node giving access to blocks in a bucket provider.";
640+ arguments = mkArgumentsOption "store";
641+ };
642+
643+ query = paramsToOptions params.query // {
644+ enable = mkEnableOption
645+ ("the Thanos query node exposing PromQL enabled Query API " +
646+ "with data retrieved from multiple store nodes");
647+ arguments = mkArgumentsOption "query";
648+ };
649+
650+ rule = paramsToOptions params.rule // {
651+ enable = mkEnableOption
652+ ("the Thanos ruler service which evaluates Prometheus rules against" +
653+ " given Query nodes, exposing Store API and storing old blocks in bucket");
654+ arguments = mkArgumentsOption "rule";
655+ };
656+
657+ compact = paramsToOptions params.compact // {
658+ enable = mkEnableOption
659+ "the Thanos compactor which continuously compacts blocks in an object store bucket";
660+ arguments = mkArgumentsOption "compact";
661+ };
662+
663+ downsample = paramsToOptions params.downsample // {
664+ enable = mkEnableOption
665+ "the Thanos downsampler which continuously downsamples blocks in an object store bucket";
666+ arguments = mkArgumentsOption "downsample";
667+ };
668+
669+ receive = paramsToOptions params.receive // {
670+ enable = mkEnableOption
671+ ("the Thanos receiver which accept Prometheus remote write API requests " +
672+ "and write to local tsdb (EXPERIMENTAL, this may change drastically without notice)");
673+ arguments = mkArgumentsOption "receive";
674+ };
675+ };
676+
677+ config = mkMerge [
678+
679+ (mkIf cfg.sidecar.enable {
680+ assertions = [
681+ {
682+ assertion = config.services.prometheus2.enable;
683+ message =
684+ "Please enable services.prometheus2 when enabling services.thanos.sidecar.";
685+ }
686+ {
687+ assertion = !(config.services.prometheus2.globalConfig.external_labels == null ||
688+ config.services.prometheus2.globalConfig.external_labels == {});
689+ message =
690+ "services.thanos.sidecar requires uniquely identifying external labels " +
691+ "to be configured in the Prometheus server. " +
692+ "Please set services.prometheus2.globalConfig.external_labels.";
693+ }
694+ ];
695+ systemd.services.thanos-sidecar = {
696+ wantedBy = [ "multi-user.target" ];
697+ after = [ "network.target" "prometheus2.service" ];
698+ serviceConfig = {
699+ User = "prometheus";
700+ Restart = "always";
701+ ExecStart = thanos "sidecar";
702+ };
703+ };
704+ })
705+
706+ (mkIf cfg.store.enable (mkMerge [
707+ (assertRelativeStateDir "store")
708+ {
709+ systemd.services.thanos-store = {
710+ wantedBy = [ "multi-user.target" ];
711+ after = [ "network.target" ];
712+ serviceConfig = {
713+ DynamicUser = true;
714+ StateDirectory = cfg.store.stateDir;
715+ Restart = "always";
716+ ExecStart = thanos "store";
717+ };
718+ };
719+ }
720+ ]))
721+
722+ (mkIf cfg.query.enable {
723+ systemd.services.thanos-query = {
724+ wantedBy = [ "multi-user.target" ];
725+ after = [ "network.target" ];
726+ serviceConfig = {
727+ DynamicUser = true;
728+ Restart = "always";
729+ ExecStart = thanos "query";
730+ };
731+ };
732+ })
733+
734+ (mkIf cfg.rule.enable (mkMerge [
735+ (assertRelativeStateDir "rule")
736+ {
737+ systemd.services.thanos-rule = {
738+ wantedBy = [ "multi-user.target" ];
739+ after = [ "network.target" ];
740+ serviceConfig = {
741+ DynamicUser = true;
742+ StateDirectory = cfg.rule.stateDir;
743+ Restart = "always";
744+ ExecStart = thanos "rule";
745+ };
746+ };
747+ }
748+ ]))
749+
750+ (mkIf cfg.compact.enable (mkMerge [
751+ (assertRelativeStateDir "compact")
752+ {
753+ systemd.services.thanos-compact =
754+ let wait = cfg.compact.startAt == null; in {
755+ wantedBy = [ "multi-user.target" ];
756+ after = [ "network.target" ];
757+ serviceConfig = {
758+ Type = if wait then "simple" else "oneshot";
759+ Restart = if wait then "always" else "no";
760+ DynamicUser = true;
761+ StateDirectory = cfg.compact.stateDir;
762+ ExecStart = thanos "compact";
763+ };
764+ } // optionalAttrs (!wait) { inherit (cfg.compact) startAt; };
765+ }
766+ ]))
767+
768+ (mkIf cfg.downsample.enable (mkMerge [
769+ (assertRelativeStateDir "downsample")
770+ {
771+ systemd.services.thanos-downsample = {
772+ wantedBy = [ "multi-user.target" ];
773+ after = [ "network.target" ];
774+ serviceConfig = {
775+ DynamicUser = true;
776+ StateDirectory = cfg.downsample.stateDir;
777+ Restart = "always";
778+ ExecStart = thanos "downsample";
779+ };
780+ };
781+ }
782+ ]))
783+
784+ (mkIf cfg.receive.enable (mkMerge [
785+ (assertRelativeStateDir "receive")
786+ {
787+ systemd.services.thanos-receive = {
788+ wantedBy = [ "multi-user.target" ];
789+ after = [ "network.target" ];
790+ serviceConfig = {
791+ DynamicUser = true;
792+ StateDirectory = cfg.receive.stateDir;
793+ Restart = "always";
794+ ExecStart = thanos "receive";
795+ };
796+ };
797+ }
798+ ]))
799+
800+ ];
801+}
nixos/tests/prometheus-2.nix+190−18
@@ -1,9 +1,44 @@
1−import ./make-test.nix {
1+let
2+ grpcPort = 19090;
3+ queryPort = 9090;
4+ minioPort = 9000;
5+ pushgwPort = 9091;
6+
7+ s3 = {
8+ accessKey = "BKIKJAA5BMMU2RHO6IBB";
9+ secretKey = "V7f1CwQqAcwo80UEIJEjc5gVQUSSx5ohQ9GSrr12";
10+ };
11+
12+ objstore.config = {
13+ type = "S3";
14+ config = {
15+ bucket = "thanos-bucket";
16+ endpoint = "s3:${toString minioPort}";
17+ region = "us-east-1";
18+ access_key = s3.accessKey;
19+ secret_key = s3.secretKey;
20+ insecure = true;
21+ signature_version2 = false;
22+ encrypt_sse = false;
23+ put_user_metadata = {};
24+ http_config = {
25+ idle_conn_timeout = "0s";
26+ insecure_skip_verify = false;
27+ };
28+ trace = {
29+ enable = false;
30+ };
31+ };
32+ };
33+
34+in import ./make-test.nix {
235 name = "prometheus-2";
336
437 nodes = {
5− one = { pkgs, ... }: {
38+ prometheus = { pkgs, ... }: {
39+ virtualisation.diskSize = 2 * 1024;
640 environment.systemPackages = [ pkgs.jq ];
41+ networking.firewall.allowedTCPPorts = [ grpcPort ];
742 services.prometheus2 = {
843 enable = true;
944 scrapeConfigs = [
@@ -11,7 +46,7 @@ import ./make-test.nix {
1146 job_name = "prometheus";
1247 static_configs = [
1348 {
14− targets = [ "127.0.0.1:9090" ];
49+ targets = [ "127.0.0.1:${toString queryPort}" ];
1550 labels = { instance = "localhost"; };
1651 }
1752 ];
@@ -21,7 +56,7 @@ import ./make-test.nix {
2156 scrape_interval = "1s";
2257 static_configs = [
2358 {
24− targets = [ "127.0.0.1:9091" ];
59+ targets = [ "127.0.0.1:${toString pushgwPort}" ];
2560 }
2661 ];
2762 }
@@ -35,33 +70,170 @@ import ./make-test.nix {
3570 expr: count(up{job="prometheus"})
3671 ''
3772 ];
73+ globalConfig = {
74+ external_labels = {
75+ some_label = "required by thanos";
76+ };
77+ };
78+ extraFlags = [
79+ # Required by thanos
80+ "--storage.tsdb.min-block-duration=5s"
81+ "--storage.tsdb.max-block-duration=5s"
82+ ];
3883 };
3984 services.prometheus.pushgateway = {
4085 enable = true;
86+ web.listen-address = ":${toString pushgwPort}";
4187 persistMetrics = true;
4288 persistence.interval = "1s";
4389 stateDir = "prometheus-pushgateway";
4490 };
91+ services.thanos = {
92+ sidecar = {
93+ enable = true;
94+ grpc-address = "0.0.0.0:${toString grpcPort}";
95+ inherit objstore;
96+ };
97+
98+ # TODO: Add some tests for these services:
99+ #rule = {
100+ # enable = true;
101+ # http-address = "0.0.0.0:19194";
102+ # grpc-address = "0.0.0.0:19193";
103+ # query.addresses = [
104+ # "localhost:19191"
105+ # ];
106+ # labels = {
107+ # just = "some";
108+ # nice = "labels";
109+ # };
110+ #};
111+ #
112+ #receive = {
113+ # http-address = "0.0.0.0:19195";
114+ # enable = true;
115+ # labels = {
116+ # just = "some";
117+ # nice = "labels";
118+ # };
119+ #};
120+ };
121+ };
122+
123+ query = { pkgs, ... }: {
124+ environment.systemPackages = [ pkgs.jq ];
125+ services.thanos.query = {
126+ enable = true;
127+ http-address = "0.0.0.0:${toString queryPort}";
128+ store.addresses = [
129+ "prometheus:${toString grpcPort}"
130+ ];
131+ };
132+ };
133+
134+ store = { pkgs, ... }: {
135+ virtualisation.diskSize = 2 * 1024;
136+ environment.systemPackages = with pkgs; [ jq thanos ];
137+ services.thanos.store = {
138+ enable = true;
139+ http-address = "0.0.0.0:10902";
140+ grpc-address = "0.0.0.0:${toString grpcPort}";
141+ inherit objstore;
142+ sync-block-duration = "1s";
143+ };
144+ services.thanos.compact = {
145+ enable = true;
146+ http-address = "0.0.0.0:10903";
147+ inherit objstore;
148+ consistency-delay = "5s";
149+ };
150+ services.thanos.query = {
151+ enable = true;
152+ http-address = "0.0.0.0:${toString queryPort}";
153+ store.addresses = [
154+ "localhost:${toString grpcPort}"
155+ ];
156+ };
157+ };
158+
159+ s3 = { pkgs, ... } : {
160+ # Minio requires at least 1GiB of free disk space to run.
161+ virtualisation.diskSize = 2 * 1024;
162+ networking.firewall.allowedTCPPorts = [ minioPort ];
163+
164+ services.minio = {
165+ enable = true;
166+ inherit (s3) accessKey secretKey;
167+ };
168+
169+ environment.systemPackages = [ pkgs.minio-client ];
45170 };
46171 };
47172
48− testScript = ''
49− startAll;
50− $one->waitForUnit("prometheus2.service");
51− $one->waitForOpenPort(9090);
52− $one->succeed("curl -s http://127.0.0.1:9090/metrics");
173+ testScript = { nodes, ... } : ''
174+ # Before starting the other machines we first make sure that our S3 service is online
175+ # and has a bucket added for thanos:
176+ $s3->start;
177+ $s3->waitForUnit("minio.service");
178+ $s3->waitForOpenPort(${toString minioPort});
179+ $s3->succeed(
180+ "mc config host add minio " .
181+ "http://localhost:${toString minioPort} ${s3.accessKey} ${s3.secretKey} S3v4");
182+ $s3->succeed("mc mb minio/thanos-bucket");
183+
184+ # Now that s3 has started we can start the other machines:
185+ $prometheus->start;
186+ $query->start;
187+ $store->start;
53188
54− # Let's test if pushing a metric to the pushgateway succeeds
55− # and whether that metric gets ingested by prometheus.
56− $one->waitForUnit("pushgateway.service");
57− $one->succeed(
189+ # Check if prometheus responds to requests:
190+ $prometheus->waitForUnit("prometheus2.service");
191+ $prometheus->waitForOpenPort(${toString queryPort});
192+ $prometheus->succeed("curl -s http://127.0.0.1:${toString queryPort}/metrics");
193+
194+ # Let's test if pushing a metric to the pushgateway succeeds:
195+ $prometheus->waitForUnit("pushgateway.service");
196+ $prometheus->succeed(
58197 "echo 'some_metric 3.14' | " .
59− "curl --data-binary \@- http://127.0.0.1:9091/metrics/job/some_job");
60− $one->waitUntilSucceeds(
61− "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=some_metric' " .
62− "| jq '.data.result[0].value[1]' | grep '\"3.14\"'");
198+ "curl --data-binary \@- http://127.0.0.1:${toString pushgwPort}/metrics/job/some_job");
199+
200+ # Now check whether that metric gets ingested by prometheus.
201+ # Since we'll check for the metric several times on different machines
202+ # we abstract the test using the following function:
203+
204+ # Function to check if the metric "some_metric" has been received and returns the correct value.
205+ local *Machine::waitForMetric = sub {
206+ my ($self) = @_;
207+ $self->waitUntilSucceeds(
208+ "curl -sf 'http://127.0.0.1:${toString queryPort}/api/v1/query?query=some_metric' " .
209+ "| jq '.data.result[0].value[1]' | grep '\"3.14\"'");
210+ };
211+
212+ $prometheus->waitForMetric;
63213
64214 # Let's test if the pushgateway persists metrics to the configured location.
65− $one->waitUntilSucceeds("test -e /var/lib/prometheus-pushgateway/metrics");
215+ $prometheus->waitUntilSucceeds("test -e /var/lib/prometheus-pushgateway/metrics");
216+
217+ # Test thanos
218+ $prometheus->waitForUnit("thanos-sidecar.service");
219+
220+ # Test if the Thanos query service can correctly retrieve the metric that was send above.
221+ $query->waitForUnit("thanos-query.service");
222+ $query->waitForMetric;
223+
224+ # Test if the Thanos sidecar has correctly uploaded its TSDB to S3, if the
225+ # Thanos storage service has correctly downloaded it from S3 and if the Thanos
226+ # query service running on $store can correctly retrieve the metric:
227+ $store->waitForUnit("thanos-store.service");
228+ $store->waitForMetric;
229+
230+ $store->waitForUnit("thanos-compact.service");
231+
232+ # Test if the Thanos bucket command is able to retrieve blocks from the S3 bucket
233+ # and check if the blocks have the correct labels:
234+ $store->succeed(
235+ "thanos bucket ls" .
236+ " --objstore.config-file=${nodes.store.config.services.thanos.store.objstore.config-file}" .
237+ " --output=json | jq .thanos.labels.some_label | grep 'required by thanos'");
66238 '';
67239}
pkgs/development/go-modules/generic/default.nix+6−3
@@ -7,6 +7,9 @@
77, passthru ? {}
88, patches ? []
99
10+# A function to override the go-modules derivation
11+, overrideModAttrs ? (_oldAttrs : {})
12+
1013# modSha256 is the sha256 of the vendored dependencies
1114, modSha256
1215
@@ -27,13 +30,13 @@
2730with builtins;
2831
2932let
30− args = removeAttrs args' [ "modSha256" "disabled" ];
33+ args = removeAttrs args' [ "overrideModAttrs" "modSha256" "disabled" ];
3134
3235 removeReferences = [ ] ++ lib.optional (!allowGoReference) go;
3336
3437 removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}'';
3538
36− go-modules = go.stdenv.mkDerivation {
39+ go-modules = go.stdenv.mkDerivation (let modArgs = {
3740 name = "${name}-go-modules";
3841
3942 nativeBuildInputs = [ go git cacert ];
@@ -79,7 +82,7 @@ let
7982 outputHashMode = "recursive";
8083 outputHashAlgo = "sha256";
8184 outputHash = modSha256;
82− };
85+ }; in modArgs // overrideModAttrs modArgs);
8386
8487 package = go.stdenv.mkDerivation (args // {
8588 nativeBuildInputs = [ removeReferencesTo go ] ++ nativeBuildInputs;
pkgs/servers/monitoring/thanos/default.nixnew file+33
@@ -0,0 +1,33 @@
1+{ stdenv, buildGoModule, fetchFromGitHub }:
2+buildGoModule rec {
3+ pname = "thanos";
4+ version = "0.6.0";
5+
6+ src = fetchFromGitHub {
7+ rev = "v${version}";
8+ owner = "improbable-eng";
9+ repo = "thanos";
10+ sha256 = "0vcp7m8fsk4jyk49jh9wmbvkx5k03xw10f4lbsxfmwib1y5pz2x0";
11+ };
12+
13+ modSha256 = "139b66jdryqv4s1hjbn9fzkyzn1160wr4z6a6wmmvm3f6p6wgjxp";
14+
15+ subPackages = "cmd/thanos";
16+
17+ buildFlagsArray = let t = "github.com/prometheus/common/version"; in ''
18+ -ldflags=
19+ -X ${t}.Version=${version}
20+ -X ${t}.Revision=unknown
21+ -X ${t}.Branch=unknown
22+ -X ${t}.BuildUser=nix@nixpkgs
23+ -X ${t}.BuildDate=unknown
24+ '';
25+
26+ meta = with stdenv.lib; {
27+ description = "Highly available Prometheus setup with long term storage capabilities";
28+ homepage = "https://github.com/improbable-eng/thanos";
29+ license = licenses.asl20;
30+ maintainers = with maintainers; [ basvandijk ];
31+ platforms = platforms.unix;
32+ };
33+}
pkgs/top-level/all-packages.nix+2
@@ -14952,6 +14952,8 @@ in
1495214952
1495314953 syncserver = callPackage ../servers/syncserver { };
1495414954
14955+ thanos = callPackage ../servers/monitoring/thanos { };
14956+
1495514957 inherit (callPackages ../servers/http/tomcat { })
1495614958 tomcat7
1495714959 tomcat8