-
Notifications
You must be signed in to change notification settings - Fork 64
/
metrics.clj
176 lines (147 loc) · 7.66 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
(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 remove-topic-tag-for-old-namespace
[additional-tags ns]
(let [topic-name (:topic_name additional-tags)]
(dissoc additional-tags (when (some #(= % topic-name) ns) :topic_name))))
(defn- get-metric-namespaces
[metric-namespaces]
(if (vector? metric-namespaces)
[(intercalate-dot metric-namespaces)]
[metric-namespaces (str (:app-name (ziggurat-config)) "." 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
"This method increments or decrements the `metric` associated with `metric-namespace` by `n` count
according to the value of `sign`. Here sign can be '+' or '-'. User can pass tags as 'key-value' pairs
through `additional_tags`.
Deprecation Notice: As of version 3.1.0, this function publishes the metrics in two formats.
The first format appends the service name to `metric-namespace` before publishing while the second format publishes
the metrics without making any changes to `metric-namespace`.
E.g. if the passed metric-namespace is `throughput` it will be published as `service_name.throughput` and `throughput`.
In future releases, Ziggurat will stop publishing metrics in the first format i.e. `service_name.namespace`.
"
([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-namespaces (get-metric-namespaces metric-namespace)
make-meter-for-namespace #(mk-meter % metric (remove-topic-tag-for-old-namespace (get-map additional-tags) metric-namespace))]
(doseq [metric-ns metric-namespaces]
(.mark ^Meter (make-meter-for-namespace metric-ns) (sign (get-int n)))))))
(def increment-count (partial inc-or-dec-count +))
(def decrement-count (partial inc-or-dec-count -))
(defn multi-ns-increment-count [nss metric additional-tags]
(doseq [ns nss]
(increment-count ns metric additional-tags)))
(defn report-histogram
([metric-namespaces val]
(report-histogram metric-namespaces val nil))
([metric-namespaces val additional-tags]
(let [intercalated-metric-namespaces (get-metric-namespaces metric-namespaces)
make-histogram-for-namespace #(mk-histogram % "all" (remove-topic-tag-for-old-namespace additional-tags metric-namespaces))]
(doseq [metric-ns intercalated-metric-namespaces]
(.update (make-histogram-for-namespace metric-ns) (get-int val))))))
(defn report-time
"This function is an alias for `report-histogram`.
Deprecation Notice: `report-time` will be removed in the future releases of Ziggurat. It has been renamed to
`report-histogram`."
[& args]
(log/warn "Deprecation Notice: This function is deprecated in favour of ziggurat.metrics/report-histogram. Both functions have the same interface, so please use that function. It will be removed in future releases.")
(apply report-histogram args))
(defn multi-ns-report-histogram [nss time-val additional-tags]
(doseq [ns nss]
(report-histogram ns time-val additional-tags)))
(defn multi-ns-report-time
"This function is an alias for `multi-ns-report-histogram`.
Deprecation Notice: `multi-ns-report-time` will be removed in the future releases of Ziggurat. It has been renamed to
`multi-ns-report-histogram`."
[nss time-val additional-tags]
(log/warn "Deprecation Notice: This function is deprecated in favour of ziggurat.metrics/multi-ns-report-histogram. Both functions have the same interface, so please use that function. It will be removed in future releases.")
(multi-ns-report-histogram nss time-val additional-tags))
(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))))