about summary refs log tree commit diff
path: root/arunisaac/prometheus.scm
blob: 9f2cef16ca1be144fc84a54ae30bdc5fd7d02a0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
;;; guix-arunisaac --- arunisaac's Guix odds and ends
;;; Copyright © 2020 Christopher Baines <mail@cbaines.net>
;;; Copyright © 2026 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; 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
;;; <https://www.gnu.org/licenses/>.

(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>
  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>
  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>
  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 <prometheus-scrape-config>
    (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 <prometheus-static-config>
                                    (targets labels)
                                    #~(cons (cons "targets" (vector #$@targets))
                                            #$(match labels
                                                (() #~'())
                                                (_ #~(cons "labels" #$label)))))
                                  static-configs))))))

(define prometheus-config-file-gexp
  (match-record-lambda <prometheus-configuration>
    (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 <prometheus-configuration>
    (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))))