Created
March 24, 2017 17:21
-
-
Save labe-me/39466221bf9748eed3b3d10162c39742 to your computer and use it in GitHub Desktop.
Akka http prometheus request latency and count instrumentation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val requestCount = io.prometheus.client.Counter.build().name("http_requests").help("HTTP requests").register() | |
val requestLatency = io.prometheus.client.Histogram.build().name("http_latency").help("HTTP requests latency").labelNames("code").register() | |
/* | |
Example usage: | |
trackPrometheusMetrics { | |
complete("OK") | |
} | |
*/ | |
def trackPrometheusMetrics(inner: Route) = { | |
// "extract" request start time | |
extract(_ => System.currentTimeMillis()){ start => | |
// increment rqt counter | |
requestCount.inc() | |
// abuse mapResponse to add our side effect | |
mapResponse({ r => | |
requestLatency | |
.labels(r.status.intValue().toString) | |
.observe((System.currentTimeMillis() - start).toDouble) | |
r | |
}){ | |
inner | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment