-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |