Skip to content

Instantly share code, notes, and snippets.

@drankard
Last active September 12, 2016 13:51
Show Gist options
  • Save drankard/6be8c4da50ed016d0dcc909d5d2e02d8 to your computer and use it in GitHub Desktop.
Save drankard/6be8c4da50ed016d0dcc909d5d2e02d8 to your computer and use it in GitHub Desktop.
Onyx CloudWatch metric sender (stub)
......
......
(:import (com.amazonaws ClientConfiguration)
(com.amazonaws.auth BasicAWSCredentials)
(com.amazonaws.services.cloudwatch AmazonCloudWatchClient)
(com.amazonaws.services.cloudwatch.model PutMetricDataRequest MetricDatum StandardUnit Dimension))
(defn get-aws-client [aws-config]
(info (str "aws-config: " aws-config))
(let [port (:https-proxy-port aws-config)
port (condp #(%1 %2) port
number? port
string? (Integer. port)
nil)
client-config (if (:https-proxy-host aws-config)
(doto (ClientConfiguration.)
(.setProxyHost (:https-proxy-host aws-config))
(.setProxyPort port))
(ClientConfiguration.))
props (BasicAWSCredentials. (:aws-access-key-id aws-config) (:aws-secret-key aws-config))
client (doto (AmazonCloudWatchClient. props client-config)
(.setEndpoint (str "monitoring." (:aws-region aws-config) ".amazonaws.com")))]
client ))
(def get-aws-client-mem (memoize get-aws-client))
(defn dimension [name value]
(doto (Dimension.)
(.setName name)
(.setValue value)))
; wiki quantile distributions for a full explanation
(defn cloudwatch-request [metric-msg]
(let [dimensions [(dimension "task-id" (name (or (:task-id metric-msg) :ingen-task-id)))
(dimension "quantile" (str (or (:quantile metric-msg) "")))
(dimension "period" (str (or (:period metric-msg) "")))]
datom (doto (MetricDatum.)
(.setMetricName (or (:label metric-msg) "ingen-label"))
(.setUnit (StandardUnit/Count))
(.setTimestamp (c/to-date (l/local-now)))
(.setValue (double (or (:value metric-msg) 0)))
(.setDimensions dimensions))
req (doto (PutMetricDataRequest.)
(.setNamespace "onyx-kildedata-sync"))]
(doto (. req (getMetricData)) (.add datom))
req))
(defn send-cloudwatch-metric [aws-config metric-msg]
(let [client (get-aws-client-mem aws-config)
req (cloudwatch-request metric-msg)]
(doto client (.putMetricData req))))
(defn cloud-watch-sender [lifecycle ch]
(future
(loop []
(when-let [metric-msg (<!! ch)]
(try
(send-cloudwatch-metric (:aws-config lifecycle) metric-msg)
(catch InterruptedException e)
(catch Exception e
(log/warn e))))
(recur))))
@drankard
Copy link
Author

Still missing to get rid of the (memoize get-aws-client)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment