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
refactor: add metrics endpoint (DEV-1555) #2331
Changes from 1 commit
cd59c52
66ed2cf
0134dd7
be422c2
d05d66f
c31f616
acb209a
b27f8f7
75458e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,6 +9,7 @@ import zio._ | |||||||||||||
import zio.logging.backend.SLF4J | ||||||||||||||
|
||||||||||||||
import org.knora.webapi.core._ | ||||||||||||||
import org.knora.webapi.instrumentation.metrics.PrometheusServer | ||||||||||||||
|
||||||||||||||
object Main extends ZIOApp { | ||||||||||||||
|
||||||||||||||
|
@@ -30,6 +31,10 @@ object Main extends ZIOApp { | |||||||||||||
] = ZLayer.empty ++ Runtime.removeDefaultLoggers ++ SLF4J.slf4j ++ LayersLive.dspLayersLive | ||||||||||||||
|
||||||||||||||
/* Here we start our Application */ | ||||||||||||||
override def run = AppServer.live.launch | ||||||||||||||
|
||||||||||||||
override def run = for { | ||||||||||||||
f1 <- PrometheusServer.make.forkDaemon | ||||||||||||||
f2 <- (AppServer.live *> ZIO.never).forkDaemon | ||||||||||||||
_ <- f1.join | ||||||||||||||
_ <- f2.join | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
How about join in the same lines? |
||||||||||||||
} yield () | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.knora.webapi.instrumentation.metrics | ||
|
||
import zhttp.html._ | ||
import zhttp.http._ | ||
import zhttp.service.EventLoopGroup | ||
import zhttp.service.Server | ||
import zhttp.service.server.ServerChannelFactory | ||
import zio.Runtime | ||
import zio.ZIO | ||
import zio.ZLayer | ||
import zio.metrics.connectors.MetricsConfig | ||
import zio.metrics.connectors.prometheus | ||
import zio.metrics.connectors.prometheus.PrometheusPublisher | ||
import zio.metrics.jvm.DefaultJvmMetrics | ||
|
||
import org.knora.webapi.config.AppConfig | ||
|
||
object PrometheusServer { | ||
|
||
private val nThreads = 5 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this not coming from the config? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what it does exactly. I still need to figure that out first. Maybe we don't need it. |
||
|
||
private lazy val indexPage = | ||
"""<html> | ||
|<title>Simple Server</title> | ||
|<body> | ||
|<p><a href="/metrics">Prometheus Metrics</a></p> | ||
|</body | ||
|</html>""".stripMargin | ||
|
||
private lazy val static = | ||
Http.collect[Request] { case Method.GET -> !! => Response.html(Html.fromString(indexPage)) } | ||
|
||
private lazy val prometheusRouter = | ||
Http | ||
.collectZIO[Request] { case Method.GET -> !! / "metrics" => | ||
ZIO.serviceWithZIO[PrometheusPublisher](_.get.map(Response.text)) | ||
} | ||
|
||
private def server(config: AppConfig): Server[PrometheusPublisher, Throwable] = | ||
Server.port(config.prometheusServerConfig.port) ++ Server.app(static ++ prometheusRouter) | ||
|
||
private def runHttpServer(config: AppConfig) = | ||
ZIO.logInfo("starting prometheus server in a separate root fiber.") *> | ||
server(config).start | ||
|
||
val make: ZIO[AppConfig, Throwable, Nothing] = | ||
ZIO | ||
.service[AppConfig] | ||
.flatMap(config => | ||
runHttpServer(config) | ||
.provide( | ||
ServerChannelFactory.auto, | ||
EventLoopGroup.auto(nThreads), | ||
|
||
// Metrics config | ||
ZLayer.succeed(MetricsConfig(config.prometheusServerConfig.interval)), | ||
|
||
// The prometheus reporting layer | ||
prometheus.publisherLayer, | ||
prometheus.prometheusLayer, | ||
|
||
// Enable the ZIO internal metrics and the default JVM metricsConfig | ||
// Do NOT forget the .unit for the JVM metrics layer | ||
Runtime.enableRuntimeMetrics, | ||
Runtime.enableFiberRoots, | ||
DefaultJvmMetrics.live.unit.orDie | ||
) | ||
) | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to leave
AppServer.live.launch
instead ofAppServer.live *> ZIO.never
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.launch
is a method onZLayer
. It does exactly the same.