;;; guix-arunisaac --- arunisaac's Guix odds and ends ;;; Copyright © 2020 Christopher Baines ;;; Copyright © 2026 Arun Isaac ;;; ;;; This file is part of guix-arunisaac. ;;; ;;; guix-arunisaac is free software: you can redistribute it and/or ;;; modify it under the terms of the GNU General Public License as ;;; published by the Free Software Foundation, either version 3 of the ;;; License, or (at your option) any later version. ;;; ;;; guix-arunisaac is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License ;;; along with guix-arunisaac. If not, see ;;; . (define-module (arunisaac prometheus) #:use-module (gnu build linux-container) #:use-module ((gnu packages admin) #:select (shadow)) #:use-module ((gnu packages guile) #:select (guile-json-4)) #:use-module (gnu services) #:use-module (gnu services shepherd) #:use-module (gnu system file-systems) #:use-module (gnu system shadow) #:use-module (guix build-system go) #:use-module (guix gexp) #:use-module (guix git-download) #:use-module (guix least-authority) #:use-module ((guix licenses) #:prefix license:) #:use-module (guix packages) #:use-module (guix records) #:use-module (ice-9 match) #:export (prometheus-configuration prometheus-configuration? this-prometheus-configuration prometheus-configuration-package prometheus-configuration-web-listen-address prometheus-configuration-storage-tsdb-path prometheus-configuration-scrape-configs prometheus-scrape-config prometheus-scrape-config? prometheus-scrape-config-job-name prometheus-scrape-config-scrape-interval prometheus-scrape-config-static-configs prometheus-static-config prometheus-static-config? prometheus-static-config-targets prometheus-static-config-labels prometheus-service-type)) ;; This package definition is based on ;; https://codeberg.org/cbaines/guix/commit/9cb1166151b795ba6aaaf9db11e31cfc3f124c1d (define-public prometheus (package (name "prometheus") (version "2.17.1") (source (origin (method git-fetch) (uri (git-reference (url "https://github.com/prometheus/prometheus.git") (commit (string-append "v" version)))) (file-name (git-file-name name version)) (sha256 (base32 "1r7zpq6647lrm7cmid6nnf2xnljqh1i9g0fxvs0qrfd2sxxgj0c7")))) (build-system go-build-system) (arguments (list #:unpack-path "github.com/prometheus/prometheus" #:import-path "github.com/prometheus/prometheus/cmd/prometheus" #:install-source? #f #:phases #~(modify-phases %standard-phases (add-after 'unpack 'patch (lambda* (#:key outputs #:allow-other-keys) (let ((assets-prefix (string-append (assoc-ref outputs "out") "/var/lib/prometheus/assets"))) (substitute* "src/github.com/prometheus/prometheus/web/ui/ui.go" (("var assetsPrefix string") (string-append "var assetsPrefix string = \"" assets-prefix "\"")))))) (add-after 'install 'install-assets (lambda* (#:key outputs #:allow-other-keys) (let ((assets-prefix (string-append (assoc-ref outputs "out") "/var/lib/prometheus/assets"))) (for-each (lambda (directory) (copy-recursively (string-append "src/github.com/prometheus/prometheus" "/web/ui/" directory) (string-append assets-prefix "/" directory))) (list "static" "templates")))))))) (home-page "https://prometheus.io/") (synopsis "Metrics, monitoring and alerting toolkit") (description "Prometheus is an open-source systems monitoring and alerting toolkit. Prometheus collects and stores its metrics as time series data, i.e. metrics information is stored with the timestamp at which it was recorded, alongside optional key-value pairs called labels.") (license license:asl2.0))) (define-record-type* prometheus-configuration make-prometheus-configuration prometheus-configuration? this-prometheus-configuration (package prometheus-configuration-package (default prometheus)) (web-listen-address prometheus-configuration-web-listen-address (default "localhost:9090")) (storage-tsdb-path prometheus-configuration-storage-tsdb-path (default "/var/lib/prometheus/data/")) (scrape-configs prometheus-configuration-scrape-configs (default '()) ;; thunked so that scrape configs can reference the web listen ;; address (thunked))) (define-record-type* prometheus-scrape-config make-prometheus-scrape-config prometheus-scrape-config? (job-name prometheus-scrape-config-job-name) (scrape-interval prometheus-scrape-config-scrape-interval (default "1m")) (static-configs prometheus-scrape-config-static-configs (default '()))) (define-record-type* prometheus-static-config make-prometheus-static-config prometheus-static-config? (targets prometheus-static-config-targets (default '())) (labels prometheus-static-config-labels (default '()))) (define %prometheus-accounts (list (user-account (name "prometheus") (group "prometheus") (system? #t) (comment "Prometheus daemon user") (home-directory "/var/lib/prometheus") (shell (file-append shadow "/sbin/nologin"))) (user-group (name "prometheus") (system? #t)))) (define scrape-config->tree (match-record-lambda (job-name scrape-interval static-configs) ;; Vectors and dotted association lists cannot be lowered correctly into a ;; G-expression. Hence we return a G-expression that evaluates to such a ;; tree. #~(list (cons "job_name" #$job-name) (cons "scrape_interval" #$scrape-interval) (cons "static_configs" (vector #$@(map (match-record-lambda (targets labels) #~(cons (cons "targets" (vector #$@targets)) #$(match labels (() #~'()) (_ #~(list (cons "labels" (list #$@(map (match-lambda ((key . value) #~(cons #$key #$value))) labels)))))))) static-configs)))))) (define prometheus-config-file-gexp (match-record-lambda (scrape-configs) (with-extensions (list guile-json-4) #~(begin (use-modules (srfi srfi-26) (json)) (call-with-output-file #$output ;; YAML is a superset of JSON. So, we can just write JSON. (cut scm->json ;; Vectors and dotted association lists cannot be lowered ;; correctly into a G-expression. Hence we deal in ;; G-expressions that evaluate to such a tree. (list (cons "scrape_configs" (vector #$@(map scrape-config->tree scrape-configs)))) <>)))))) (define (prometheus-shepherd-service config) (match-record config (package web-listen-address storage-tsdb-path) (let ((config-file (computed-file "prometheus.yml" (prometheus-config-file-gexp config)))) (shepherd-service (documentation "Prometheus monitoring system and time series database.") (provision '(prometheus)) (requirement '(networking)) (start #~(make-forkexec-constructor (list #$(least-authority-wrapper (file-append package "/bin/prometheus") #:name "prometheus-pola-wrapper" #:mappings (list (file-system-mapping (source config-file) (target source)) (file-system-mapping (source "/var/lib/prometheus") (target source) (writable? #t))) #:namespaces (delq 'net %namespaces)) "--config.file" #$config-file "--web.listen-address" #$web-listen-address "--storage.tsdb.path" #$storage-tsdb-path) #:user "prometheus" #:group "prometheus" #:log-file "/var/log/prometheus.log")) (stop #~(make-kill-destructor)))))) (define prometheus-service-type (service-type (name 'prometheus) (description "Run Prometheus monitoring server.") (extensions (list (service-extension account-service-type (const %prometheus-accounts)) (service-extension shepherd-root-service-type (compose list prometheus-shepherd-service)))) (default-value (prometheus-configuration))))