feat(metrics): 初步增加获取运行指标的功能(兼容 Prometheus).

添加兼容 Prometheus 的运行指标收集导出功能, 通过内置 HttpServer 将其导出.
默认关闭.
This commit is contained in:
2022-02-17 19:06:28 +08:00
parent beb3fd8280
commit d85ee024cb
4 changed files with 105 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package net.lamgc.scalabot
import io.prometheus.client.exporter.HTTPServer
import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
import net.lamgc.scalabot.util.registerShutdownHook
@ -18,11 +19,30 @@ fun main(args: Array<String>): Unit = runBlocking {
log.info { "ScalaBot 正在启动中..." }
log.debug { "启动参数: ${args.joinToString(prefix = "[", postfix = "]")}" }
initialFiles()
if (Const.config.metrics.enable) {
startMetricsServer()
}
if (!launcher.launch()) {
exitProcess(1)
}
}
/**
* 启动运行指标服务器.
* 使用 Prometheus 指标格式.
*/
fun startMetricsServer() {
val builder = HTTPServer.Builder()
.withDaemonThreads(true)
.withPort(Const.config.metrics.port)
.withHostname(Const.config.metrics.bindAddress)
val httpServer = builder
.build()
.registerShutdownHook()
log.info { "运行指标服务器已启动. (Port: ${httpServer.port})" }
}
internal class Launcher : AutoCloseable {
companion object {