-
Notifications
You must be signed in to change notification settings - Fork 80
/
gauges.clj
61 lines (50 loc) · 1.9 KB
/
gauges.clj
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
(ns metrics.gauges
(:require [metrics.core :refer [default-registry metric-name]]
[metrics.utils :refer [desugared-title]])
(:import [com.codahale.metrics MetricRegistry MetricFilter Gauge]
clojure.lang.IFn))
(defn gauge-fn
"Create a new Gauge metric with the given title.
The given function will be called (with no arguments) to retrieve the value of
the Gauge when requested."
([title ^IFn f]
(gauge-fn default-registry title f))
([^MetricRegistry reg title ^IFn f]
(let [g (reify Gauge
(getValue [this]
(f)))
s (metric-name title)]
(.remove reg s)
(.register reg s g))))
(defn gauge
"Retrieve an existing gauge from the provided registry (or the
default registry) from its name. Returns nil when not found."
([title]
(gauge default-registry title))
([^MetricRegistry reg title]
(when-let [matches (seq (.getGauges reg (reify MetricFilter
(matches [this name _]
(= name (metric-name title))))))]
(val (first matches)))))
(defmacro defgauge
"Define a new Gauge metric with the given title and a function.
to call to retrieve the value of the Gauge.
The title uses some basic desugaring to let you concisely define metrics:
; Define a gauge titled \"default.default.foo\" into var foo
(defgauge foo ,,,)
(defgauge \"foo\" ,,,)
; Define a gauge titled \"a.b.c\" into var c
(defgauge [a b c] ,,,)
(defgauge [\"a\" \"b\" \"c\"] ,,,)
(defgauge [a \"b\" c] ,,,)
"
([title ^clojure.lang.IFn f]
(let [[s title] (desugared-title title)]
`(def ~s (gauge-fn '~title ~f))))
([^MetricRegistry reg title ^clojure.lang.IFn f]
(let [[s title] (desugared-title title)]
`(def ~s (gauge-fn ~reg '~title ~f)))))
(defn value
"Return the value of the given Gauge."
[^Gauge g]
(.getValue g))