-
Notifications
You must be signed in to change notification settings - Fork 2
/
hook.clj
39 lines (33 loc) · 1.31 KB
/
hook.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(ns com.timezynk.domain.mongo.channel.hook
(:require [somnium.congomongo :as mongo]
[com.timezynk.useful.channel.subscriber.hook :refer [Hook]]
[com.timezynk.useful.mongo.db :refer [db]]
[com.timezynk.useful.prometheus.core :as metrics]
[com.timezynk.domain.context :as context]))
(defonce handler-time (metrics/counter :channel_handler_time_seconds
"A counter of the total user time used for a handler"
:function))
(defn- ->str [hook]
(str (:f hook)))
(defrecord PerformanceTrackingHook [f]
Hook
(call [this topic cname context message]
(mongo/with-mongo @db
(let [fn-name (->str this)
[new-doc old-doc] message
start-at (System/nanoTime)
current-request (or context/*request* {:id context})
_ (binding [context/*request* current-request]
(f topic cname new-doc old-doc))
end-at (System/nanoTime)]
(metrics/inc-by! handler-time
(/ (double (- end-at start-at)) 1000000000.0)
fn-name))))
(pretty-print [this]
(->str this))
(report [_]
"" ; TODO provide a helpful list of statistics here
; - average
; - min / max
; - ...
))