Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus Effect suspension #2370

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/src/main/tut/client.md
Expand Up @@ -271,7 +271,9 @@ implicit val clock = Clock.create[IO]
val registry = new CollectorRegistry()
val requestMethodClassifier = (r: Request[IO]) => Some(r.method.toString.toLowerCase)

val meteredClient = Metrics[IO](Prometheus(registry, "prefix"), requestMethodClassifier)(httpClient)
val meteredClient = Prometheus[IO](registry, "prefix").map(
Metrics[IO](_, requestMethodClassifier)(httpClient)
)
```


Expand Down
4 changes: 3 additions & 1 deletion docs/src/main/tut/middleware.md
Expand Up @@ -205,7 +205,9 @@ import io.prometheus.client.CollectorRegistry
implicit val clock = Clock.create[IO]
val registry = new CollectorRegistry()

val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(apiService)
val meteredRoutes = Prometheus[IO](registry, "server").map(
Metrics[IO](_)(apiService)
)
```

[service]: ../service
Expand Down
Expand Up @@ -62,7 +62,7 @@ object Prometheus {
* * @param prefix a prefix that will be added to all metrics
**/
def apply[F[_]](registry: CollectorRegistry, prefix: String = "org_http4s_server")(
implicit F: Sync[F]): MetricsOps[F] =
implicit F: Sync[F]): F[MetricsOps[F]] = F.delay {
new MetricsOps[F] {

override def increaseActiveRequests(classifier: Option[String]): F[Unit] = F.delay {
Expand Down Expand Up @@ -180,6 +180,7 @@ object Prometheus {
.register(registry)
)
}
}
}

case class MetricsCollection(
Expand Down
Expand Up @@ -19,9 +19,9 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register a 2xx response" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_)(client))

val resp = meteredClient.expect[String]("ok").attempt.unsafeRunSync()
val resp = meteredClient.flatMap(_.expect[String]("ok")).attempt.unsafeRunSync()

resp must beRight { contain("200 OK") }
count(registry, "2xx_responses", "client") must beEqualTo(1)
Expand All @@ -33,10 +33,11 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register a 4xx response" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val prom = Prometheus[IO](registry, "client")
val meteredClient = prom.map(Metrics(_)(client))

val resp =
meteredClient.expect[String]("bad-request").attempt.unsafeRunSync()
meteredClient.flatMap(_.expect[String]("bad-request")).attempt.unsafeRunSync()

resp must beLeft { (e: Throwable) =>
e must beLike { case UnexpectedStatus(Status(400)) => ok }
Expand All @@ -50,10 +51,10 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register a 5xx response" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_)(client))

val resp =
meteredClient.expect[String]("internal-server-error").attempt.unsafeRunSync()
meteredClient.flatMap(_.expect[String]("internal-server-error")).attempt.unsafeRunSync()

resp must beLeft { (e: Throwable) =>
e must beLike { case UnexpectedStatus(Status(500)) => ok }
Expand All @@ -67,9 +68,9 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register a GET request" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_)(client))

val resp = meteredClient.expect[String]("ok").attempt.unsafeRunSync()
val resp = meteredClient.flatMap(_.expect[String]("ok")).attempt.unsafeRunSync()

resp must beRight { contain("200 OK") }
count(registry, "2xx_responses", "client", "get") must beEqualTo(1)
Expand All @@ -81,10 +82,12 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register a POST request" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_)(client))

val resp = meteredClient
.expect[String](Request[IO](POST, Uri.unsafeFromString("ok")))
.flatMap(
_.expect[String](Request[IO](POST, Uri.unsafeFromString("ok")))
)
.attempt
.unsafeRunSync()

Expand All @@ -98,10 +101,10 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register a PUT request" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_)(client))

val resp = meteredClient
.expect[String](Request[IO](PUT, Uri.unsafeFromString("ok")))
.flatMap(_.expect[String](Request[IO](PUT, Uri.unsafeFromString("ok"))))
.attempt
.unsafeRunSync()

Expand All @@ -115,10 +118,10 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register a DELETE request" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_)(client))

val resp = meteredClient
.expect[String](Request[IO](DELETE, Uri.unsafeFromString("ok")))
.flatMap(_.expect[String](Request[IO](DELETE, Uri.unsafeFromString("ok"))))
.attempt
.unsafeRunSync()

Expand All @@ -132,10 +135,10 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register an error" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_)(client))

val resp =
meteredClient.expect[String]("error").attempt.unsafeRunSync()
meteredClient.flatMap(_.expect[String]("error")).attempt.unsafeRunSync()

resp must beLeft { (e: Throwable) =>
e must beAnInstanceOf[IOException]
Expand All @@ -147,10 +150,10 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
"register a timeout" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"))(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_)(client))

val resp =
meteredClient.expect[String]("timeout").attempt.unsafeRunSync()
meteredClient.flatMap(_.expect[String]("timeout")).attempt.unsafeRunSync()

resp must beLeft { (e: Throwable) =>
e must beAnInstanceOf[TimeoutException]
Expand All @@ -163,9 +166,9 @@ class PrometheusClientMetricsSpec extends Http4sSpec {
implicit val clock = FakeClock[IO]
val classifier = (_: Request[IO]) => Some("classifier")
val registry: CollectorRegistry = new CollectorRegistry()
val meteredClient = Metrics(Prometheus(registry, "client"), classifier)(client)
val meteredClient = Prometheus(registry, "client").map(Metrics(_, classifier)(client))

val resp = meteredClient.expect[String]("ok").attempt.unsafeRunSync()
val resp = meteredClient.flatMap(_.expect[String]("ok")).attempt.unsafeRunSync()

resp must beRight { contain("200 OK") }
count(registry, "2xx_responses", "client", "get", "classifier") must beEqualTo(1)
Expand Down
Expand Up @@ -16,10 +16,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register a 2xx response" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](uri = uri("/ok"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.Ok)
resp must haveBody("200 OK")
Expand All @@ -32,10 +32,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register a 4xx response" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](uri = uri("/bad-request"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.BadRequest)
resp must haveBody("400 Bad Request")
Expand All @@ -48,10 +48,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register a 5xx response" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](uri = uri("/internal-server-error"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.InternalServerError)
resp must haveBody("500 Internal Server Error")
Expand All @@ -64,10 +64,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register a GET request" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](method = GET, uri = uri("/ok"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.Ok)
resp must haveBody("200 OK")
Expand All @@ -80,10 +80,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register a POST request" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](method = POST, uri = uri("/ok"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.Ok)
resp must haveBody("200 OK")
Expand All @@ -96,10 +96,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register a PUT request" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](method = PUT, uri = uri("/ok"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.Ok)
resp must haveBody("200 OK")
Expand All @@ -112,10 +112,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register a DELETE request" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](method = DELETE, uri = uri("/ok"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.Ok)
resp must haveBody("200 OK")
Expand All @@ -128,10 +128,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register an error" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](method = GET, uri = uri("/error"))

val resp = meteredRoutes.orNotFound(req).attempt.unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).attempt.unsafeRunSync

resp must beLeft
count(registry, "errors", "server") must beEqualTo(1)
Expand All @@ -143,10 +143,10 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
"register an abnormal termination" in {
implicit val clock = FakeClock[IO]
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes = Metrics[IO](Prometheus(registry, "server"))(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(Metrics(_)(testRoutes))
val req = Request[IO](method = GET, uri = uri("/abnormal-termination"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.Ok)
resp.body.attempt.compile.lastOrError.unsafeRunSync must beLeft
Expand All @@ -160,11 +160,11 @@ class PrometheusServerMetricsSpec extends Http4sSpec {
implicit val clock = FakeClock[IO]
val classifierFunc = (_: Request[IO]) => Some("classifier")
val registry: CollectorRegistry = new CollectorRegistry()
val meteredRoutes =
Metrics[IO](ops = Prometheus(registry, "server"), classifierF = classifierFunc)(testRoutes)
val meteredRoutes = Prometheus(registry, "server").map(op =>
Metrics[IO](ops = op, classifierF = classifierFunc)(testRoutes))
val req = Request[IO](uri = uri("/ok"))

val resp = meteredRoutes.orNotFound(req).unsafeRunSync
val resp = meteredRoutes.flatMap(_.orNotFound(req)).unsafeRunSync

resp must haveStatus(Status.Ok)
resp must haveBody("200 OK")
Expand Down