Steve Losh avatar Steve Losh committed 33deb3b

Support metrics 2.0.1 and Clojure 1.3.0.

Comments (0)

Files changed (15)

 
 Add this to your `project.clj`'s dependencies:
 
-    [metrics-clojure "0.5.0"]
+    [metrics-clojure "0.6.0"]
 
 That's it.
 

docs/source/conf.py

 # built documents.
 #
 # The short X.Y version.
-version = '0.5.0'
+version = '0.6.0'
 # The full version, including alpha/beta/rc tags.
-release = '0.5.0'
+release = '0.6.0'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.

docs/source/installation.rst

 
 Add this to your ``project.clj``'s dependencies::
 
-    [metrics-clojure "0.1.0"]
+    [metrics-clojure "0.6.0"]
 
 That's it.

docs/source/reporting.rst

 Reporting
 =========
 
-Currently ``metrics-clojure`` only supports reporting metrics through the
+Currently ``metrics-clojure`` supports reporting metrics through the
 console (on standard error)::
 
     (use '[metrics.core :only (report-to-console)])
 This will tell ``metrics`` to print all metrics to the console every ten
 seconds.
 
+``metrics-clojure`` also supports [JMX reporting][jmx], since it's built into
+``metrics`` itself.
+
 More reporting methods will be added in the future.  Pull requests are welcome.
+
+[jmx]: http://metrics.codahale.com/manual.html#jmx-reporter
-(defproject metrics-clojure "0.5.0"
+(defproject metrics-clojure "0.6.0"
   :description "A Clojure façade for Coda Hale's metrics library."
-  :dependencies [[org.clojure/clojure "1.2.1"]
-                 [com.yammer.metrics/metrics-core "2.0.0-BETA17"]]
-  :repositories {"repo.codahale.com" "http://repo.codahale.com"})
+  :dependencies [[org.clojure/clojure "[1.2.1,1.3.0]"]
+                 [com.yammer.metrics/metrics-core "2.0.1"]]
+  :repositories {"repo.codahale.com" "http://repo.codahale.com"}
+  :warn-on-reflection true)

src/metrics/core.clj

 (ns metrics.core
-  (use [metrics.utils :only (metric-name)])
-  (import (com.yammer.metrics Metrics))
-  (import (com.yammer.metrics.reporting ConsoleReporter))
-  (import (java.util.concurrent TimeUnit)))
+  (:use [metrics.utils :only (metric-name)])
+  (:import (com.yammer.metrics Metrics))
+  (:import (com.yammer.metrics.reporting ConsoleReporter))
+  (:import (java.util.concurrent TimeUnit)))
 
 
 (defn remove-metric [title]
-  (Metrics/removeMetric (metric-name title)))
+  (.removeMetric (Metrics/defaultRegistry) (metric-name title)))
 
 
 (defn report-to-console [seconds]

src/metrics/counters.clj

 (ns metrics.counters
-  (use [metrics.utils :only (metric-name)])
-  (import (com.yammer.metrics Metrics))
-  (import (com.yammer.metrics.core CounterMetric)))
+  (:use [metrics.utils :only (metric-name)])
+  (:import (com.yammer.metrics Metrics))
+  (:import (com.yammer.metrics.core Counter)))
 
 
 ; Create ----------------------------------------------------------------------
 
 
 ; Read ------------------------------------------------------------------------
-(defn value [^CounterMetric c]
+(defn value [^Counter c]
   (.count c))
 
 
 ; Write -----------------------------------------------------------------------
 (defn inc!
-  ([^CounterMetric c] (inc! c 1))
-  ([^CounterMetric c n]
+  ([^Counter c] (inc! c 1))
+  ([^Counter c n]
    (.inc c n)
    c))
 
 (defn dec!
-  ([^CounterMetric c] (dec! c 1))
-  ([^CounterMetric c n]
+  ([^Counter c] (dec! c 1))
+  ([^Counter c n]
    (.dec c n)
    c))
 
-(defn clear! [^CounterMetric c]
+(defn clear! [^Counter c]
   (.clear c)
   c)
 

src/metrics/gauges.clj

 (ns metrics.gauges
-  (use [metrics.utils :only (metric-name)])
-  (import (com.yammer.metrics Metrics))
-  (import (com.yammer.metrics.core GaugeMetric)))
+  (:use [metrics.utils :only (metric-name)])
+  (:import (com.yammer.metrics Metrics))
+  (:import (com.yammer.metrics.core Gauge)))
 
 
 ; Create ----------------------------------------------------------------------
 (defmacro gauge [title & body]
   `(Metrics/newGauge (metric-name ~title)
-                     (proxy [GaugeMetric] []
+                     (proxy [Gauge] []
                        (value [] (do ~@body)))))
 
+
 ; Read ------------------------------------------------------------------------
 (defn gauge-fn [title f]
   (Metrics/newGauge (metric-name title)
-                    (proxy [GaugeMetric] []
+                    (proxy [Gauge] []
                       (value [] (f)))))
 
 
 ; Read ------------------------------------------------------------------------
-(defn value [^GaugeMetric g]
+(defn value [^Gauge g]
   (.value g))

src/metrics/histograms.clj

 (ns metrics.histograms
-  (use [metrics.utils :only (metric-name)])
+  (use [metrics.utils :only (metric-name get-percentiles)])
   (import (com.yammer.metrics Metrics))
-  (import (com.yammer.metrics.core HistogramMetric)))
+  (import (com.yammer.metrics.core Histogram MetricName)))
 
 
 ; Create ----------------------------------------------------------------------
 (defn histogram
   ([title] (histogram title true))
   ([title biased]
-   (Metrics/newHistogram (metric-name title) biased)))
+   (Metrics/newHistogram
+     ^MetricName (metric-name title)
+     (boolean biased))))
 
 
 ; Read ------------------------------------------------------------------------
-(defn mean [^HistogramMetric h]
+(defn mean [^Histogram h]
   (.mean h))
 
-(defn std-dev [^HistogramMetric h]
+(defn std-dev [^Histogram h]
   (.stdDev h))
 
 (defn percentiles
-  ([^HistogramMetric h]
+  ([^Histogram h]
    (percentiles h [0.75 0.95 0.99 0.999 1.0]))
-  ([^HistogramMetric h ps]
-   (zipmap ps
-           (.percentiles h (double-array ps)))))
+  ([^Histogram h ps]
+   (get-percentiles h ps)))
 
 
-(defn number-recorded [^HistogramMetric h]
+(defn number-recorded [^Histogram h]
   (.count h))
 
-(defn largest [^HistogramMetric h]
+(defn largest [^Histogram h]
   (.max h))
 
-(defn smallest [^HistogramMetric h]
+(defn smallest [^Histogram h]
   (.min h))
 
-(defn sample [^HistogramMetric h]
-  (.values h))
+(defn sample [^Histogram h]
+  (.getValues (.getSnapshot h)))
 
 
 ; Write -----------------------------------------------------------------------
-(defn update! [^HistogramMetric h n]
+(defn update! [^Histogram h n]
   (.update h (long n))
   h)
 
-(defn clear! [^HistogramMetric h]
+(defn clear! [^Histogram h]
   (.clear h)
   h)
 

src/metrics/meters.clj

 (ns metrics.meters
   (use [metrics.utils :only (metric-name)])
   (import (com.yammer.metrics Metrics))
-  (import (com.yammer.metrics.core MeterMetric))
+  (import (com.yammer.metrics.core Meter))
   (import (java.util.concurrent TimeUnit)))
 
 
 
 
 ; Read ------------------------------------------------------------------------
-(defn rates [^MeterMetric m]
+(defn rates [^Meter m]
   {1 (.oneMinuteRate m)
    5 (.fiveMinuteRate m)
    15 (.fifteenMinuteRate m)})
 
-(defn rate-one [^MeterMetric m]
+(defn rate-one [^Meter m]
   (.oneMinuteRate m))
 
-(defn rate-five [^MeterMetric m]
+(defn rate-five [^Meter m]
   (.fiveMinuteRate m))
 
-(defn rate-fifteen [^MeterMetric m]
+(defn rate-fifteen [^Meter m]
   (.fifteenMinuteRate m))
 
-(defn rate-mean [^MeterMetric m]
+(defn rate-mean [^Meter m]
   (.meanRate m))
 
 
 ; Write -----------------------------------------------------------------------
 (defn mark!
-  ([^MeterMetric m]
+  ([^Meter m]
    (mark! m 1))
-  ([^MeterMetric m n]
+  ([^Meter m n]
    (.mark m (long n))
    m))

src/metrics/timers.clj

 (ns metrics.timers
-  (use [metrics.utils :only (metric-name)])
+  (use [metrics.utils :only (metric-name get-percentiles)])
   (import (com.yammer.metrics Metrics))
-  (import (com.yammer.metrics.core TimerMetric))
+  (import (com.yammer.metrics.core Timer MetricName))
   (import (java.util.concurrent TimeUnit)))
 
 
 ; Create ----------------------------------------------------------------------
 (defn timer [title]
-  (Metrics/newTimer (metric-name title)
+  (Metrics/newTimer ^MetricName (metric-name title)
                     TimeUnit/MILLISECONDS
                     TimeUnit/SECONDS))
 
 
 ; Read ------------------------------------------------------------------------
-(defn rates [^TimerMetric m]
+(defn rates [^Timer m]
   {1 (.oneMinuteRate m)
    5 (.fiveMinuteRate m)
    15 (.fifteenMinuteRate m)})
 
-(defn rate-one [^TimerMetric m]
+(defn rate-one [^Timer m]
   (.oneMinuteRate m))
 
-(defn rate-five [^TimerMetric m]
+(defn rate-five [^Timer m]
   (.fiveMinuteRate m))
 
-(defn rate-fifteen [^TimerMetric m]
+(defn rate-fifteen [^Timer m]
   (.fifteenMinuteRate m))
 
-(defn rate-mean [^TimerMetric m]
+(defn rate-mean [^Timer m]
   (.meanRate m))
 
 
-(defn mean [^TimerMetric t]
+(defn mean [^Timer t]
   (.mean t))
 
-(defn std-dev [^TimerMetric t]
+(defn std-dev [^Timer t]
   (.stdDev t))
 
 (defn percentiles
-  ([^TimerMetric t]
-   (percentiles t [0.75 0.90 0.95 1.0]))
-  ([^TimerMetric t ps]
-   (zipmap ps
-           (.percentiles t (double-array ps)))))
+  ([^Timer t]
+   (percentiles t [0.75 0.95 0.99 0.999 1.0]))
+  ([^Timer t ps]
+   (get-percentiles t ps)))
 
 
-(defn number-recorded [^TimerMetric t]
+(defn number-recorded [^Timer t]
   (.count t))
 
-(defn largest [^TimerMetric t]
+(defn largest [^Timer t]
   (.max t))
 
-(defn smallest [^TimerMetric t]
+(defn smallest [^Timer t]
   (.min t))
 
-(defn sample [^TimerMetric t]
-  (.values t))
+(defn sample [^Timer t]
+  (.getValues (.getSnapshot t)))
 
 
 ; Write -----------------------------------------------------------------------
-(defmacro time! [t & body]
-  `(.time ~t
+(defmacro time! [^Timer t & body]
+  `(.time ~(vary-meta t assoc :tag `Timer)
          (proxy [Callable] []
            (call [] (do ~@body)))))
 
-(defn time-fn! [t f]
+(defn time-fn! [^Timer t f]
   (.time t
          (proxy [Callable] []
            (call [] (f)))))
 
-(defn clear! [^TimerMetric t]
+(defn clear! [^Timer t]
   (.clear t))

src/metrics/utils.clj

 (ns metrics.utils
-  (import (com.yammer.metrics.core MetricName)))
+  (import (com.yammer.metrics.core Sampling MetricName)))
 
 
-(defn metric-name [title]
+(defn get-percentile [^Sampling metric ^double percentile]
+  (-> metric
+    .getSnapshot
+    (.getValue percentile)))
+
+(defn get-percentiles [metric percentiles]
+  (zipmap percentiles
+           (map (partial get-percentile metric)
+                percentiles)))
+
+(defn metric-name ^MetricName [title]
   (if (string? title)
-    (new MetricName "default" "default" title)
-    (new MetricName (first title) (second title) (last title))))
+    (new MetricName "default" "default" ^String title)
+    (new MetricName
+         ^String (first title)
+         ^String (second title)
+         ^String (last title))))

test/metrics/test/histograms.clj

 
 (deftest test-mean
   (let [h (histograms/histogram ["test" "histograms" "test-mean"])]
-    (is (= (histograms/mean h) 0))
+    (is (= (histograms/mean h) 0.0))
     (histograms/update! h 5)
-    (is (= (histograms/mean h) 5))
+    (is (= (histograms/mean h) 5.0))
     (histograms/update! h 15)
-    (is (= (histograms/mean h) 10))))
+    (is (= (histograms/mean h) 10.0))))
 
 (deftest test-number-recorded
   (let [h (histograms/histogram ["test" "histograms" "test-number-recorded"])]
 
 (deftest test-std-dev
   (let [h (histograms/histogram ["test" "histograms" "test-std-dev"])]
-    (is (= (histograms/std-dev h) 0))
+    (is (= (histograms/std-dev h) 0.0))
     (histograms/update! h 2)
     (histograms/update! h 4)
     (histograms/update! h 6)
-    (is (= (histograms/std-dev h) 2))))
+    (is (= (histograms/std-dev h) 2.0))))
 
 (deftest test-smallest
   (let [h (histograms/histogram ["test" "histograms" "test-smallest"])]
-    (is (= (histograms/smallest h) 0))
+    (is (= (histograms/smallest h) 0.0))
     (histograms/update! h 1)
     (histograms/update! h 9)
     (histograms/update! h 3)
-    (is (= (histograms/smallest h) 1))))
+    (is (= (histograms/smallest h) 1.0))))
 
 (deftest test-largest
   (let [h (histograms/histogram ["test" "histograms" "test-largest"])]
-    (is (= (histograms/largest h) 0))
+    (is (= (histograms/largest h) 0.0))
     (histograms/update! h 1)
     (histograms/update! h 9)
     (histograms/update! h 3)
-    (is (= (histograms/largest h) 9))))
+    (is (= (histograms/largest h) 9.0))))
 
 (deftest test-percentiles
   (let [h (histograms/histogram ["test" "histograms" "test-percentiles"])]
 
 (deftest test-clear
   (let [h (histograms/histogram ["test" "histograms" "test-clear"])]
-    (is (= (histograms/mean h) 0))
+    (is (= (histograms/mean h) 0.0))
     (histograms/update! h 5)
-    (is (= (histograms/mean h) 5))
+    (is (= (histograms/mean h) 5.0))
     (histograms/clear! h)
-    (is (= (histograms/mean h) 0))))
+    (is (= (histograms/mean h) 0.0))))
 

test/metrics/test/meters.clj

 
 (deftest test-rate-mean
   (let [m (meters/meter ["test" "meters" "test-rate-mean"] "test-events")]
-    (is (= (meters/rate-mean m) 0))
+    (is (= (meters/rate-mean m) 0.0))
     (meters/mark! m)
-    (is (> (meters/rate-mean m) 0))))
+    (is (> (meters/rate-mean m) 0.0))))
 
 (deftest test-rate-mean-update-multiple
   (let [m (meters/meter ["test" "meters" "test-rate-mean-update-multiple"] "test-events")]
-    (is (= (meters/rate-mean m) 0))
+    (is (= (meters/rate-mean m) 0.0))
     (meters/mark! m 10)
-    (is (> (meters/rate-mean m) 0))))
+    (is (> (meters/rate-mean m) 0.0))))
 
 (deftest test-rate-one
   (let [m (meters/meter ["test" "meters" "test-rate-one"] "test-events")]
-    (is (= (meters/rate-one m) 0))
+    (is (= (meters/rate-one m) 0.0))
     (meters/mark! m 20000)
     (Thread/sleep 8000)
-    (is (> (meters/rate-one m) 0))))
+    (is (> (meters/rate-one m) 0.0))))
 
 (deftest test-rate-five
   (let [m (meters/meter ["test" "meters" "test-rate-five"] "test-events")]
-    (is (= (meters/rate-five m) 0))
+    (is (= (meters/rate-five m) 0.0))
     (meters/mark! m 20000)
     (Thread/sleep 8000)
-    (is (> (meters/rate-five m) 0))))
+    (is (> (meters/rate-five m) 0.0))))
 
 (deftest test-rate-fifteen
   (let [m (meters/meter ["test" "meters" "test-rate-fifteen"] "test-events")]
-    (is (= (meters/rate-fifteen m) 0))
+    (is (= (meters/rate-fifteen m) 0.0))
     (meters/mark! m 20000)
     (Thread/sleep 8000)
-    (is (> (meters/rate-fifteen m) 0))))
+    (is (> (meters/rate-fifteen m) 0.0))))
 
 (deftest test-rates
   (let [m (meters/meter ["test" "meters" "test-rates"] "test-events")]
     (is (every? zero? (vals (meters/rates m))))
     (meters/mark! m 20000)
     (Thread/sleep 8000)
-    (is (every? #(> % 0) (vals (meters/rates m))))))
+    (is (every? #(> % 0.0) (vals (meters/rates m))))))

test/metrics/test/timers.clj

 
 (deftest test-rate-mean
   (let [t (timers/timer ["test" "timers" "test-rate-mean"])]
-    (is (= (timers/rate-mean t) 0))
+    (is (= (timers/rate-mean t) 0.0))
     (is (= (timers/time! t (sleep-100)) 100))
-    (is (> (timers/rate-mean t) 0))))
+    (is (> (timers/rate-mean t) 0.0))))
 
 (deftest test-rate-mean-fn
   (let [t (timers/timer ["test" "timers" "test-rate-mean-fn"])]
-    (is (= (timers/rate-mean t) 0))
+    (is (= (timers/rate-mean t) 0.0))
     (is (= (timers/time-fn! t sleep-100) 100))
-    (is (> (timers/rate-mean t) 0))))
+    (is (> (timers/rate-mean t) 0.0))))
 
 (deftest test-rate-one
   (let [t (timers/timer ["test" "timers" "test-rate-one"])]
-    (is (= (timers/rate-one t) 0))
+    (is (= (timers/rate-one t) 0.0))
     (is (= (timers/time! t (sleep-100)) 100))
     (Thread/sleep 8000)
-    (is (> (timers/rate-one t) 0))))
+    (is (> (timers/rate-one t) 0.0))))
 
 (deftest test-rate-five
   (let [t (timers/timer ["test" "timers" "test-rate-five"])]
-    (is (= (timers/rate-five t) 0))
+    (is (= (timers/rate-five t) 0.0))
     (is (= (timers/time! t (sleep-100)) 100))
     (Thread/sleep 8000)
-    (is (> (timers/rate-five t) 0))))
+    (is (> (timers/rate-five t) 0.0))))
 
 (deftest test-rate-fifteen
   (let [t (timers/timer ["test" "timers" "test-rate-fifteen"])]
-    (is (= (timers/rate-fifteen t) 0))
+    (is (= (timers/rate-fifteen t) 0.0))
     (is (= (timers/time! t (sleep-100)) 100))
     (Thread/sleep 8000)
-    (is (> (timers/rate-fifteen t) 0))))
+    (is (> (timers/rate-fifteen t) 0.0))))
 
 
 (deftest test-largest
   (let [t (timers/timer ["test" "timers" "test-largest"])]
-    (is (= (timers/largest t) 0))
+    (is (= (timers/largest t) 0.0))
     (is (= (timers/time! t (sleep-100)) 100))
-    (is (within-ten (timers/largest t) 100))
+    (is (within-ten (timers/largest t) 100.0))
     (is (= (timers/time! t (sleep-200)) 200))
-    (is (within-ten (timers/largest t) 200))))
+    (is (within-ten (timers/largest t) 200.0))))
+
 (deftest test-smallest
   (let [t (timers/timer ["test" "timers" "test-smallest"])]
-    (is (= (timers/smallest t) 0))
+    (is (= (timers/smallest t) 0.0))
     (is (= (timers/time! t (sleep-100)) 100))
-    (is (within-ten (timers/smallest t) 100))
+    (is (within-ten (timers/smallest t) 100.0))
     (is (= (timers/time! t (sleep-200)) 200))
-    (is (within-ten (timers/smallest t) 100))))
+    (is (within-ten (timers/smallest t) 100.0))))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.