Add a couple of procedures for generating histogram buckets

This commit is contained in:
Christopher Baines 2020-12-10 19:50:02 +00:00
parent 0b22a8760c
commit 35dc26c0ea

View file

@ -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)