-
Notifications
You must be signed in to change notification settings - Fork 19
/
Histogram.scala
284 lines (261 loc) · 9.63 KB
/
Histogram.scala
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
package io.chrisdavenport.epimetheus
import cats._
import cats.implicits._
import cats.effect._
import cats.implicits._
import io.prometheus.client.{Histogram => JHistogram}
import scala.concurrent.duration._
import shapeless._
/**
* Histogram metric, to track distributions of events.
*
* Note: Each bucket is one timeseries. Many buckets and/or many dimensions with labels
* can produce large amount of time series, that may cause performance problems.
*
* The default buckets are intended to cover a typical web/rpc request from milliseconds to seconds.
*
*/
sealed abstract class Histogram[F[_]]{
/**
* Persist an observation into this [[Histogram]]
*
* @param d The observation to persist
*/
def observe(d: Double): F[Unit]
}
/**
* Histogram Constructors, Convenience Methods and Unsafe Histogram Access
*
* Convenience function exposed here will also be exposed as implicit syntax
* enhancements on the Histogram
*/
object Histogram {
// Convenience ----------------------------------------------------
// Since these methods are not ex
/**
* Persist a timed value into this [[Histogram]]
*
* @param h Histogram
* @param fa The action to time
* @param unit The unit of time to observe the timing in. Default Histogram buckets
* are optimized for `SECONDS`.
*/
def timed[E, F[_]: Bracket[?[_], E]: Clock, A](h: Histogram[F], fa: F[A], unit: TimeUnit): F[A] =
Bracket[F, E].bracket(Clock[F].monotonic(unit))
{_: Long => fa}
{start: Long => Clock[F].monotonic(unit).flatMap(now => h.observe((now - start).toDouble))}
/**
* Persist a timed value into this [[Histogram]] in unit Seconds. This is exposed.
* since default buckets are in seconds it makes sense the general case will be to
* match the default buckets.
*
* @param h Histogram
* @param fa The action to time
*/
def timedSeconds[E, F[_]: Bracket[?[_], E]: Clock, A](h: Histogram[F], fa: F[A]): F[A] =
timed(h, fa, SECONDS)
// Constructors ---------------------------------------------------
/**
* Default Buckets
*
* Intended to cover a typical web/rpc request from milliseconds to seconds.
*/
val defaults = List(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10)
/**
* Constructor for a [[Histogram]] with no labels. Default buckets are [[defaults]]
* and are intended to cover a typical web/rpc request from milliseconds to seconds.
*
* @param cr CollectorRegistry this [[Histogram]] will be registered with
* @param name The name of the Histogram
* @param help The help string of the metric
*/
def noLabels[F[_]: Sync](
cr: CollectorRegistry[F],
name: Name,
help: String
): F[Histogram[F]] =
noLabelsBuckets(cr, name, help, defaults:_*)
/**
* Constructor for a [[Histogram]] with no labels. Default buckets are [[defaults]]
* and are intended to cover a typical web/rpc request from milliseconds to seconds.
*
* @param cr CollectorRegistry this [[Histogram]] will be registered with
* @param name The name of the Gauge
* @param help The help string of the metric
* @param buckets The buckets to measure observations by.
*/
def noLabelsBuckets[F[_]: Sync](
cr: CollectorRegistry[F],
name: Name,
help: String,
buckets: Double*
): F[Histogram[F]] = for {
c <- Sync[F].delay(
JHistogram.build()
.name(name.getName)
.help(help)
.buckets(buckets:_*)
)
out <- Sync[F].delay(c.register(CollectorRegistry.Unsafe.asJava(cr)))
} yield new NoLabelsHistogram[F](out)
def noLabelsLinearBuckets[F[_]: Sync](
cr: CollectorRegistry[F],
name: Name,
help: String,
start: Double,
factor: Double,
count: Int
): F[Histogram[F]] = for {
c <- Sync[F].delay(
JHistogram.build()
.name(name.getName)
.help(help)
.linearBuckets(start, factor, count)
)
out <- Sync[F].delay(c.register(CollectorRegistry.Unsafe.asJava(cr)))
} yield new NoLabelsHistogram[F](out)
def noLabelsExponentialBuckets[F[_]: Sync](
cr: CollectorRegistry[F],
name: Name, help: String,
start: Double,
factor: Double,
count: Int
): F[Histogram[F]] = for {
c <- Sync[F].delay(
JHistogram.build()
.name(name.getName)
.help(help)
.exponentialBuckets(start, factor, count)
)
out <- Sync[F].delay(c.register(CollectorRegistry.Unsafe.asJava(cr)))
} yield new NoLabelsHistogram[F](out)
/**
* Constructor for a labelled [[Histogram]]. Default buckets are [[defaults]]
* and are intended to cover a typical web/rpc request from milliseconds to seconds.
*
* This generates a specific number of labels via `Sized`, in combination with a function
* to generate an equally `Sized` set of labels from some type. Values are applied by position.
*
* This counter needs to have a label applied to the [[UnlabelledHistogram]] in order to
* be measureable or recorded.
*
* @param cr CollectorRegistry this [[Histogram]] will be registred with
* @param name The name of the [[Histogram]].
* @param help The help string of the metric
* @param labels The name of the labels to be applied to this metric
* @param f Function to take some value provided in the future to generate an equally sized list
* of strings as the list of labels. These are assigned to labels by position.
*/
def labelled[F[_]: Sync, A, N <: Nat](
cr: CollectorRegistry[F],
name: Name,
help: String,
labels: Sized[IndexedSeq[Label], N],
f: A => Sized[IndexedSeq[String], N]
): F[UnlabelledHistogram[F, A]] =
labelledBuckets(cr, name, help, labels, f, defaults:_*)
/**
* Constructor for a labelled [[Histogram]]. Default buckets are [[defaults]]
* and are intended to cover a typical web/rpc request from milliseconds to seconds.
*
* This generates a specific number of labels via `Sized`, in combination with a function
* to generate an equally `Sized` set of labels from some type. Values are applied by position.
*
* This counter needs to have a label applied to the [[UnlabelledHistogram]] in order to
* be measureable or recorded.
*
* @param cr CollectorRegistry this [[Histogram]] will be registred with
* @param name The name of the [[Histogram]].
* @param help The help string of the metric
* @param labels The name of the labels to be applied to this metric
* @param f Function to take some value provided in the future to generate an equally sized list
* of strings as the list of labels. These are assigned to labels by position.
* @param buckets The buckets to measure observations by.
*/
def labelledBuckets[F[_]: Sync, A, N <: Nat](
cr: CollectorRegistry[F],
name: Name,
help: String,
labels: Sized[IndexedSeq[Label], N],
f: A => Sized[IndexedSeq[String], N],
buckets: Double*
): F[UnlabelledHistogram[F, A]] = for {
c <- Sync[F].delay(
JHistogram.build()
.name(name.getName)
.help(help)
.labelNames(labels.map(_.getLabel):_*)
.buckets(buckets:_*)
)
out <- Sync[F].delay(c.register(CollectorRegistry.Unsafe.asJava(cr)))
} yield new UnlabelledHistogram[F, A](out, f.andThen(_.unsized))
def labelledLinearBuckets[F[_]: Sync, A, N <: Nat](
cr: CollectorRegistry[F],
name: Name,
help: String,
labels: Sized[IndexedSeq[Label], N],
f: A => Sized[IndexedSeq[String], N],
start: Double,
factor: Double,
count: Int
): F[UnlabelledHistogram[F, A]] = for {
c <- Sync[F].delay(
JHistogram.build()
.name(name.getName)
.help(help)
.labelNames(labels.map(_.getLabel):_*)
.linearBuckets(start, factor, count)
)
out <- Sync[F].delay(c.register(CollectorRegistry.Unsafe.asJava(cr)))
} yield new UnlabelledHistogram[F, A](out, f.andThen(_.unsized))
def labelledExponentialBuckets[F[_]: Sync, A, N <: Nat](
cr: CollectorRegistry[F],
name: Name,
help: String,
labels: Sized[IndexedSeq[Label], N],
f: A => Sized[IndexedSeq[String], N],
start: Double,
factor: Double,
count: Int
): F[UnlabelledHistogram[F, A]] = for {
c <- Sync[F].delay(
JHistogram.build()
.name(name.getName)
.help(help)
.labelNames(labels.map(_.getLabel):_*)
.exponentialBuckets(start, factor, count)
)
out <- Sync[F].delay(c.register(CollectorRegistry.Unsafe.asJava(cr)))
} yield new UnlabelledHistogram[F, A](out, f.andThen(_.unsized))
private final class NoLabelsHistogram[F[_]: Sync] private[Histogram] (
private[Histogram] val underlying: JHistogram
) extends Histogram[F] {
def observe(d: Double): F[Unit] = Sync[F].delay(underlying.observe(d))
}
private final class LabelledHistogram[F[_]: Sync] private[Histogram] (
private val underlying: JHistogram.Child
) extends Histogram[F] {
def observe(d: Double): F[Unit] = Sync[F].delay(underlying.observe(d))
}
/**
* Generic UnlabelledHistorgram
*
* It is necessary to apply a value of type `A` to this
* histogram to be able to take any measurements.
*/
final class UnlabelledHistogram[F[_]: Sync, A] private[Histogram] (
private[Histogram] val underlying: JHistogram,
private val f: A => IndexedSeq[String]
) {
def label(a: A): Histogram[F] =
new LabelledHistogram[F](underlying.labels(f(a):_*))
}
object Unsafe {
def asJavaUnlabelled[F[_], A](h: UnlabelledHistogram[F, A]): JHistogram =
h.underlying
def asJava[F[_]: ApplicativeError[?[_], Throwable]](c: Histogram[F]): F[JHistogram] = c match {
case _: LabelledHistogram[F] => ApplicativeError[F, Throwable].raiseError(new IllegalArgumentException("Cannot Get Underlying Parent with Labels Applied"))
case n: NoLabelsHistogram[F] => n.underlying.pure[F]
}
}
}