/
InstrumentationHttpServer.scala
64 lines (53 loc) · 2.03 KB
/
InstrumentationHttpServer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.core
import zhttp.service.Server
import zio.Runtime
import zio.ZIO
import zio.ZLayer
import zio.metrics.connectors.MetricsConfig
import zio.metrics.connectors.prometheus
import zio.metrics.jvm.DefaultJvmMetrics
import org.knora.webapi.config.AppConfig
import org.knora.webapi.instrumentation.health.HealthApp
import org.knora.webapi.instrumentation.index.IndexApp
import org.knora.webapi.instrumentation.prometheus.PrometheusApp
object InstrumentationHttpServer {
private val routes =
for {
index <- ZIO.service[IndexApp].map(_.route)
health <- ZIO.service[HealthApp].map(_.route)
prometheus <- ZIO.service[PrometheusApp].map(_.route)
} yield index ++ health ++ prometheus
private val run =
for {
config <- ZIO.service[AppConfig]
r <- routes
_ <- Server.start(config.instrumentationServerConfig.port, r).forkDaemon
_ <- ZIO.logInfo(s"Starting instrumentation http server on port: ${config.instrumentationServerConfig.port}")
} yield ()
val make: ZIO[AppConfig with State, Throwable, Unit] =
ZIO
.service[AppConfig]
.flatMap(config =>
run
.provideSome[AppConfig with State](
// HttpApp implementation layers
IndexApp.layer,
HealthApp.layer,
PrometheusApp.layer,
// Metrics config
ZLayer.succeed(MetricsConfig(config.instrumentationServerConfig.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
)
)
}