nix-server/modules/services/metrics.nix
2024-02-26 17:02:11 -08:00

98 lines
2 KiB
Nix

{ config, lib, pkgs, options, ... }:
with lib;
let
cfg = config.modules.services.metrics;
in {
options.modules.services.metrics = {
enable = mkOption {
type = types.bool;
default = false;
};
domain = mkOption {
type = types.str;
default = "grafana.reidlab.online";
};
port = mkOption {
type = types.int;
default = 2342;
};
};
config = mkIf cfg.enable {
systemd.services.promtail = {
description = "promtail, an agent for loki";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = ''
${pkgs.grafana-loki}/bin/promtail --config.file ${./promtail.yml}
'';
};
};
services = {
grafana = {
enable = true;
settings = {
server = {
domain = cfg.domain;
http_port = cfg.port;
http_addr = "127.0.0.1";
};
};
};
prometheus = let
ports = {
base = 9001;
node = 9002;
nginx = 9003;
};
in {
enable = true;
port = ports.base;
exporters = {
node = {
enable = true;
enabledCollectors = [ "systemd" ];
port = ports.node;
};
nginx = {
enable = true;
port = ports.nginx;
};
};
scrapeConfigs = [
{
job_name = "nixos-server-reid";
static_configs = [{
targets = [
"127.0.0.1:${toString ports.node}"
"127.0.0.1:${toString ports.nginx}"
];
}];
}
];
};
loki = {
enable = true;
configFile = ./loki.yml;
};
nginx.statusPage = true;
nginx.virtualHosts."${cfg.domain}" = {
forceSSL = true;
enableACME = true;
locations."/" = {
proxyPass = "http://127.0.0.1:${toString cfg.port}";
proxyWebsockets = true;
};
};
};
};
}