ring-clojure / ring

Clojure HTTP server abstraction
MIT License
3.73k stars 518 forks source link

How to measure time that it takes to complete a request? #487

Closed Sleepful closed 7 months ago

Sleepful commented 7 months ago

Just curious, I was looking at Pedestal example to time a request:

http://pedestal.io/pedestal/0.7-pre/guides/what-is-an-interceptor.html#_sharing_information_between_phases

I was wondering if something similar could be done with Ring. My understanding is that a handler is the last function that runs before returning the request, so there can't be something after the handle (a middleware?) to measure the amount of time that it took to complete.

weavejester commented 7 months ago

So this won't measure the time the adapter takes to parse the request and generate the response, but if you're just interested in the time your Clojure code has taken, you could use middleware like:

(defn wrap-time [handler]
  (fn [request]
    (time (handler request))))
Sleepful commented 7 months ago

@weavejester oh, of course, so straightforward. Thank you! Still wrapping my head around the idioms 😄