Skip to content

Commit 16ab9e3

Browse files
committed
Add Metrics Server for JVM Metrics
1 parent 68bff6a commit 16ab9e3

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/target
2+
/classes
3+
/checkouts
4+
pom.xml
5+
pom.xml.asc
6+
*.jar
7+
*.class
8+
/.lein-*
9+
/.nrepl-port
10+
.hgignore
11+
.hg/

project.clj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(defproject datomic-tx-metrics "0.1.0-SNAPSHOT"
2+
:description "Containing a callback handler for collecting Datomic Transactor + JVM metrics for consumption (e.g. by Prometheus) using a web endpoint offered by the included web server."
3+
:dependencies
4+
[[aleph "0.4.6"]
5+
[bidi "2.1.6"]
6+
[com.taoensso/timbre "4.10.0"]
7+
[environ "1.1.0"]
8+
[io.prometheus/simpleclient_hotspot "0.5.0"]
9+
[org.clojure/clojure "1.10.1"]
10+
[prom-metrics "0.5-alpha2"]
11+
[ring/ring-core "1.7.1"]])

src/datomic_tx_metrics/core.clj

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)