aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2020-11-29 09:09:21 +0000
committerChristopher Baines <mail@cbaines.net>2020-11-29 09:09:21 +0000
commit12d3d9de676f518eccf0a384d461cc4c035939b0 (patch)
tree6d50e2d738296b5d37db57d33bf5c0a4c3044a31
parent81d8dda11862b9c2243cc88f21cce89e2e2ec840 (diff)
downloadprometheus-12d3d9de676f518eccf0a384d461cc4c035939b0.tar
prometheus-12d3d9de676f518eccf0a384d461cc4c035939b0.tar.gz
Help with writing textfiles
-rw-r--r--prometheus.scm22
1 files changed, 22 insertions, 0 deletions
diff --git a/prometheus.scm b/prometheus.scm
index 68336cf..0d44377 100644
--- a/prometheus.scm
+++ b/prometheus.scm
@@ -27,6 +27,7 @@
#:export (make-metrics-registry
metrics-registry-fetch-metric
write-metrics
+ write-textfile
make-counter-metric
make-gauge-metric
@@ -409,3 +410,24 @@ so that it can receive and store the metric values."
value))
(metric-values metric))))
(metrics-registry-metrics-hash registry)))
+
+(define (write-textfile registry filename)
+ "Write all metrics from the given @var{registry} to @var{filename}
+in the standard text based exposition format.
+
+For the node exporter to read the file, the @var{filename} must end
+with .prom.
+
+This procedure takes care of atomically replacing the file."
+ (let* ((template (string-append filename ".XXXXXX"))
+ (out (mkstemp! template)))
+ (with-throw-handler #t
+ (lambda ()
+ (chmod out(logand #o666 (lognot (umask))))
+ (write-metrics registry out)
+ (close out)
+ (rename-file template filename)
+ #t)
+ (lambda (key . args)
+ (false-if-exception (delete-file template))))))
+