metrics-clojure / metrics-clojure-ring / src / metrics / ring / instrument.clj

(ns metrics.ring.instrument
  (:use [metrics.counters :only (counter inc! dec!)]
        [metrics.meters :only (meter mark!)]
        [metrics.timers :only (timer time!)]))

(defn- mark-in! [metric-map k]
  (when-let [metric (metric-map k)]
    (mark! metric)))

(defn instrument
  "Instrument a ring handler.

  This middleware should be added as late as possible for maximum effect."
  ([handler]
   (let [active-requests (counter ["ring" "requests" "active"])
         requests (meter ["ring" "requests" "rate"] "requests")
         responses (meter ["ring" "responses" "rate"] "responses")
         statuses {2 (meter ["ring" "responses" "rate.2xx"] "responses")
                   3 (meter ["ring" "responses" "rate.3xx"] "responses")
                   4 (meter ["ring" "responses" "rate.4xx"] "responses")
                   5 (meter ["ring" "responses" "rate.5xx"] "responses")}
         times {:get     (timer ["ring" "handling-time" "GET"])
                :put     (timer ["ring" "handling-time" "PUT"])
                :post    (timer ["ring" "handling-time" "POST"])
                :head    (timer ["ring" "handling-time" "HEAD"])
                :delete  (timer ["ring" "handling-time" "DELETE"])
                :options (timer ["ring" "handling-time" "OPTIONS"])}
         request-methods {:get     (meter ["ring" "requests" "rate.GET"] "requests")
                          :put     (meter ["ring" "requests" "rate.PUT"] "requests")
                          :post    (meter ["ring" "requests" "rate.POST"] "requests")
                          :head    (meter ["ring" "requests" "rate.HEAD"] "requests")
                          :delete  (meter ["ring" "requests" "rate.DELETE"] "requests")
                          :options (meter ["ring" "requests" "rate.OPTIONS"] "requests")}]
     (fn [request]
       (inc! active-requests)
       (try
         (let [request-method (:request-method request)]
           (mark! requests)
           (mark-in! request-methods request-method)
           (let [resp (time! (times request-method)
                             (handler request))
                 status-code (or (:status resp) 404)]
             (mark! responses)
             (mark-in! statuses (int (/ status-code 100)))
             resp))
         (finally (dec! active-requests)))))))
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.