From 35dc26c0ea44c3d70f1819f240d84e2cbb4b7b4c Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Thu, 10 Dec 2020 19:50:02 +0000 Subject: Add a couple of procedures for generating histogram buckets --- prometheus.scm | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/prometheus.scm b/prometheus.scm index 2a181f8..9d8d7ad 100644 --- a/prometheus.scm +++ b/prometheus.scm @@ -164,6 +164,47 @@ list of label names to be permitted for this metric and ;; The default buckets used in other client libraries (list 0.005 0.01 0.025 0.05 0.1 0.25 0.5 1 2.5 5 10 (inf))) +(define* (linear-histogram-buckets #:key start step end count) + (when (and end count) + (raise-exception + (make-exception-with-message + "you can only specify either end or count to linear-histogram-buckets"))) + + (append (if count + (map (lambda (index) + (+ start + (* step index))) + (iota count)) + (let loop ((reverse-result (list start)) + (current-value start)) + (let ((next-value (+ current-value step))) + (if (>= next-value end) + (reverse (cons end reverse-result)) + (loop (cons next-value reverse-result) + next-value))))) + (list (inf)))) + +(define* (exponential-histogram-buckets #:key start (factor 2) end count) + (when (and end count) + (raise-exception + (make-exception-with-message + "you can only specify either end or count to exponential-histogram-buckets"))) + + (append (if count + (map (lambda (index) + (* start + (expt factor index))) + (iota count)) + (let loop ((reverse-result (list start))) + (let ((next-value + (* start + (expt factor + (+ 1 (length reverse-result)))))) + (if (>= next-value end) + (reverse (cons end reverse-result)) + (loop (cons next-value reverse-result)))))) + (list (inf)))) + (define* (make-histogram-metric registry name #:key (buckets %default-histogram-buckets) -- cgit v1.2.3