Last active
February 23, 2017 19:14
-
-
Save geoff-kruss/4504cdcf7e017d289862ab75fc856720 to your computer and use it in GitHub Desktop.
Datomic peer CloudWatch metrics
This file contains hidden or 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
; Metric callback handler for pushing Datomic metrics to CloudWatch | |
; | |
; http://docs.datomic.com/monitoring.html | |
; | |
; 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 | |
com.amazonaws.services.cloudwatch.AmazonCloudWatchClient | |
[com.amazonaws.services.cloudwatch.model | |
Dimension MetricDatum PutMetricDataRequest])) | |
(defn avg [{:keys [sum count]}] | |
(if (or (zero? sum) (zero? count)) | |
0 | |
(/ 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)) | |
metrics)) | |
;; | |
(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 | |
prepare-metrics | |
send-metrics!) | |
;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment