Commits

Ian Eure committed 27c731b

Add `defcounter`, `defgauge`, `defhistogram`, `defmeter`, and `deftimer` macros.

  • Participants
  • Parent commits da1a3a2

Comments (0)

Files changed (5)

metrics-clojure-core/src/metrics/counters.clj

   [title]
   (Metrics/newCounter (metric-name title)))
 
+(defmacro defcounter
+  "Define a new Counter metric with a givne title, or group, type and title."
+  ([title]
+     `(def ~title (counter ~(str title))))
+
+  ([group type title]
+     `(def ~title (counter ~(vec (map str [group type title]))))))
+
 
 ; Read ------------------------------------------------------------------------
 (defn value

metrics-clojure-core/src/metrics/gauges.clj

                      (proxy [Gauge] []
                        (value [] (do ~@body)))))
 
-
-; Read ------------------------------------------------------------------------
 (defn gauge-fn
   "Create a new Gauge metric with the given title.
 
                     (proxy [Gauge] []
                       (value [] (f)))))
 
+(defmacro defgauge [title & body]
+  "Define a new Gauge metric with the given title.
+
+   The rest of the arguments may be a body form or function to call to
+   retrieve the value of the Gauge"
+  (cond (and (= 1 (count body)) (and (symbol? (first body))
+                                     (fn? (eval (first body)))))
+        `(gauge-fn ~(str title) ~(first body))
+        true
+        `(gauge ~(str title) ~@body)))
+
 
 ; Read ------------------------------------------------------------------------
 (defn value

metrics-clojure-core/src/metrics/histograms.clj

      ^MetricName (metric-name title)
      (boolean biased))))
 
+(defmacro defhistogram [title & args]
+  "Define a Histogram metric with the given title."
+  `(def ~title
+     (histogram ~@(cons (str title) args))))
+
 
 ; Read ------------------------------------------------------------------------
 (defn mean

metrics-clojure-core/src/metrics/meters.clj

                     event-type
                     TimeUnit/SECONDS))
 
+(defmacro defmeter [title event-type]
+  `(def ~title (meter ~(str title) ~event-type)))
+
 
 ; Read ------------------------------------------------------------------------
 (defn rates [^Meter m]

metrics-clojure-core/src/metrics/timers.clj

                     TimeUnit/MILLISECONDS
                     TimeUnit/SECONDS))
 
+(defmacro deftimer [title]
+  `(def ~title (timer ~(str title))))
+
 
 ; Read ------------------------------------------------------------------------
 (defn rates [^Timer m]