|
| 1 | +(ns datomic-tx-metrics.core |
| 2 | + (:require |
| 3 | + [aleph.http :as http] |
| 4 | + [bidi.ring :as bidi] |
| 5 | + [environ.core :refer [env]] |
| 6 | + [prometheus.alpha :as prom] |
| 7 | + [taoensso.timbre :as log]) |
| 8 | + (:import [io.prometheus.client CollectorRegistry] |
| 9 | + [io.prometheus.client.hotspot StandardExports MemoryPoolsExports |
| 10 | + GarbageCollectorExports ThreadExports |
| 11 | + ClassLoadingExports VersionInfoExports])) |
| 12 | + |
| 13 | + |
| 14 | +;; ---- Metrics ---------------------------------------------------------------- |
| 15 | + |
| 16 | +(def ^:private metrics-registry |
| 17 | + (doto (CollectorRegistry. true) |
| 18 | + (.register (StandardExports.)) |
| 19 | + (.register (MemoryPoolsExports.)) |
| 20 | + (.register (GarbageCollectorExports.)) |
| 21 | + (.register (ThreadExports.)) |
| 22 | + (.register (ClassLoadingExports.)) |
| 23 | + (.register (VersionInfoExports.)))) |
| 24 | + |
| 25 | + |
| 26 | +;; ---- Callback --------------------------------------------------------------- |
| 27 | + |
| 28 | +(defn tx-metrics-callback-handler |
| 29 | + "Called by Datomic transactor transferring its metrics." |
| 30 | + [tx-metrics] |
| 31 | + (doseq [[name value] tx-metrics] |
| 32 | + (log/info "Metric: " name " with value: " value))) |
| 33 | + |
| 34 | + |
| 35 | +;; ---- Server ----------------------------------------------------------------- |
| 36 | + |
| 37 | +(defn- wrap-not-found |
| 38 | + "Middleware which returns a 404 response if no downstream handler can be |
| 39 | + found processing a request. Otherwise forwards the request to the found |
| 40 | + handler as well as its response to the caller." |
| 41 | + [handler] |
| 42 | + (fn [req] |
| 43 | + (if-let [resp (handler req)] |
| 44 | + resp |
| 45 | + {:status 404 |
| 46 | + :header {:content-type "text/plain"} |
| 47 | + :body "Not Found"}))) |
| 48 | + |
| 49 | + |
| 50 | +(defn- health-handler |
| 51 | + "Health handler returning a 200 response code with 'OK' as a response body." |
| 52 | + [_] |
| 53 | + {:status 200 :body "OK"}) |
| 54 | + |
| 55 | + |
| 56 | +(defn- metrics-handler |
| 57 | + "Metrics handler returning the transactor and JVM metrics of a transactor." |
| 58 | + [_] |
| 59 | + (prom/dump-metrics metrics-registry)) |
| 60 | + |
| 61 | + |
| 62 | +(def ^:private routes |
| 63 | + "Defines the routes for the web server." |
| 64 | + ["/" |
| 65 | + [["health" {:get health-handler}] |
| 66 | + ["metrics" {:get metrics-handler}]]]) |
| 67 | + |
| 68 | + |
| 69 | +(defn- routing |
| 70 | + "Creates a ring handler for routing requests to the appropriate sub-handler |
| 71 | + based on `routes`." |
| 72 | + [routes] |
| 73 | + (-> (bidi/make-handler routes) |
| 74 | + (wrap-not-found))) |
| 75 | + |
| 76 | + |
| 77 | +(defn- start-metrics-server |
| 78 | + "Starts the web server that can be used to scrape transactor + JVM metrics." |
| 79 | + [] |
| 80 | + (let [metrics-port (Integer/parseInt (or (:metrics-port env) "11509"))] |
| 81 | + (log/info "Starting metrics server on port " metrics-port) |
| 82 | + (http/start-server (routing routes) {:port metrics-port}))) |
| 83 | + |
| 84 | + |
| 85 | +(start-metrics-server) |
0 commit comments