From 29555fda8b5a3117d6e0bc8a6edaf7892dac1b2c Mon Sep 17 00:00:00 2001 From: Adithya Nair Date: Sat, 14 Sep 2024 19:36:59 +0530 Subject: [PATCH] all: reformat caddy logs --- hosts/layne/services/apps/jellyfin.nix | 8 +++- hosts/layne/services/apps/transmission.nix | 7 +++- hosts/shared/caddy-helpers.nix | 11 +++++ hosts/shared/caddy.nix | 8 +++- hosts/shared/prometheus-exporters.nix | 49 +++++++++++++--------- 5 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 hosts/shared/caddy-helpers.nix diff --git a/hosts/layne/services/apps/jellyfin.nix b/hosts/layne/services/apps/jellyfin.nix index 1f96910..82c9d96 100644 --- a/hosts/layne/services/apps/jellyfin.nix +++ b/hosts/layne/services/apps/jellyfin.nix @@ -1,13 +1,19 @@ -_: { +_: +let + inherit (import ../../../shared/caddy-helpers.nix) logFormat; +in +{ services = { caddy = { virtualHosts = { "jellyfin.local.adtya.xyz" = { + logFormat = logFormat "jellyfin.local.adtya.xyz"; extraConfig = '' reverse_proxy 127.0.0.1:8096 ''; }; "jellyfin.labs.adtya.xyz" = { + logFormat = logFormat "jellyfin.labs.adtya.xyz"; extraConfig = '' reverse_proxy 127.0.0.1:8096 ''; diff --git a/hosts/layne/services/apps/transmission.nix b/hosts/layne/services/apps/transmission.nix index 4a14d1d..f61ff3b 100644 --- a/hosts/layne/services/apps/transmission.nix +++ b/hosts/layne/services/apps/transmission.nix @@ -1,7 +1,12 @@ -{ pkgs, ... }: { +{ pkgs, ... }: +let + inherit (import ../../../shared/caddy-helpers.nix) logFormat; +in +{ services = { caddy = { virtualHosts."transmission.labs.adtya.xyz" = { + logFormat = logFormat "transmission.labs.adtya.xyz"; extraConfig = '' reverse_proxy 127.0.0.1:9091 ''; diff --git a/hosts/shared/caddy-helpers.nix b/hosts/shared/caddy-helpers.nix new file mode 100644 index 0000000..1046238 --- /dev/null +++ b/hosts/shared/caddy-helpers.nix @@ -0,0 +1,11 @@ +{ + logFormat = fileName: '' + output file /var/log/caddy/${fileName}.log { + roll_size 100MiB + roll_keep 5 + roll_keep_for 100d + } + format json + level INFO + ''; +} diff --git a/hosts/shared/caddy.nix b/hosts/shared/caddy.nix index d790ba6..0ee9371 100644 --- a/hosts/shared/caddy.nix +++ b/hosts/shared/caddy.nix @@ -1,4 +1,8 @@ -{ config, inputs, pkgs, ... }: { +{ config, inputs, pkgs, ... }: +let + inherit (import ./caddy-helpers.nix) logFormat; +in +{ sops = { secrets = { "caddy/env_file" = { @@ -15,7 +19,7 @@ globalConfig = '' acme_dns digitalocean {env.DO_API_TOKEN} ''; - logFormat = "level INFO"; + logFormat = logFormat "caddy_main"; }; systemd.services.caddy.serviceConfig.EnvironmentFile = config.sops.secrets."caddy/env_file".path; networking.firewall.allowedTCPPorts = [ 80 443 ]; diff --git a/hosts/shared/prometheus-exporters.nix b/hosts/shared/prometheus-exporters.nix index 0a46f2f..3741c6c 100644 --- a/hosts/shared/prometheus-exporters.nix +++ b/hosts/shared/prometheus-exporters.nix @@ -1,25 +1,34 @@ -{ lib, config, ... }: { +{ lib, config, ... }: +let + inherit (import ./caddy-helpers.nix) logFormat; +in +{ services = { - caddy = { - virtualHosts."${config.networking.hostName}.labs.adtya.xyz" = { - extraConfig = '' - metrics /caddy-metrics - handle /metrics { - reverse_proxy 127.0.0.1:9100 - } - handle /systemd-metrics { - uri replace /systemd-metrics /metrics - reverse_proxy 127.0.0.1:9558 - } - ${lib.optionalString config.services.prometheus.exporters.postgres.enable '' - handle /postgres-metrics { - uri replace /postgres-metrics /metrics - reverse_proxy ${config.services.prometheus.exporters.postgres.listenAddress}:${toString config.services.prometheus.exporters.postgres.port} - } - ''} - ''; + caddy = + let + vHost = "${config.networking.hostName}.labs.adtya.xyz"; + in + { + virtualHosts."${vHost}" = { + logFormat = logFormat vHost; + extraConfig = '' + metrics /caddy-metrics + handle /metrics { + reverse_proxy 127.0.0.1:9100 + } + handle /systemd-metrics { + uri replace /systemd-metrics /metrics + reverse_proxy 127.0.0.1:9558 + } + ${lib.optionalString config.services.prometheus.exporters.postgres.enable '' + handle /postgres-metrics { + uri replace /postgres-metrics /metrics + reverse_proxy ${config.services.prometheus.exporters.postgres.listenAddress}:${toString config.services.prometheus.exporters.postgres.port} + } + ''} + ''; + }; }; - }; prometheus.exporters = { node = { enable = true;