/
reporter.clj
173 lines (126 loc) · 5.94 KB
/
reporter.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
(ns samsara.trackit.reporter
(:import [java.util.concurrent TimeUnit]
[com.codahale.metrics MetricFilter])
(:require [metrics.reporters.console :as console]
[clojure.tools.logging :as log]))
(defmulti start-reporting
"Starts the reporting with the given configuration for the `registry`.
It returns a function which when called it stops the reporter."
(fn [registry cfg] (:type cfg)))
(defn- load-function-from-name
([fqn-fname]
(if (string? fqn-fname)
(let [[_ fns ff] (re-find #"([^/]+)/([^/]+)" fqn-fname)]
(when (not (and fns ff))
(throw (ex-info (str "function '" fqn-fname "' is invalid format. must be \"namespace/fun-name\".") {})))
(load-function-from-name fns ff))
fqn-fname))
([fn-ns fn-name]
(when (not (and fn-ns fn-name))
(throw (ex-info (str "function '" fn-ns "/" fn-name "' not found.") {})))
;; requiring the namespace
(require (symbol fn-ns))
(let [fn-symbol (resolve (symbol fn-ns fn-name))]
(when-not fn-symbol
(throw (ex-info (str "function '" fn-ns "/" fn-name "' not found.") {})))
fn-symbol)))
(defn- reporting-error [type cause]
(throw
(ex-info
(str "Unable to load appropriate reporter."
" Please ensure you have the followin dependency "
"[samsara/trackit-" (name type) " \"x.y.z\"]"
" in your project.clj")
{:type type} cause)))
(defn- load-dynamic-reporter
[reporter-name registry cfg]
(try
(let [reporter (load-function-from-name reporter-name)]
(reporter registry cfg))
(catch Exception x
(reporting-error (:type cfg) x))))
(defmethod start-reporting :default [registry cfg]
(log/warn "TRACKit!: Invalid or no reporting method selected."))
(defmethod start-reporting :custom
[registry
{:keys [fqn-start-function] :as cfg}]
(let [reporter (load-function-from-name fqn-start-function)]
(reporter registry cfg)))
(defmethod start-reporting :console
[registry
{:keys [reporting-frequency-seconds stream rate-unit duration-unit]
:or {reporting-frequency-seconds 300, stream (System/err)
rate-unit TimeUnit/SECONDS, duration-unit TimeUnit/MILLISECONDS} :as cfg}]
(let [reporter (console/reporter registry cfg)]
(console/start reporter reporting-frequency-seconds)
(fn [] (console/stop reporter))))
(defmethod start-reporting :graphite
[registry
{:keys [reporting-frequency-seconds host port prefix rate-unit duration-unit]
:or {reporting-frequency-seconds 10, host "localhost", port 2003, prefix "trackit"
rate-unit TimeUnit/SECONDS, duration-unit TimeUnit/MILLISECONDS} :as cfg}]
(load-dynamic-reporter "samsara.trackit.reporter-graphite/start-reporting"
registry
cfg))
(defmethod start-reporting :statsd
[registry
{:keys [reporting-frequency-seconds host port prefix rate-unit duration-unit]
:or {reporting-frequency-seconds 10, host "localhost", port 8125, prefix "trackit"
rate-unit TimeUnit/SECONDS, duration-unit TimeUnit/MILLISECONDS} :as cfg}]
(load-dynamic-reporter "samsara.trackit.reporter-statsd/start-reporting"
registry
cfg))
(defmethod start-reporting :riemann
[registry
{:keys [reporting-frequency-seconds host port prefix rate-unit duration-unit]
:or {reporting-frequency-seconds 10, host "localhost", port 5555, prefix "trackit"
rate-unit TimeUnit/SECONDS, duration-unit TimeUnit/MILLISECONDS} :as cfg}]
(load-dynamic-reporter "samsara.trackit.reporter-riemann/start-reporting"
registry
cfg))
(defmethod start-reporting :ganglia
[registry
{:keys [reporting-frequency-seconds host port prefix rate-unit duration-unit]
:or {reporting-frequency-seconds 60, host "localhost", port 8649, prefix "trackit"
rate-unit TimeUnit/SECONDS, duration-unit TimeUnit/MILLISECONDS} :as cfg}]
(load-dynamic-reporter "samsara.trackit.reporter-ganglia/start-reporting"
registry
cfg))
(defmethod start-reporting :influxdb
[registry
{:keys [reporting-frequency-seconds host port prefix rate-unit duration-unit
tags fields dbname auth connect-timeout read-timeout]
:or {reporting-frequency-seconds 10, host "localhost", port 8086, prefix "trackit"
rate-unit TimeUnit/SECONDS, duration-unit TimeUnit/MILLISECONDS} :as cfg}]
(load-dynamic-reporter "samsara.trackit.reporter-influxdb/start-reporting"
registry
cfg))
(defmethod start-reporting :newrelic
[registry
{:keys [reporter-name reporting-frequency-seconds metrics-attribute-filter
prefix rate-unit duration-unit]
:or {reporting-frequency-seconds 30, reporter-name "trackit-reporter"
metrics-attribute-filter (constantly true)
prefix "trackit/", rate-unit TimeUnit/SECONDS, duration-unit TimeUnit/MILLISECONDS
} :as cfg}]
(load-dynamic-reporter "samsara.trackit.reporter-newrelic/start-reporting"
registry
cfg))
(defmethod start-reporting :cloudwatch
[registry
{:keys [namespace reporting-frequency-seconds rate-unit duration-unit global-dimensions]
:or {reporting-frequency-seconds 300
rate-unit TimeUnit/SECONDS duration-unit TimeUnit/MILLISECONDS}
:as cfg}]
(load-dynamic-reporter "samsara.trackit.reporter-cloudwatch/start-reporting"
registry
cfg))
(defmethod start-reporting :prometheus
[registry
{:keys [reporter-name reporting-frequency-seconds rate-unit duration-unit push-gateway-url grouping-keys]
:or {reporting-frequency-seconds 300
rate-unit TimeUnit/SECONDS duration-unit TimeUnit/MILLISECONDS}
:as cfg}]
(load-dynamic-reporter "samsara.trackit.reporter-prometheus/start-reporting"
registry
cfg))