-
Notifications
You must be signed in to change notification settings - Fork 64
/
metrics.clj
134 lines (113 loc) · 5.2 KB
/
metrics.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
(ns ziggurat.metrics
(:require [clojure.string :as str]
[clojure.tools.logging :as log]
[clojure.walk :refer [stringify-keys]]
[ziggurat.config :refer [ziggurat-config]]
[ziggurat.util.java-util :as util])
(:import com.gojek.metrics.datadog.DatadogReporter
[com.gojek.metrics.datadog.transport UdpTransport UdpTransport$Builder]
[io.dropwizard.metrics5 Histogram Meter MetricName MetricRegistry]
java.util.concurrent.TimeUnit)
(:gen-class
:name tech.gojek.ziggurat.internal.Metrics
:methods [^{:static true} [incrementCount [String String] void]
^{:static true} [incrementCount [String String java.util.Map] void]
^{:static true} [decrementCount [String String] void]
^{:static true} [decrementCount [String String java.util.Map] void]
^{:static true} [reportTime [String long] void]
^{:static true} [reportTime [String long java.util.Map] void]]))
(defonce metrics-registry
(MetricRegistry.))
(defn- merge-tags
[additional-tags]
(let [default-tags {"actor" (:app-name (ziggurat-config))}]
(merge default-tags (stringify-keys additional-tags))))
(defn- get-tagged-metric
[metric-name tags]
(.tagged ^MetricName metric-name tags))
(defn mk-meter
([category metric]
(mk-meter category metric nil))
([category metric additional-tags]
(let [namespace (str category "." metric)
metric-name (MetricRegistry/name ^String namespace nil)
tags (merge-tags additional-tags)
tagged-metric (get-tagged-metric metric-name tags)]
(.meter ^MetricRegistry metrics-registry ^MetricName tagged-metric))))
(defn mk-histogram
([category metric]
(mk-histogram category metric nil))
([category metric additional-tags]
(let [namespace (str category "." metric)
metric-name (MetricRegistry/name ^String namespace nil)
tags (merge-tags additional-tags)
tagged-metric (.tagged ^MetricName metric-name tags)]
(.histogram ^MetricRegistry metrics-registry ^MetricName tagged-metric))))
(defn intercalate-dot
[names]
(str/join "." names))
(defn- get-metric-namespaces
[metric-namespaces]
(if (vector? metric-namespaces)
(intercalate-dot metric-namespaces)
metric-namespaces))
(defn- get-v
[f d v]
(if (f v) v d))
(def ^:private get-int (partial get-v number? 1))
(def ^:private get-map (partial get-v map? {}))
(defn- inc-or-dec-count
([sign metric-namespace metric]
(inc-or-dec-count sign metric-namespace metric 1 {}))
([sign metric-namespace metric n-or-additional-tags]
(inc-or-dec-count sign metric-namespace metric (get-int n-or-additional-tags) (get-map n-or-additional-tags)))
([sign metric-namespace metric n additional-tags]
(inc-or-dec-count sign {:metric-namespace metric-namespace :metric metric :n n :additional-tags additional-tags}))
([sign {:keys [metric-namespace metric n additional-tags]}]
(let [metric-ns (get-metric-namespaces metric-namespace)
meter ^Meter (mk-meter metric-ns metric (get-map additional-tags))]
(.mark meter (sign (get-int n))))))
(def increment-count (partial inc-or-dec-count +))
(def decrement-count (partial inc-or-dec-count -))
(defn report-histogram
([metric-namespaces val]
(report-histogram metric-namespaces val nil))
([metric-namespaces val additional-tags]
(let [metric-namespace (get-metric-namespaces metric-namespaces)
histogram ^Histogram (mk-histogram metric-namespace "all" additional-tags)]
(.update histogram (get-int val)))))
(def report-time report-histogram) ;; for backward compatibility
(defn start-statsd-reporter [statsd-config env]
(let [{:keys [enabled host port]} statsd-config]
(when enabled
(let [transport (-> (UdpTransport$Builder.)
(.withStatsdHost host)
(.withPort port)
(.build))
reporter (-> (DatadogReporter/forRegistry metrics-registry)
(.withTransport transport)
(.withTags [(str env)])
(.build))]
(log/info "Starting statsd reporter")
(.start reporter 1 TimeUnit/SECONDS)
{:reporter reporter :transport transport}))))
(defn stop-statsd-reporter [datadog-reporter]
(when-let [{:keys [reporter transport]} datadog-reporter]
(.stop ^DatadogReporter reporter)
(.close ^UdpTransport transport)
(log/info "Stopped statsd reporter")))
(defn -incrementCount
([metric-namespace metric]
(increment-count metric-namespace metric))
([metric-namespace metric additional-tags]
(increment-count metric-namespace metric (util/java-map->clojure-map additional-tags))))
(defn -decrementCount
([metric-namespace metric]
(decrement-count metric-namespace metric))
([metric-namespace metric additional-tags]
(decrement-count metric-namespace metric (util/java-map->clojure-map additional-tags))))
(defn -reportTime
([metric-namespace time-val]
(report-histogram metric-namespace time-val))
([metric-namespace time-val additional-tags]
(report-histogram metric-namespace time-val (util/java-map->clojure-map additional-tags))))