Search code examples
clojurecompojurecodahale-metrics

metrics clojure ring middleware not recording metrics


In my handler I have the following defined:

(def reg (new-registry))

(def app (-> app-routes 
         (expose-metrics-as-json)
         (instrument reg)))

If I go to /metrics I only see an empty json object. What am I missing? Also, the documetation is not clear as to how I can report on the console, it seems report-to-console which is in the documentation is not used any more, instead there are reporters but I cannot find any documentation as to how you use reporters.

EDIT:

I have seen that metrics are being added and recorded by looking at this:

(meters/rates ((all-metrics reg) "ring.responses.rate.2xx"))

And the results are as expected, something like:

{1 0.06325196458537237, 5 0.01824839591203641, 15 0.006466051961211013, :total 6}

So it seems that although expose-metrics-as-json is creating an endpoint, the metrics are not getting added to it.


Solution

  • Ha! I found that this is actually a bug in the code. https://github.com/sjl/metrics-clojure/blob/master/metrics-clojure-ring/src/metrics/ring/expose.clj

    ([handler uri registry]
        (expose-metrics-as-json handler uri default-registry {:pretty-print? false}))
    

    This ignores the registry parameter. I got things to work by using

    (expose-metrics-as-json "/metrics" reg {:pretty-print? false})
    

    I will fix and create a pull request.