Last active
September 12, 2016 13:51
-
-
Save drankard/6be8c4da50ed016d0dcc909d5d2e02d8 to your computer and use it in GitHub Desktop.
Onyx CloudWatch metric sender (stub)
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
...... | |
...... | |
(: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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still missing to get rid of the (memoize get-aws-client)