项目作者: yannick-cw

项目描述 :
Prometheus exporter for http4s
高级语言: Scala
项目地址: git://github.com/yannick-cw/http4s-prometheus.git
创建时间: 2017-11-10T09:50:50Z
项目社区:https://github.com/yannick-cw/http4s-prometheus

开源协议:

下载


http4s-prometheus exporter

Provides instumentation to wrap http4s HttpService[F] with a prometheus metric collecting middleware.

Add to sbt

Build Status
Maven Central

  1. "io.github.yannick-cw" %% "http4s-prometheus" % x.x.x

Usage

Wrap services you want to monitor and provide a metrics service endpoint for prometheus:

  1. import io.github.yannick_cw.http4s_prometheus.{MetricsMiddleware, MetricsService}
  2. private val M = MetricsMiddleware()
  3. val httpService = HttpService[IO] { case GET -> Root / "hi" => Ok() }
  4. val httpService2 = HttpService[IO] { case GET -> Root / "Ups" => NotFound() }
  5. val routing = Router(
  6. "one" -> M.collect("service 1 name", httpService),
  7. "two" -> M.collect("service 2 name", httpService2),
  8. "/metrics" -> MetricsService[IO]()
  9. )

Via /metrics prometheus can now access the http request counter

  1. # HELP http_requests_total Total http requests received
  2. # TYPE http_requests_total counter
  3. http_requests_total{service="service 1 name",status="200",} 10.0

and a request duration histogram in seconds

  1. # HELP http_requests_duration_seconds Histogram of the response time of http requests in Seconds
  2. # TYPE http_requests_duration_seconds histogram
  3. http_requests_duration_seconds_bucket{service="service 2 name",status="404",le="0.01",} 9.0

Additional settings

If you want you can supply the buckets for the Histogram and a custom CollectorRegistry

  1. MetricsMiddleware(List(0.01, 0.025), CollectorRegistry.defaultRegistry)