-
Notifications
You must be signed in to change notification settings - Fork 40
/
client.clj
238 lines (204 loc) · 7.82 KB
/
client.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
(ns riemann.client
"Network client for connecting to a Riemann server. Usage:
(def c (tcp-client {:host \"monitoring.local\"}))
(send-event c {:service \"fridge\"
:state \"running\"
:metric 2.0
:tags [\"joke\"]})
=> #<com.riemann.riemann.client.MapPromise ...>
@(query c \"tagged \\\"joke\\\"\")
=> [{:service \"fridge\" ... }]
(close! c)
Clients are resistant to failure; they will attempt to reconnect a dropped
connection periodically. Note that clients will not automatically queue or
retry failed sends."
(:import (io.riemann.riemann.client RiemannBatchClient
RiemannClient
MapPromise
IPromise
Fn2
Transport
AsynchronousTransport
IRiemannClient
TcpTransport
UdpTransport
SSL)
(java.util List)
(clojure.lang IDeref)
(java.net InetSocketAddress)
(java.io IOException))
(:require [less.awful.ssl :as ssl])
(:use riemann.codec))
(defn map-promise
"Maps a riemann client promise by applying a function."
[^IPromise p f]
(.map p (reify Fn2 (call [_ x] (f x)))))
(defn send-msg
"Send a message to the server, asynchronously. Returns an IDeref which can be
resolved to a response message."
[^AsynchronousTransport client msg]
(-> client
(.sendMessage (encode-pb-msg msg))
(map-promise decode-pb-msg)))
(defn query
"Query the server for events in the index. Returns a list of events."
[^IRiemannClient client string]
(-> client
(.query string)
(map-promise (partial map decode-pb-event))))
(defn send-events
"Sends several events, asynchronously, over client. Returns an IDeref which
can be resolved to a response message."
[^IRiemannClient client events]
(-> client
(.sendEvents ^List (map encode-client-pb-event events))
(map-promise decode-pb-msg)))
(defn send-event
"Sends a single event, asynchronously, over client. Returns an IDeref which
can be resolved to a response message."
[^IRiemannClient client event]
(-> client
(.sendEvent (encode-client-pb-event event))
(map-promise decode-pb-msg)))
(defn send-exception
"Send an exception, asynchronously, over client. Uses (:name service) as the
service. Returns an IDeref which can be resolved to a response message."
[^IRiemannClient client service ^Throwable t]
(-> client
(.sendException (name service) t)
(map-promise decode-pb-msg)))
; Transports
(defn connect!
"Connect a client or transport."
[^Transport t]
(.connect t))
(defn connected?
"Is a client or transport connected?"
[^Transport t]
(.isConnected t))
(defn close!
"Close a client or transport, shutting it down."
[^Transport client]
(.close client))
(defn flush!
"Flush messages from a client or transport's buffers."
[^Transport t]
(.flush t))
(defn reconnect!
"Reconnect a client or transport."
[^Transport client]
(.reconnect client))
(defn transport
"Get an underlying transport from a client or transport."
[^Transport t]
(.transport t))
(defn ^RiemannBatchClient batch-client
"Wraps a client in a RiemannBatchClient, with batch size n."
([client] (batch-client client 10))
([^IRiemannClient client n]
(RiemannBatchClient. client n)))
(defn ^RiemannClient tcp-client
"Creates a new TCP client. Options:
:host The host to connect to
:port The port to connect to
:tls? Whether to use TLS when connecting
:key A PKCS8 key
:cert A PEM certificate
:ca-cert The signing cert for our certificate and the server's
:auto-connect Whether to call 'connect!' prior to returning the client
Example:
(tcp-client)
(tcp-client {:host \"foo\" :port 5555})"
[& opts]
(let [opts (if (and (= 1 (count opts))
(map? (first opts)))
(first opts)
(apply hash-map opts))
{:keys [^String host
^String remote-host
^String local-host
^Integer port
^Integer remote-port
^Integer local-port
tls?
key
cert
ca-cert
auto-connect]
:or {host "localhost", local-port 0, auto-connect true}} opts]
; Check options
(when tls?
(assert key)
(assert cert)
(assert ca-cert))
; Create client
(let [remote-port (int (or remote-port port (if tls? 5554 5555)))
client (if tls?
; TLS client
(RiemannClient.
(doto (if-not local-host
(TcpTransport. (or remote-host host) remote-port)
(TcpTransport. (or remote-host host) remote-port local-host local-port))
(-> .sslContext
;; (.set (SSL/sslContext key cert ca-cert))
(.set (ssl/ssl-context key cert ca-cert)))))
; Standard client
(if-not local-host
(RiemannClient/tcp (or remote-host host) remote-port)
(RiemannClient/tcp (or remote-host host) remote-port local-host local-port)))]
; Attempt to connect lazily.
(when auto-connect (try (connect! client)
(catch IOException _ nil)))
client)))
(defn ^RiemannClient udp-client
"Creates a new UDP client. Can take an optional maximum message size. Example:
(udp-client)
(udp-client {:host \"foo\" :port 5555 :max-size 16384})"
[& opts]
(let [opts (if (and (= 1 (count opts))
(map? (first opts)))
(first opts)
(apply hash-map opts))
{:keys [^String host
^String remote-host
^String local-host
^Integer port
^Integer remote-port
^Integer local-port
^Integer max-size
auto-connect]
:or {port 5555
host "localhost"
max-size 16384
local-port 0
auto-connect true}} opts
c (RiemannClient.
(doto (if-not local-host
(UdpTransport. (or remote-host host) (or remote-port port))
(UdpTransport. (or remote-host host) (or remote-port port) local-host local-port))
(-> .sendBufferSize (.set max-size))))]
(when auto-connect (try (connect! c)
(catch IOException _ nil)))
c))
(defn ^IRiemannClient multi-client
"Creates a new multiclient from n clients"
[clients]
(let [clients (vec clients)
n (count clients)
c (fn choose-client []
(nth clients (mod (.getId (Thread/currentThread)) n)))]
(reify IRiemannClient
; Transport
(isConnected [c] (boolean (some connected? clients)))
(connect [c] (locking c (dorun (map connect! clients))))
(reconnect [c] (locking c (dorun (map reconnect! clients))))
(close [c] (locking c (dorun (map close! clients))))
(flush [c] (dorun (map flush! clients)))
(transport [c] (throw (UnsupportedOperationException.)))
; Client
(sendMessage [_ msg] (.sendMessage ^IRiemannClient (c) msg))
(sendEvent [_ e] (.sendEvent ^IRiemannClient (c) e))
(^IPromise sendEvents [_ ^List es]
(.sendEvents ^IRiemannClient (c) ^List es))
(sendException [_ s t] (.sendException ^IRiemannClient (c) s t))
(event [_] (.event ^IRiemannClient (c))))))