   (-> (Counter/newBuilder)
       (.namespace namespace)
       (.name "http_request_durations_milliseconds_total")
-      (.labelNames (into-array String ["method" "status"]))
+      (.labelNames (into-array String ["method" "status" "path"]))
       (.documentation "The total amount of time Ring has spent answering HTTP requests (in milliseconds).")
       (.build)))
 

  (-> (Counter/newBuilder)
      (.namespace namespace)
      (.name "http_requests_total")
      (.labelNames (into-array String ["method" "status"]))
      (.documentation "A counter of the total number of HTTP requests processed.")
      (.build)))


  (-> (Summary/newBuilder)
      (.namespace namespace)
      (.name "http_request_durations_milliseconds")
      (.labelNames (into-array String ["method" "status" "path"]))
      (.documentation "A histogram of the response latency for HTTP requests (in milliseconds).")
      (.purgeInterval 2 TimeUnit/MINUTES)
      (.build)))

(defn- record-request-metric [request-method request-path response-status request-time]
  (let [status-label (str (int (/ response-status 100)) "XX")
        method-label (.toUpperCase (name request-method))]
    (-> (.newPartial @request-counter)
        (.labelPair "method" method-label)
        (.labelPair "status" status-label)
        (.labelPair "path" request-path)
        (.apply)
        (.increment))
    (-> (.newPartial @request-duration-counter)
        (.labelPair "method" method-label)
        (.labelPair "status" status-label)
        (.labelPair "path" request-path)
        (.apply)
        (.increment (double request-time)))
    (-> (.newPartial @request-summary)
        (.labelPair "method" method-label)
        (.labelPair "status" status-label)
        (.labelPair "path" request-path)
        (.apply)
        (.observe (double request-time)))))

(defn instrument-handler [handler]
  "Ring middleware to record request metrics"
  (fn [request]
    (let [request-path (:uri request)
          request-method (:request-method request)
          start-time (System/currentTimeMillis)
          response (handler request)
          finish-time (System/currentTimeMillis)
          response-status (or (:status response) 404)
          request-time (- finish-time start-time)]
      (record-request-metric request-method request-path response-status request-time)
      response)))

(defn metrics-json []
