-
Notifications
You must be signed in to change notification settings - Fork 6
/
meter.clj
214 lines (173 loc) · 6.3 KB
/
meter.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
(ns blocks.meter
"Instrumentation for block stores to measure data flows, call latencies, and
other metrics.
The logic in this namespace is built around the notion of a _metric event_ and
an associated _recording function_ on the store which the events are passed
to. Each event has at least a namespaced `:type` keyword, a `:label`
associated with the store, and a numeric `:value`.
Events may contain other information like the block id or method name as
well, and it is up to the receiver to interpret them."
(:require
[blocks.data :as data]
[clojure.tools.logging :as log]
[manifold.deferred :as d]
[manifold.stream :as s])
(:import
java.io.InputStream
java.util.concurrent.atomic.AtomicLong
org.apache.commons.io.input.ProxyInputStream))
;; ## Utilities
(defn- stopwatch
"Create a delay expression which will return the number of milliseconds
elapsed between its creation and dereference."
[]
(let [start (System/nanoTime)]
(delay (/ (- (System/nanoTime) start) 1e6))))
(defn- format-bytes
"Format a byte value as a string with the given suffix."
[value unit]
(loop [value value
prefixes ["" "K" "M" "G"]]
(if (and (< 1024 value) (seq prefixes))
(recur (/ value 1024) (next prefixes))
(if (nat-int? value)
(format "%d %s%s" value (first prefixes) unit)
(format "%.1f %s%s" (double value) (first prefixes) unit)))))
(defn- meter-label
"Construct a string to label the metered store."
[store]
(str (or (::label store) (.getSimpleName (class store)))))
(defn- enabled?
"True if the store has metering enabled and a valid recorder."
[store]
(boolean (::recorder store)))
(defn- record!
"Helper to record an event to the metered store if a recording function is
present."
[store metric-type value attrs]
(when-let [recorder (::recorder store)]
(try
(recorder
store
(assoc attrs
:type metric-type
:label (meter-label store)
:value value))
(catch Exception ex
(log/warn ex "Failure while recording metric")))))
;; ## Stream Metering
(def ^:dynamic *io-report-period*
"Record incremental IO metrics every N seconds."
10)
(defn- metering-block-stream
"Wrap the given stream in an intermediate stream which will record metric
events with the number of blocks which passed through the stream."
[store metric-type attrs stream]
(let [counter (AtomicLong. 0)
period *io-report-period*
label (meter-label store)
out (s/map #(do (.incrementAndGet counter) %) stream)
reports (s/periodically
(* period 1000)
#(.getAndSet counter 0))
flush! (fn flush!
[sum]
(when (pos? sum)
(log/tracef "Metered %s of %d blocks through stream %s (%.2f/sec)"
(name metric-type) sum label
(double (/ sum period)))
(record! store metric-type sum nil)))]
(s/consume flush! reports)
(s/on-closed
stream
(fn report-final
[]
(flush! (.getAndSet counter 0))
(s/close! reports)))
out))
(defn- metering-input-stream
"Wrap the given input stream in a proxy which will record metric events with
the given type and number of bytes read."
[store metric-type block-id ^InputStream input-stream]
(let [meter (volatile! [(System/nanoTime) 0])]
(letfn [(flush!
[]
(let [[last-time sum] @meter
elapsed (/ (- (System/nanoTime) last-time) 1e9)
label (meter-label store)]
(when (pos? sum)
(log/tracef "Metered %s of %s block %s: %s (%s)"
(name metric-type) label block-id
(format-bytes sum "B")
(format-bytes (/ sum elapsed) "Bps"))
(record! store metric-type sum {:block block-id})
(vreset! meter [(System/nanoTime) 0]))))]
(proxy [ProxyInputStream] [input-stream]
(afterRead
[n]
(when (pos? n)
(let [[last-time sum] (vswap! meter update 1 + n)
elapsed (/ (- (System/nanoTime) last-time) 1e9)]
(when (<= *io-report-period* elapsed)
(flush!)))))
(close
[]
(flush!)
(.close input-stream))))))
;; ## Metered Content
(deftype MeteredContentReader
[store metric-type block-id content]
data/ContentReader
(read-all
[this]
(metering-input-stream
store metric-type block-id
(data/read-all content)))
(read-range
[this start end]
(metering-input-stream
store metric-type block-id
(data/read-range content start end))))
(alter-meta! #'->MeteredContentReader assoc :private true)
(defn metered-block
"Wrap the block with a lazy constructor for a metered input stream which will
report metrics for the given type. If the store does not have a recorder, the
block will be returned unchanged."
[store metric-type block]
(when block
(if (enabled? store)
(data/wrap-content
block
(partial ->MeteredContentReader
store
metric-type
(:id block)))
block)))
;; ## Method Wrappers
(defn measure-stream
"Measure the flow of blocks through a manifold stream. Returns the wrapped
stream, or the original if the store does not have metering enabled."
[store method-kw attrs stream]
(cond->> stream
(enabled? store)
(metering-block-stream
store ::list-stream
(assoc attrs :method method-kw))))
(defn measure-method
"Measure the end-to-end elapsed time for a block store method. Returns a
deferred with a final report hook if the store has metering enabled."
[store method-kw attrs body-deferred]
(let [elapsed (stopwatch)]
(cond-> body-deferred
(enabled? store)
(d/finally
(fn record-elapsed
[]
(log/tracef "Method %s of %s block store on %s took %.1f ms"
(name method-kw)
(meter-label store)
attrs
@elapsed)
(record!
store ::method-time @elapsed
(assoc attrs :method method-kw)))))))