nix-server/modules/services/metrics.nix
2024-08-02 19:56:19 -07:00

101 lines
2.2 KiB
Nix

{ config, lib, pkgs, options, ... }:
with lib;
let
cfg = config.modules.services.metrics;
in {
options.modules.services.metrics = {
enable = mkEnableOption "enable grafana with loki, prometheus, and promtail";
domain = mkOption {
type = types.str;
default = "grafana.reidlab.pink";
};
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-local-config.yml;
};
nginx.statusPage = true;
nginx.virtualHosts."${cfg.domain}" = {
forceSSL = true;
enableACME = true;
locations."/" = {
proxyPass = "http://127.0.0.1:${toString cfg.port}";
proxyWebsockets = true;
};
locations."= /robots.txt" ={
extraConfig = ''
add_header Content-Type text/plain;
return 200 "User-agent: *\nDisallow: /\n";
'';
};
};
};
};
}