/
impl.clj
566 lines (515 loc) · 20.4 KB
/
impl.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
(ns spootnik.reporter.impl
(:require [aleph.http :as http]
[clojure.java.io :as io]
[com.stuartsierra.component :as c]
[metrics.reporters.console :as console]
[metrics.reporters.jmx :as jmx]
[metrics.reporters.graphite :as graphite]
[metrics.reporters.riemann :as riemann]
[spootnik.log-reporter :as logs]
[metrics.jvm.core :as jvm]
[metrics.core :as m]
[metrics.gauges :as gge]
[metrics.counters :as cnt]
[metrics.meters :as mtr]
[metrics.timers :as tmr]
[metrics.histograms :as hst]
[camel-snake-kebab.core :as csk]
[clojure.string :as str]
[clojure.tools.logging :refer [info error]]
[spootnik.uncaught :refer [with-uncaught]]
[spootnik.reporter.sentry :as rs])
(:import com.aphyr.riemann.client.RiemannClient
com.aphyr.riemann.client.RiemannBatchClient
com.aphyr.riemann.client.TcpTransport
com.aphyr.riemann.client.EventDSL
com.aphyr.riemann.client.IPromise
com.codahale.metrics.ScheduledReporter
io.netty.handler.ssl.ClientAuth
io.netty.handler.ssl.SslContextBuilder
io.prometheus.client.CollectorRegistry
io.prometheus.client.dropwizard.DropwizardExports
io.prometheus.client.exporter.common.TextFormat
io.prometheus.client.hotspot.DefaultExports
io.prometheus.client.Gauge
io.prometheus.client.Counter
io.prometheus.client.Gauge$Child
io.prometheus.client.Counter$Child
io.prometheus.client.SimpleCollector$Builder
io.prometheus.client.Collector
io.prometheus.client.exporter.PushGateway
io.prometheus.client.exporter.HttpConnectionFactory
java.io.File
java.io.FileInputStream
java.io.InputStream
java.io.StringWriter
java.util.concurrent.TimeUnit
java.util.List
java.util.Map
java.net.InetSocketAddress
java.net.URL
java.security.cert.X509Certificate
java.security.cert.CertificateFactory
javax.net.ssl.HttpsURLConnection
io.netty.handler.ssl.JdkSslContext
javax.net.ssl.SSLContext))
(defprotocol RiemannSink
(send! [this e]))
(defprotocol SentrySink
(capture! [this e] [this e tags]))
(defprotocol PushGatewaySink
(counter! [this metric])
(gauge! [this metric]))
(defprotocol MetricHolder
(instrument! [this prefix])
(build! [this type alias] [this type alias f])
(inc! [this alias] [this alias v])
(dec! [this alias] [this alias v])
(mark! [this alias] [this alias v])
(update! [this alias v])
(time-fn! [this alias f])
(start! [this alias])
(stop! [this ctx]))
(def config->protocol
(comp keyword
str/lower-case
(fnil name "tcp")
:protocol))
(defmulti build-client config->protocol)
(defmethod build-client :udp
[{:keys [host port] :or {host "127.0.0.1" port 5555}}]
(RiemannClient/udp host (int port)))
(defmethod build-client :tcp
[{:keys [host port] :or {host "127.0.0.1" port 5555}}]
(RiemannClient/tcp host (int port)))
(declare client-ssl-context)
(defmethod build-client :tls
[{:keys [host port tls] :or {host "127.0.0.1" port 5554}}]
(RiemannClient/wrap
(doto (new TcpTransport host (int port))
(-> .-sslContext
(.set (.context (client-ssl-context tls)))))))
(defmethod build-client :default
[_]
(throw (ex-info "Cannot build riemann client for invalid protocol" {})))
(defn build-console-metrics-reporter [reg {:keys [opts interval]}]
(let [r (console/reporter reg opts)]
(reify
c/Lifecycle
(start [this] (console/start r interval) this)
(stop [this]
(info "scheduling a final console report")
(.report r)
(console/stop r)
this))))
(defn build-logs-metrics-reporter [reg {:keys [opts interval]}]
(let [r (logs/reporter reg opts)]
(reify
c/Lifecycle
(start [this] (logs/start r interval) this)
(stop [this]
(info "scheduling a final console report")
(.report r)
(logs/stop r)
this))))
(defn build-jmx-metrics-reporter [reg {:keys [opts]}]
(let [r (jmx/reporter reg opts)]
(reify
c/Lifecycle
(start [this] (jmx/start r) this)
(stop [this] (jmx/stop r) this))))
(defn build-graphite-metrics-reporter [reg {:keys [opts interval]}]
(let [r (graphite/reporter reg opts)]
(reify
c/Lifecycle
(start [this] (graphite/start r interval) this)
(stop [this] (graphite/stop r) this))))
(defn build-riemann-metrics-reporter [reg rclient {:keys [opts interval]}]
(if-not rclient
(throw (ex-info "need a valid riemann client to build reporter" {}))
(let [r (riemann/reporter rclient reg opts)]
(reify
c/Lifecycle
(start [this] (riemann/start r interval) this)
(stop [this]
(info "scheduling a final report of our metrics")
(.report ^ScheduledReporter r)
(riemann/stop r)
this)))))
(defn build-prometheus-metrics-reporter [reg ^CollectorRegistry registry]
(reify
c/Lifecycle
(start [this]
(info "start prometheus reporter")
(let [exporter (DropwizardExports. reg)]
(.register registry exporter)
(DefaultExports/initialize)
this))
(stop [this])))
(defn build-pushgateway-metrics-reporter [^CollectorRegistry registry]
(reify
c/Lifecycle
(start [this]
(info "Starting pushgateway reporter")
this)
(stop [this]
(info "Clearing pushgateway registry")
(.clear registry))))
(defn build-metrics-reporter [reg rclient ^CollectorRegistry prometheus-registry ^CollectorRegistry pushgateway-registry [type opt]]
(condp = type
:console (build-console-metrics-reporter reg opt)
:logs (build-logs-metrics-reporter reg opt)
:jmx (build-jmx-metrics-reporter reg opt)
:graphite (build-graphite-metrics-reporter reg opt)
:riemann (build-riemann-metrics-reporter reg rclient opt)
:prometheus (build-prometheus-metrics-reporter reg prometheus-registry)
:pushgateway (build-pushgateway-metrics-reporter pushgateway-registry)
:else (throw (ex-info "Cannot build requested metrics reporter" type))))
(defn riemann-client
"To keep dependency conflicts, let's use RiemannClient directly."
^RiemannClient
[{:keys [batch] :as opts}]
(try
(let [client ^RiemannClient (build-client opts)]
(try
(.connect client)
(catch Exception e
(error e "cannot connect to riemann")))
(if (and batch (number? batch) (pos? batch))
(RiemannBatchClient. client (int batch))
client))
(catch Exception e
(error e "cannot create riemann client"))))
(defn ->event [^RiemannClient client defaults {:keys [host service time ^Double metric description] :as ev}]
(let [tags (some-> (concat (:tags defaults) (:tags ev)) set seq)
attrs (merge (:attrs defaults) (:attrs ev))
ttl (when-let [t (or (:ttl ev) (:ttl defaults))] (float t))
state (or (:state ev) (:state defaults))
time ^Long (or time (quot (System/currentTimeMillis) 1000))]
(-> (.event client)
(.host (or host (:host defaults) (rs/localhost)))
(.service (or service (:service defaults) "<none>"))
(.time (long time))
(cond-> metric (.metric metric)
state (.state ^String state)
(seq attrs) (.attributes ^Map attrs)
(seq tags) (.tags ^List tags)
ttl (.ttl (float ttl))
description (.description ^String description))
^EventDSL
(.build))))
(defn riemann-send [^RiemannClient client events]
(if (= (count events) 1)
(.sendEvent client (first events))
(.sendEvents client ^List events)))
(defn riemann-events!
[client defaults events]
(let [deref' #(.deref ^IPromise % 100 TimeUnit/MILLISECONDS)]
(->> events
(map #(->event client defaults %))
(riemann-send client)
(deref'))))
(defn build-metrics-reporters
[reg reporters rclient ^CollectorRegistry prometheus-registry pushgateway-registry]
(info "building metrics reporters")
(mapv (partial build-metrics-reporter reg rclient prometheus-registry pushgateway-registry) reporters))
(defn build-metrics
[{:keys [reporters]} rclient ^CollectorRegistry prometheus-registry pushgateway-registry]
(let [reg (m/new-registry)
reps (build-metrics-reporters reg reporters rclient prometheus-registry pushgateway-registry)]
(doseq [r reps]
(c/start r))
[reg reps]))
(defn ->alias
[v]
(if (sequential? v)
(map name v)
(name v)))
(defn prometheus-str-metrics
"Extracts and returns as string metrics from a Prometheus registry.
Defaults to the defaultRegistry if no registry is provided."
([]
(prometheus-str-metrics CollectorRegistry/defaultRegistry))
([^CollectorRegistry registry]
(let [writer (StringWriter.)]
(TextFormat/write004
writer
(.metricFamilySamples registry))
(.toString writer))))
(defn prometheus-handler
[prometheus ^CollectorRegistry prometheus-registry req]
(if (and (:endpoint prometheus)
(not= (:uri req) (:endpoint prometheus)))
{:status 404}
{:status 200
:headers {"Content-Type" TextFormat/CONTENT_TYPE_004}
:body (prometheus-str-metrics prometheus-registry)}))
(defn ^"[Ljava.security.cert.X509Certificate;" read-ca-certs
"Read all available certificate from the default UNIX location
Adds any provided certificate as CA.
Yields an array of X509Certificate, ready to be used to
build a `trustManager` for an `SslContext`"
[files]
(let [->file #(if (string? %) (io/file %) %)
dir (io/file "/etc/ssl/certs")
fact (CertificateFactory/getInstance "X509")]
(->> dir
(.listFiles)
(seq)
(concat (map ->file files))
(filter #(.isFile ^File %))
(map #(FileInputStream. ^File %))
(map #(.generateCertificates fact ^InputStream %))
(into [])
(apply concat)
(into-array X509Certificate))))
(defn server-ssl-context
[{:keys [pkey cert ca-cert]}]
(-> (SslContextBuilder/forServer (io/file cert) (io/file pkey))
(.trustManager (io/file ca-cert))
(.clientAuth ClientAuth/REQUIRE)
(.build)))
(defn client-ssl-context
^JdkSslContext
[{:keys [pkey cert ca-cert authorities]}]
(let [authorities (or authorities [ca-cert])
ca-certs (read-ca-certs authorities)]
(-> (SslContextBuilder/forClient)
(.trustManager ca-certs)
(.keyManager (io/file cert) (io/file pkey))
(.build))))
(defn- https-connection-factory
[tls]
(let [ssl-context (client-ssl-context tls)]
(reify HttpConnectionFactory
(create [this url]
(doto ^HttpsURLConnection (.openConnection (URL. url))
(.setSSLSocketFactory (.getSocketFactory ^SSLContext (.context ssl-context))))))))
(defn collector-builder-of
^SimpleCollector$Builder [type]
(condp = type
:gauge (Gauge/build)
:counter (Counter/build)))
(defn build-pushgateway-collector
^SimpleCollector$Builder [{:keys [name type help label-names]}]
(-> (collector-builder-of type)
(.name (csk/->snake_case_string name))
(.help help)
(.labelNames (into-array String (map csk/->snake_case_string label-names)))))
(defn register-pushgateway-collector
^Collector
[^SimpleCollector$Builder collector registry]
(.register collector registry))
(defn build-pushgateway-client
[{:keys [host port tls]
:or {port 9091}}]
(let [protocol (if tls "https" "http")
url (URL. protocol host port "")
client (PushGateway. url)]
(when tls
(.setConnectionFactory client (https-connection-factory tls)))
client))
(defn build-collectors! [registry metrics]
(into {} (map (fn [metric] [(:name metric) (-> (build-pushgateway-collector metric)
(register-pushgateway-collector registry))])
metrics)))
(defn set-gauge!
[^Gauge gauge {:keys [label-values value]}]
(-> gauge
^Gauge$Child (.labels (into-array String (map name label-values)))
(.set ^double value)))
(defn inc-counter!
"Increment the given `counter` with optional increment `value`."
[^Counter counter {:keys [value label-values] :or {value 1}}]
(-> counter
^Counter$Child (.labels (into-array String (map name label-values)))
(.inc (double value))))
(defn push-pushgateway-metric!
[^PushGateway pg ^CollectorRegistry registry ^String job ^java.util.Map grouping-keys]
(.push pg registry job grouping-keys))
(defn parse-pggrouping-keys [grouping-keys]
(into {} (for [[k v] grouping-keys] [(csk/->snake_case_string k) v])))
;; Reporter configuration specs:
;; https://github.com/exoscale/reporter/blob/master/src/spootnik/reporter/specs.clj
(defrecord Reporter [rclient sentry-options reporters registry sentry
metrics riemann prevent-capture? prometheus
started? pushgateway]
c/Lifecycle
(start [this]
(if started?
this
(let [prometheus-registry (CollectorRegistry/defaultRegistry)
[pgclient pgjob pgregistry pggrouping-keys] (when pushgateway [(build-pushgateway-client pushgateway)
(name (:job pushgateway))
(CollectorRegistry.)
(parse-pggrouping-keys (:grouping-keys pushgateway))])
pgmetrics (when pushgateway (build-collectors! pgregistry (get-in metrics [:reporters :pushgateway])))
rclient (when riemann (riemann-client riemann))
[reg reps] (build-metrics metrics rclient prometheus-registry pgregistry)
options (when sentry (or sentry-options {}))
prometheus-server (when prometheus
(let [tls (:tls prometheus)
opts (cond-> {:port (:port prometheus)}
(some? (:tls prometheus))
(assoc :ssl-context (server-ssl-context tls))
(:host prometheus)
(assoc :socket-address
(InetSocketAddress.
^String (:host prometheus)
^int (:port prometheus))))]
(http/start-server
(partial prometheus-handler
prometheus
prometheus-registry)
opts)))]
(rs/init! sentry)
(when-not prevent-capture?
(with-uncaught e
(capture! (assoc this :sentry-options options) e)))
(cond-> (assoc this
:registry reg
:reporters reps
:rclient rclient
:sentry-options options
:started? true)
prometheus (assoc :prometheus {:server prometheus-server
:registry prometheus-registry})
pushgateway (assoc :pushgateway {:client pgclient
:registry pgregistry
:grouping-keys pggrouping-keys
:metrics pgmetrics
:job pgjob})))))
(stop [this]
(when started?
(when-not prevent-capture?
(with-uncaught e
(error e "uncaught exception.")))
(doseq [r reporters]
(c/stop r))
(when registry
(info "shutting down metric registry")
(m/remove-all-metrics registry))
(when rclient
(try
(.close ^RiemannClient rclient)
(catch Exception _)))
(when prometheus
(.close ^java.io.Closeable (:server prometheus)))
(rs/close! sentry))
(assoc this
:sentry-options nil
:reporters nil
:registry nil
:rclient nil
:prometheus nil
:pushgateway nil
:started? false))
MetricHolder
(instrument! [this prefix]
(when registry
(doseq [[f title] [[jvm/register-jvm-attribute-gauge-set ["jvm" "attribute"]]
[jvm/register-memory-usage-gauge-set ["jvm" "memory"]]
[jvm/register-file-descriptor-ratio-gauge-set ["jvm" "file"]]
[jvm/register-garbage-collector-metric-set ["jvm" "gc"]]
[jvm/register-thread-state-gauge-set ["jvm" "thread"]]]]
(f registry (mapv name (concat prefix title))))))
(build! [this type alias f]
(when registry
(assert (fn? f))
(case type
:gauge (or (gge/gauge registry (->alias alias))
(gge/gauge-fn registry (->alias alias) f))
(throw (ex-info "invalid metric type" {})))))
(build! [this type alias]
(when registry
(case type
:counter (cnt/counter registry (->alias alias))
:meter (mtr/meter registry (->alias alias))
:histogram (hst/histogram registry (->alias alias))
:timer (tmr/timer registry (->alias alias))
(throw (ex-info "invalid metric type" {})))))
(inc! [this alias]
(when registry
(cnt/inc! (cnt/counter registry (->alias alias)))))
(inc! [this alias v]
(when registry
(cnt/inc! (cnt/counter registry (->alias alias)) v)))
(dec! [this alias]
(when registry
(cnt/dec! (cnt/counter registry (->alias alias)))))
(dec! [this alias v]
(when registry
(cnt/dec! (cnt/counter registry (->alias alias)) v)))
(mark! [this alias]
(when registry
(mtr/mark! (mtr/meter registry (->alias alias)))))
(mark! [this alias v]
(when registry
(mtr/mark! (mtr/meter registry (->alias alias)) v)))
(update! [this alias v]
(when registry
(hst/update! (hst/histogram registry (->alias alias)) v)))
(time-fn! [this alias f]
(if registry
(tmr/time-fn! (tmr/timer registry (->alias alias)) f)
(f)))
(start! [this alias]
(when registry
(tmr/start (tmr/timer registry (->alias alias)))))
(stop! [this ctx]
(when registry
(tmr/stop ctx)))
PushGatewaySink
(counter! [this {:keys [name] :as metric}]
(when pushgateway
(let [{:keys [client metrics job registry grouping-keys]} pushgateway
collector (name metrics)]
(inc-counter! collector metric)
(push-pushgateway-metric! client registry job grouping-keys))))
(gauge! [this {:keys [name] :as metric}]
(when pushgateway
(let [{:keys [client metrics job registry grouping-keys]} pushgateway
collector (name metrics)]
(set-gauge! collector metric)
(push-pushgateway-metric! client registry job grouping-keys))))
SentrySink
(capture! [this e]
(capture! this e {}))
(capture! [_this e tags]
(rs/send-event! sentry sentry-options e tags))
RiemannSink
(send! [_this ev]
(when rclient
(let [to-seq #(if-not (sequential? %) [%] %)]
(->> ev
to-seq
(riemann-events! rclient (:defaults riemann)))))))
(defmacro time!
[reporter alias & body]
`(time-fn! ~reporter ~alias (fn [] (do ~@body))))
(defmacro with-time!
[[reporter alias] & body]
`(time-fn! ~reporter ~alias (fn [] (do ~@body))))
(defn make-reporter
([]
(map->Reporter nil))
([reporter]
(map->Reporter reporter)))
(extend-type nil
MetricHolder
(instrument! [this prefix])
(build! ([this type alias f]) ([this type alias]))
(inc! ([this alias]) ([this alias v]))
(dec! ([this alias]) ([this alias v]))
(mark! ([this alias]) ([this alias v]))
(update! [this alias v])
(time-fn! [this alias f] (f))
(start! [this alias])
(stop! [this ctx])
PushGatewaySink
(gauge! ([this metric]))
(counter! ([this metric]))
SentrySink
(capture! ([this e]) ([this e tags]))
RiemannSink
(send! [this ev]))