Skip to content

Instantly share code, notes, and snippets.

Last active Feb 23, 2017
What would you like to do?
Datomic peer CloudWatch metrics
; Metric callback handler for pushing Datomic metrics to CloudWatch
; Instructions:
; 1) Add CloudWatch sdk to your project.clj
; [com.amazonaws/aws-java-sdk-cloudwatch "1.11.6"]
; 2) Specify the handler function as the metrics callback handler with a java opt at run time
; -Ddatomic.metricsCallback=cognician.peerwatch/handler
(ns cognician.peerwatch
(:import com.amazonaws.auth.InstanceProfileCredentialsProvider
Dimension MetricDatum PutMetricDataRequest]))
(defn avg [{:keys [sum count]}]
(if (or (zero? sum) (zero? count))
(/ sum count)))
(def metric-value-xforms
{:AvailableMB identity
:MetricsFail :hi
:IndexWrites :hi
:CreateEntireIndexMsec :hi
:CreateFulltextIndexMsec :hi
:IndexDatoms :hi
:Datoms :hi
:MemoryIndexFillMsec :hi
:MetricsReport :hi})
(defn metric-value [[metric-value metric]]
[metric-value ((get metric-value-xforms metric-value avg) metric)])
(def MetricDimension
[(doto (Dimension.)
(.setName "Peer")
(.setValue "Datomic"))])
(defn metric->MetricDatum [[metric-value metric]]
(doto (MetricDatum.)
(.setMetricName (name metric-value))
(.setValue (double metric))
(.setDimensions MetricDimension)))
(defn prepare-metrics [metrics]
(into [] (comp (map metric-value)
(map metric->MetricDatum))
(def cloudwatch-client
(AmazonCloudWatchClient. (InstanceProfileCredentialsProvider. true)))
(defn send-metrics! [metrics]
(let [request (doto (PutMetricDataRequest.)
(.setNamespace "Datomic")
(.setMetricData metrics))
result (.putMetricData cloudwatch-client request)]
[request result]))
(defn handler [metrics]
(-> metrics
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment