-
Notifications
You must be signed in to change notification settings - Fork 787
/
Dropwizard.scala
157 lines (143 loc) · 6.35 KB
/
Dropwizard.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
package org.http4s.metrics.dropwizard
import cats.effect.Sync
import com.codahale.metrics.MetricRegistry
import java.util.concurrent.TimeUnit
import org.http4s.{Method, Status}
import org.http4s.metrics.MetricsOps
import org.http4s.metrics.TerminationType
import org.http4s.metrics.TerminationType.{Abnormal, Error, Timeout}
/**
* [[MetricsOps]] algebra capable of recording Dropwizard metrics
*
* For example, the following code would wrap a [[org.http4s.HttpRoutes]] with a [[org.http4s.server.middleware.Metrics]]
* that records metrics to a given metric registry.
* {{{
* import org.http4s.client.middleware.Metrics
* import org.http4s.client.dropwizard.Dropwizard
*
* val meteredRoutes = Metrics[IO](Dropwizard(registry, "server"))(testRoutes)
* }}}
*
* Analogously, the following code would wrap a [[org.http4s.client.Client]] with a [[org.http4s.client.middleware.Metrics]]
* that records metrics to a given Metric Registry, classifying the metrics by HTTP method.
* {{{
* import org.http4s.client.metrics.core.Metrics
* import org.http4s.client.metrics.dropwizard.Dropwizard
*
* val classifierFunc = (r: Request[IO]) => Some(r.method.toString.toLowerCase)
* val meteredClient = Metrics(Dropwizard(registry, "client"), classifierFunc)(client)
* }}}
*
* Registers the following metrics:
*
* {prefix}.{classifier}.active.requests - Counter
* {prefix}.{classifier}.requests.headers - Timer
* {prefix}.{classifier}.requests.total - Timer
* {prefix}.{classifier}.get-requests - Timer
* {prefix}.{classifier}.post-requests - Timer
* {prefix}.{classifier}.put-requests - Timer
* {prefix}.{classifier}.head-requests - Timer
* {prefix}.{classifier}.move-requests - Timer
* {prefix}.{classifier}.options-requests - Timer
* {prefix}.{classifier}.trace-requests - Timer
* {prefix}.{classifier}.connect-requests - Timer
* {prefix}.{classifier}.delete-requests - Timer
* {prefix}.{classifier}.other-requests - Timer
* {prefix}.{classifier}.1xx-responses - Timer
* {prefix}.{classifier}.2xx-responses - Timer
* {prefix}.{classifier}.3xx-responses - Timer
* {prefix}.{classifier}.4xx-responses - Timer
* {prefix}.{classifier}.5xx-responses - Timer
* {prefix}.{classifier}.errors - Timer
* {prefix}.{classifier}.timeouts - Timer
* {prefix}.{classifier}.abnormal-terminations - Timer
*/
object Dropwizard {
/**
* Creates a [[MetricsOps]] that supports Dropwizard metrics
*
* @param registry a dropwizard metric registry
* @param prefix a prefix that will be added to all metrics
*/
def apply[F[_]](registry: MetricRegistry, prefix: String = "org.http4s.server")(
implicit F: Sync[F]): MetricsOps[F] =
new MetricsOps[F] {
override def increaseActiveRequests(classifier: Option[String]): F[Unit] = F.delay {
registry.counter(s"${namespace(prefix, classifier)}.active-requests").inc()
}
override def decreaseActiveRequests(classifier: Option[String]): F[Unit] = F.delay {
registry.counter(s"${namespace(prefix, classifier)}.active-requests").dec()
}
override def recordHeadersTime(
method: Method,
elapsed: Long,
classifier: Option[String]): F[Unit] = F.delay {
registry
.timer(s"${namespace(prefix, classifier)}.requests.headers")
.update(elapsed, TimeUnit.NANOSECONDS)
}
override def recordTotalTime(
method: Method,
status: Status,
elapsed: Long,
classifier: Option[String]): F[Unit] =
F.delay {
registry
.timer(s"${namespace(prefix, classifier)}.requests.total")
.update(elapsed, TimeUnit.NANOSECONDS)
registry
.timer(s"${namespace(prefix, classifier)}.${requestTimer(method)}")
.update(elapsed, TimeUnit.NANOSECONDS)
registerStatusCode(status, elapsed, classifier)
}
override def recordAbnormalTermination(
elapsed: Long,
terminationType: TerminationType,
classifier: Option[String]): F[Unit] = terminationType match {
case Abnormal => recordAbnormal(elapsed, classifier)
case Error => recordError(elapsed, classifier)
case Timeout => recordTimeout(elapsed, classifier)
}
private def recordAbnormal(elapsed: Long, classifier: Option[String]): F[Unit] = F.delay {
registry
.timer(s"${namespace(prefix, classifier)}.abnormal-terminations")
.update(elapsed, TimeUnit.NANOSECONDS)
}
private def recordError(elapsed: Long, classifier: Option[String]): F[Unit] = F.delay {
registry
.timer(s"${namespace(prefix, classifier)}.errors")
.update(elapsed, TimeUnit.NANOSECONDS)
}
private def recordTimeout(elapsed: Long, classifier: Option[String]): F[Unit] = F.delay {
registry
.timer(s"${namespace(prefix, classifier)}.timeouts")
.update(elapsed, TimeUnit.NANOSECONDS)
}
private def namespace(prefix: String, classifier: Option[String]): String =
classifier.map(d => s"${prefix}.${d}").getOrElse(s"${prefix}.default")
private def registerStatusCode(status: Status, elapsed: Long, classifier: Option[String]) =
(status.code match {
case hundreds if hundreds < 200 =>
registry.timer(s"${namespace(prefix, classifier)}.1xx-responses")
case twohundreds if twohundreds < 300 =>
registry.timer(s"${namespace(prefix, classifier)}.2xx-responses")
case threehundreds if threehundreds < 400 =>
registry.timer(s"${namespace(prefix, classifier)}.3xx-responses")
case fourhundreds if fourhundreds < 500 =>
registry.timer(s"${namespace(prefix, classifier)}.4xx-responses")
case _ => registry.timer(s"${namespace(prefix, classifier)}.5xx-responses")
}).update(elapsed, TimeUnit.NANOSECONDS)
private def requestTimer(method: Method): String = method match {
case Method.GET => "get-requests"
case Method.POST => "post-requests"
case Method.PUT => "put-requests"
case Method.HEAD => "head-requests"
case Method.MOVE => "move-requests"
case Method.OPTIONS => "options-requests"
case Method.TRACE => "trace-requests"
case Method.CONNECT => "connect-requests"
case Method.DELETE => "delete-requests"
case _ => "other-requests"
}
}
}