Fork me on GitHub
#pedestal
<
2018-08-01
>
skrat19:08:08

I'm kind of lost in interceptors, I would like to log response status code and duration, how can I do that?

ddeaguiar19:08:25

You can create an interceptor with an :enter and :leave implementation which, on enter, appends a timestamp to the context and, on leave, calculates the difference and logs it as well as the status code

ddeaguiar19:08:14

Unfortunately response timings are not available out of the box

skrat19:08:01

@ddeaguiar excellent answer, I'll give it a try

souenzzo20:08:25

You can add some headers then it will appear on chrome/firefox network tab

(def http-timming-header
  {:name  ::http-timming-header
   :enter (fn [ctx] (assoc-in ctx [:request :inst] (new Date)))
   :leave (fn [{{:keys [inst]} :request
                :as            ctx}]
            (let [diff (-> (time.coerce/to-date-time inst)
                           (time/interval (time/now))
                           (time/in-millis))]
              (assoc-in ctx [:response :headers "Server-Timing"]
                        (format "app=%s; \"My App\"" diff))))})

martinklepsch20:08:54

I wonder what approaches are there if you want to measure the time taken for every interceptor individually.

martinklepsch20:08:50

Interleave :chain with appropriate interceptors? Update :enter & :leave handlers of interceptors to include this?

ddeaguiar21:08:30

I suppose you can do that. The interceptors I’d be must interested in instrumenting are the ones I created. If anything, I’d create an interceptor helper fn which allowed me to create instrumented interceptors but personally I’ve never had the need for anything like that.

skrat21:08:25

I don't see any use cases for that, for performance tuning you can use profiler