com.sbuslab.http.directives.MetricsDirectives.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of akka-http-tools Show documentation
Show all versions of akka-http-tools Show documentation
Tools for akka-http (marshaling, error handling, websockets, sbus support)
package com.sbuslab.http.directives
import scala.util.control.NonFatal
import akka.http.scaladsl.server.{Directive, Directives, ExceptionHandler}
import io.prometheus.client.Histogram
object MetricsDirectives {
private val responseTimes = Histogram.build()
.name("http_request_processing_seconds")
.help("Time spent processing request")
.labelNames("endpoint")
.register()
}
trait MetricsDirectives extends Directives {
def metrics(endpoint: String): Directive[Unit] =
extractRequestContext.flatMap { _ ⇒
val timer = MetricsDirectives.responseTimes.labels(endpoint).startTimer()
mapResponse { resp ⇒
timer.observeDuration()
resp
} & handleExceptions(ExceptionHandler {
case NonFatal(e) ⇒
timer.observeDuration()
throw e
})
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy