Skip to content

Commit

Permalink
Add Metrics Server for JVM Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
DiCanio committed Oct 10, 2019
1 parent 68bff6a commit 16ab9e3
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/target
/classes
/checkouts
pom.xml
pom.xml.asc
*.jar
*.class
/.lein-*
/.nrepl-port
.hgignore
.hg/
11 changes: 11 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(defproject datomic-tx-metrics "0.1.0-SNAPSHOT"
: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."
:dependencies
[[aleph "0.4.6"]
[bidi "2.1.6"]
[com.taoensso/timbre "4.10.0"]
[environ "1.1.0"]
[io.prometheus/simpleclient_hotspot "0.5.0"]
[org.clojure/clojure "1.10.1"]
[prom-metrics "0.5-alpha2"]
[ring/ring-core "1.7.1"]])
85 changes: 85 additions & 0 deletions src/datomic_tx_metrics/core.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
(ns datomic-tx-metrics.core
(:require
[aleph.http :as http]
[bidi.ring :as bidi]
[environ.core :refer [env]]
[prometheus.alpha :as prom]
[taoensso.timbre :as log])
(:import [io.prometheus.client CollectorRegistry]
[io.prometheus.client.hotspot StandardExports MemoryPoolsExports
GarbageCollectorExports ThreadExports
ClassLoadingExports VersionInfoExports]))


;; ---- Metrics ----------------------------------------------------------------

(def ^:private metrics-registry
(doto (CollectorRegistry. true)
(.register (StandardExports.))
(.register (MemoryPoolsExports.))
(.register (GarbageCollectorExports.))
(.register (ThreadExports.))
(.register (ClassLoadingExports.))
(.register (VersionInfoExports.))))


;; ---- Callback ---------------------------------------------------------------

(defn tx-metrics-callback-handler
"Called by Datomic transactor transferring its metrics."
[tx-metrics]
(doseq [[name value] tx-metrics]
(log/info "Metric: " name " with value: " value)))


;; ---- Server -----------------------------------------------------------------

(defn- wrap-not-found
"Middleware which returns a 404 response if no downstream handler can be
found processing a request. Otherwise forwards the request to the found
handler as well as its response to the caller."
[handler]
(fn [req]
(if-let [resp (handler req)]
resp
{:status 404
:header {:content-type "text/plain"}
:body "Not Found"})))


(defn- health-handler
"Health handler returning a 200 response code with 'OK' as a response body."
[_]
{:status 200 :body "OK"})


(defn- metrics-handler
"Metrics handler returning the transactor and JVM metrics of a transactor."
[_]
(prom/dump-metrics metrics-registry))


(def ^:private routes
"Defines the routes for the web server."
["/"
[["health" {:get health-handler}]
["metrics" {:get metrics-handler}]]])


(defn- routing
"Creates a ring handler for routing requests to the appropriate sub-handler
based on `routes`."
[routes]
(-> (bidi/make-handler routes)
(wrap-not-found)))


(defn- start-metrics-server
"Starts the web server that can be used to scrape transactor + JVM metrics."
[]
(let [metrics-port (Integer/parseInt (or (:metrics-port env) "11509"))]
(log/info "Starting metrics server on port " metrics-port)
(http/start-server (routing routes) {:port metrics-port})))


(start-metrics-server)

0 comments on commit 16ab9e3

Please sign in to comment.