Commits

Steve Losh committed 0bf0879

Initial commit.

  • Participants

Comments (0)

Files changed (7)

+syntax:glob
+/pom.xml
+*jar
+/lib
+/classes
+/native
+/.lein-failures
+/checkouts
+/.lein-deps-sum
+.lein-deps-sum
+# metrics-clojure-ring
+
+Glues together metrics-clojure and ring.
+
+Documentation is included in the [metrics-clojure docs](http://metrics-clojure.rtfd.org/).
+<?xml version="1.0" encoding="UTF-8"?>
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>metrics-clojure-ring</groupId>
+  <artifactId>metrics-clojure-ring</artifactId>
+  <version>0.6.0</version>
+  <name>metrics-clojure-ring</name>
+  <description>Various things gluing together metrics-clojure and ring.</description>
+  <build>
+    <sourceDirectory>src</sourceDirectory>
+    <testSourceDirectory>test</testSourceDirectory>
+    <resources>
+      <resource>
+        <directory>resources</directory>
+      </resource>
+    </resources>
+    <testResources>
+      <testResource>
+        <directory>test-resources</directory>
+      </testResource>
+    </testResources>
+  </build>
+  <repositories>
+    <repository>
+      <id>central</id>
+      <url>http://repo1.maven.org/maven2</url>
+    </repository>
+    <repository>
+      <id>clojars</id>
+      <url>http://clojars.org/repo/</url>
+    </repository>
+  </repositories>
+  <dependencies>
+    <dependency>
+      <groupId>ring</groupId>
+      <artifactId>ring-core</artifactId>
+      <version>1.0.2</version>
+    </dependency>
+    <dependency>
+      <groupId>cheshire</groupId>
+      <artifactId>cheshire</artifactId>
+      <version>2.2.2</version>
+    </dependency>
+    <dependency>
+      <groupId>metrics-clojure</groupId>
+      <artifactId>metrics-clojure</artifactId>
+      <version>0.6.0</version>
+    </dependency>
+  </dependencies>
+</project>
+
+<!-- This file was autogenerated by Leiningen.
+  Please do not edit it directly; instead edit project.clj and regenerate it.
+  It should not be considered canonical data. For more information see
+  https://github.com/technomancy/leiningen -->
+(defproject metrics-clojure-ring "0.7.0"
+  :description "Various things gluing together metrics-clojure and ring."
+  :dependencies [[cheshire "2.2.2"]
+                 [metrics-clojure "0.7.0"]])

src/metrics/ring/expose.clj

+(ns metrics.ring.expose
+  (:import (com.yammer.metrics.core Gauge Timer Counter Histogram Meter))
+  (:require [metrics.gauges :as gauges])
+  (:require [metrics.meters :as meters])
+  (:require [metrics.histograms :as histograms])
+  (:require [metrics.counters :as counters])
+  (:require [metrics.timers :as timers])
+  (:use [ring.util.response :only [header response]])
+  (:use [metrics.utils :only [all-metrics]])
+  (:use [cheshire.core :only [generate-string]]))
+
+
+(defprotocol RenderableMetric
+  (render-to-basic [metric] "Turn a metric into a basic Clojure datastructure."))
+
+(extend-type Gauge
+  RenderableMetric
+  (render-to-basic [g]
+    {:type :gauge
+     :value (gauges/value g)}))
+
+(extend-type Timer
+  RenderableMetric
+  (render-to-basic [t]
+    {:type :timer
+     :rates (timers/rates t)
+     :percentiles (timers/percentiles t)
+     :max (timers/largest t)
+     :min (timers/smallest t)
+     :mean (timers/mean t)
+     :standard-deviation (timers/std-dev t)}))
+
+(extend-type Meter
+  RenderableMetric
+  (render-to-basic [m]
+    {:type :meter
+     :rates (meters/rates m)}))
+
+(extend-type Histogram
+  RenderableMetric
+  (render-to-basic [h]
+    {:type :histogram
+     :max (histograms/largest h)
+     :min (histograms/smallest h)
+     :mean (histograms/mean h)
+     :standard-deviation (histograms/std-dev h)
+     :percentiles (histograms/percentiles h)}))
+
+(extend-type Counter
+  RenderableMetric
+  (render-to-basic [c]
+    {:type :counter
+     :value (counters/value c)}))
+
+(defn- ensure-leading-slash [s]
+  (if (not= \/ (first s))
+    (str \/ s)
+    s))
+
+(defn- strip-trailing-slash [s]
+  (if (= \/ (last s))
+    (apply str (butlast s))
+    s))
+
+(defn- sanitize-uri [uri]
+  (str (-> uri
+         ensure-leading-slash
+         strip-trailing-slash)
+       \/))
+
+(defn- render-metric [[metric-name metric]]
+  [metric-name (render-to-basic metric)])
+
+(defn- metrics-json [request]
+  (let [metrics-map (into {} (map render-metric (all-metrics)))
+        json (generate-string metrics-map)]
+    (-> (response json)
+      (header "Content-Type" "application/json"))))
+
+(defn expose-metrics-as-json
+  ([handler] (expose-metrics-as-json handler "/metrics"))
+  ([handler uri]
+   (let [uri (sanitize-uri uri)]
+     (fn [request]
+       (if (.startsWith (:uri request) uri)
+         (metrics-json request)
+         (handler request))))))
+

src/metrics/ring/rendering.clj

+(ns metrics.rendering)
+
+
+(defprotocol RenderableMetric
+  (render-to-basic [metric] "Turn a metric into a basic Clojure datastructure."))
+
+(defn render-metric)
+

test/metrics/test/ring/expose.clj

+(ns metrics-clojure-ring.test.core
+  (:use [metrics-clojure-ring.core])
+  (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+  (is false "No tests have been written."))