-
Notifications
You must be signed in to change notification settings - Fork 64
/
clj_statsd_metrics_wrapper.clj
44 lines (36 loc) · 1.29 KB
/
clj_statsd_metrics_wrapper.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(ns ziggurat.clj-statsd-metrics-wrapper
(:require [clj-statsd :as statsd]
[ziggurat.metrics-interface])
(:import (ziggurat.metrics_interface MetricsProtocol)))
(def rate 1.0)
(defn initialize [{:keys [host port enabled]}]
(when enabled
(statsd/setup host port)))
(defn terminate []
(reset! statsd/cfg nil)
(send statsd/sockagt (constantly nil)))
(defn- coerce-value
[v]
(if (or (keyword? v)
(symbol? v))
(name v)
v))
(defn- build-tags
[tags]
(map (fn [[key value]]
(str (name key) ":" (coerce-value value)))
tags))
(defn update-counter [namespace metric tags value]
(let [final-tags (build-tags tags)
final-metric (str namespace "." metric)]
(statsd/increment final-metric value rate final-tags)))
(defn update-timing [namespace metric tags value]
(let [final-tags (build-tags tags)
final-metric (str namespace "." metric)]
(statsd/timing final-metric value rate final-tags)))
(deftype CljStatsd []
MetricsProtocol
(initialize [this statsd-config] (initialize statsd-config))
(terminate [this] (terminate))
(update-counter [this namespace metric tags signed-val] (update-counter namespace metric tags signed-val))
(update-timing [this namespace metric tags value] (update-timing namespace metric tags value)))