Created
December 20, 2022 16:13
-
-
Save bilal-fazlani/4d15f1d279885a389aee9c7f6a8143e3 to your computer and use it in GitHub Desktop.
zio-http-premetheus
This file contains 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
import zio.http.* | |
import zio.http.service.* | |
import zio.metrics.connectors.prometheus.* | |
import zio.* | |
import zio.metrics.Metric | |
import zio.metrics.connectors.MetricsConfig | |
import zio.http.model.Method.GET | |
object Metrics extends ZIOAppDefault { | |
def reqCounter(method: String, path: String) = | |
Metric | |
.counterInt("request-counter") | |
.fromConst(1) | |
.tagged("method", method) | |
.tagged("path", path) | |
private val appRoutes = Http.collectZIO[Request] { | |
case GET -> !! / "metrics" => ZIO.serviceWithZIO[PrometheusPublisher](_.get.map(Response.text)) | |
case GET -> !! / "hit" => | |
ZIO.succeed( | |
Response.text("OK") | |
) @@ reqCounter("GET", "/hit") | |
} | |
val run = (for { | |
ticker <- (ZIO.log("tick") @@ reqCounter("NA", "/tick")).schedule(Schedule.spaced(3.second)).fork | |
_ <- ZIO.log("starting web application...") | |
_ <- Server.serve(appRoutes) zipPar ticker.await | |
} yield ()) | |
.provide( | |
Server.live, | |
ServerConfig.live(ServerConfig.default.port(9300)), | |
publisherLayer, | |
prometheusLayer, | |
ZLayer.succeed(MetricsConfig(1.second)) | |
) | |
} |
Author
bilal-fazlani
commented
Dec 20, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment