Skip to content

Commit

Permalink
add prometheus service (#114)
Browse files Browse the repository at this point in the history
* prometheus: init service

* prometheus: add doc

* prometheus: update doc
  • Loading branch information
conscious-puppet authored Feb 28, 2024
1 parent 8d877fb commit bdd6dde
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
41 changes: 41 additions & 0 deletions doc/prometheus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Prometheus

[Prometheus] is a systems and service monitoring system. It collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts when specified conditions are observed.

[Prometheus]: https://github.com/prometheus/prometheus

## Getting Started

```nix
# In `perSystem.process-compose.<name>`
{
services.prometheus."pro1".enable = true;
}
```

{#tips}
## Tips & Tricks

{#scrape-configs}
### Adding Scrape Configs

`scrape_configs` controls what resources Prometheus monitors.

Since Prometheus also exposes data about itself as an HTTP endpoint it can scrape and monitor its own health. In the [default example configuration](https://github.com/prometheus/prometheus/blob/3f686cad8bee405229b2532584ef181ce9f6a8b3/documentation/examples/prometheus.yml) there is a single job, called prometheus. We can add it to `scrape_configs` using the following config:

```nix
{
services.prometheus."pro1" = {
enable = true;
# scrape prometheus
extraConfig = {
scrape_configs = [{
job_name = "prometheus";
static_configs = [{
targets = [ "localhost:9090" ];
}];
}];
};
};
}
```
1 change: 1 addition & 0 deletions doc/services.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ short-title: Services
- [ ] Redis Cluster
- [ ] Zookeeper
- [x] [[grafana]]#
- [X] [[prometheus]]#
- [ ] ...

[gh]: https://github.com/juspay/services-flake
1 change: 1 addition & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ in
./redis.nix
./zookeeper.nix
./grafana.nix
./prometheus.nix
];
}
109 changes: 109 additions & 0 deletions nix/prometheus.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{ pkgs, lib, name, config, ... }:
let
inherit (lib) types;
yamlFormat = pkgs.formats.yaml { };
in
{
options = {
enable = lib.mkEnableOption name;

package = lib.mkPackageOption pkgs "prometheus" { };

port = lib.mkOption {
type = types.port;
default = 9090;
description = "Port to listen on";
};

listenAddress = lib.mkOption {
type = types.str;
default = "0.0.0.0";
description = lib.mdDoc "Address to listen on for the web interface, API, and telemetry";
};

dataDir = lib.mkOption {
type = types.str;
default = "./data/${name}";
description = "The prometheus data directory";
};

extraFlags = lib.mkOption {
type = types.listOf types.str;
default = [ ];
description = "Extra commandline options when launching Prometheus";
};

defaultExtraConfig = lib.mkOption {
type = yamlFormat.type;
internal = true;
readOnly = true;
default = {
global = {
scrape_interval = "15s";
evaluation_interval = "15s";
};
};
};

extraConfig = lib.mkOption {
type = yamlFormat.type;
default = { };
description = "Additional config for prometheus";
example = ''
# scrape prometheus itself
scrape_configs = [{
job_name = "prometheus";
static_configs = [{
targets = [ "localhost:9090" ];
}];
}];
'';
};

outputs.settings = lib.mkOption {
type = types.deferredModule;
internal = true;
readOnly = true;
default = {
processes = {
"${name}" =
let
prometheusConfig = yamlFormat.generate "prometheus.yaml" (
lib.recursiveUpdate config.defaultExtraConfig config.extraConfig
);
execFlags = builtins.concatStringsSep " \\\n" ([
"--config.file=${prometheusConfig}"
"--storage.tsdb.path=${config.dataDir}"
"--web.listen-address=${config.listenAddress}:${builtins.toString config.port}"
] ++ config.extraFlags);

startScript = pkgs.writeShellApplication {
name = "start-prometheus";
runtimeInputs = [ config.package ];
text = "prometheus ${execFlags}";
};
in
{
command = "${startScript}/bin/start-prometheus";
readiness_probe = {
http_get = {
host = config.listenAddress;
port = config.port;
path = "/-/ready";
};
initial_delay_seconds = 2;
period_seconds = 10;
timeout_seconds = 4;
success_threshold = 1;
failure_threshold = 5;
};
namespace = name;

# https://github.com/F1bonacc1/process-compose#-auto-restart-if-not-healthy
availability.restart = "on_failure";
};
};
};
};
};
}
30 changes: 30 additions & 0 deletions nix/prometheus_test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{ pkgs, config, ... }: {
services.prometheus."pro1" = {
enable = true;
# scrape prometheus
extraConfig = {
scrape_configs = [{
job_name = "prometheus";
static_configs = [{
targets = [ "localhost:9090" ];
}];
}];
};
};

settings.processes.test =
let
cfg = config.services.prometheus."pro1";
in
{
command = pkgs.writeShellApplication {
runtimeInputs = [ cfg.package pkgs.curl pkgs.gnugrep ];
text = ''
curl -sS ${cfg.listenAddress}:${builtins.toString cfg.port}/-/healthy
curl -s -o /dev/null -w "%{http_code}" ${cfg.listenAddress}:${builtins.toString cfg.port}/metrics
'';
name = "prometheus-test";
};
depends_on."pro1".condition = "process_healthy";
};
}
1 change: 1 addition & 0 deletions test/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"${inputs.services-flake}/nix/redis-cluster_test.nix"
"${inputs.services-flake}/nix/zookeeper_test.nix"
"${inputs.services-flake}/nix/grafana_test.nix"
"${inputs.services-flake}/nix/prometheus_test.nix"
]);
};
};
Expand Down

0 comments on commit bdd6dde

Please sign in to comment.