-
Notifications
You must be signed in to change notification settings - Fork 47
/
publisher.clj
319 lines (220 loc) · 8.49 KB
/
publisher.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
(ns com.brunobonacci.mulog.publisher
(:require [com.brunobonacci.mulog.buffer :as rb]
[com.brunobonacci.mulog.utils :as ut]
[clojure.java.io :as io]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| P U B L I S H E R P R O T O C O L |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defprotocol PPublisher
"Publisher protocol"
(agent-buffer [this]
"Returns the agent-buffer where items are sent to, basically your
inbox.")
(publish-delay [this]
"The number of milliseconds between two calls to `publish` function.
return `nil` if you don't want μ/log call the `publish` function")
(publish [this buffer]
"publishes the items in the buffer and returns the new state of
the buffer which presumably doesn't contains the messages
successfully sent.")
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| C O N S O L E |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftype ConsolePublisher [config buffer transform]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_]
buffer)
(publish-delay [_]
200)
(publish [_ buffer]
;; items are pairs [offset <item>]
(doseq [item (transform (map second (rb/items buffer)))]
(if (:pretty? config)
(printf "%s\n" (ut/pprint-event-str item))
(printf "%s\n" (ut/edn-str item))))
(flush)
(rb/clear buffer)))
(defn console-publisher
[{:keys [transform pretty?] :as config}]
(ConsolePublisher. config (rb/agent-buffer 10000) (or transform identity)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| S I M P L E - F I L E |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftype SimpleFilePublisher [config ^java.io.Writer filewriter buffer transform]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_]
buffer)
(publish-delay [_]
500)
(publish [_ buffer]
;; items are pairs [offset <item>]
(doseq [item (transform (map second (rb/items buffer)))]
(.write filewriter ^String (str (ut/edn-str item) \newline)))
(.flush filewriter)
(rb/clear buffer))
java.io.Closeable
(close [_]
(.flush filewriter)
(.close filewriter)))
(defn simple-file-publisher
[{:keys [filename transform] :as config}]
{:pre [filename]}
(let [filename (io/file filename)]
;; make parent dirs
(when-let [path (.getParentFile filename)]
(.mkdirs path))
(SimpleFilePublisher.
config
(io/writer filename :append true)
(rb/agent-buffer 10000)
(or transform identity))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| F A C T O R Y |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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\".")
{:fqn-fname fqn-fname})))
(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.")
{:fn-ns fn-ns :fn-name fn-name})))
;; 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-ns fn-ns :fn-name fn-name})))
fn-symbol)))
(defn- loading-error [stage info cause]
(case stage
:loading
(throw
(ex-info
(str "Unable to load appropriate publisher."
" Please ensure you have the following dependency "
"[com.brunobonacci/mulog-"
(some-> info :config :type name) " \"x.y.z\"]"
" in your project.clj")
info cause))
:init
(throw
(ex-info
(str "Unable to initialize publisher."
" Please ensure you have the publisher has a function with 1 argument.")
info cause))
:verify
(throw
(ex-info "Invalid publisher, not an instance of com.brunobonacci.mulog.publisher.PPublisher"
info))))
(defn- load-dynamic-publisher
[publisher-name config]
(let [;; load publisher factory function
publisher* (try
(load-function-from-name publisher-name)
(catch Exception x
(loading-error :loading {:config config} x)))
;; initialize publisher
publisher (try (publisher* config)
(catch Exception x
(loading-error :init {:config config} x)))]
;; verify type
(when-not (instance? com.brunobonacci.mulog.publisher.PPublisher publisher)
(loading-error :verify {:config config
:name publisher-name
:type (type publisher)} nil))
;; if all ok return the publisher
publisher))
(defmulti publisher-factory
"Creates a publisher of the give `:type`."
(fn [cfg] (:type cfg)))
(defmethod publisher-factory :default
[cfg]
(throw
(ex-info "μ/log Invalid or no reporting method selected."
{:type (:type cfg)
:config cfg})))
(defmethod publisher-factory :custom
[{:keys [fqn-function] :as cfg}]
(load-dynamic-publisher fqn-function cfg))
(defmethod publisher-factory :inline
[{:keys [publisher] :as cfg}]
;; verify type
(when-not (instance? com.brunobonacci.mulog.publisher.PPublisher publisher)
(loading-error :verify {:config cfg
:name :direct
:type (type publisher)} nil))
publisher)
(defmethod publisher-factory :console
[config]
(console-publisher config))
(defmethod publisher-factory :console-json
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.console-json/json-console-publisher"
config))
(defmethod publisher-factory :simple-file
[config]
(simple-file-publisher config))
(defmethod publisher-factory :elasticsearch
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.elasticsearch/elasticsearch-publisher"
config))
(defmethod publisher-factory :jvm-metrics
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.jvm-metrics/jvm-metrics-publisher"
config))
(defmethod publisher-factory :filesystem-metrics
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.filesystem-metrics/filesystem-metrics-publisher"
config))
(defmethod publisher-factory :kafka
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.kafka/kafka-publisher"
config))
(defmethod publisher-factory :zipkin
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.zipkin/zipkin-publisher"
config))
(defmethod publisher-factory :kinesis
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.kinesis/kinesis-publisher"
config))
(defmethod publisher-factory :cloudwatch
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.cloudwatch/cloudwatch-publisher"
config))
(defmethod publisher-factory :slack
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.slack/slack-publisher"
config))
(defmethod publisher-factory :prometheus
[config]
(load-dynamic-publisher
"com.brunobonacci.mulog.publishers.prometheus/prometheus-publisher"
config))