mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-07-03 05:47:24 +00:00
feat(metrics): 初步增加获取运行指标的功能(兼容 Prometheus).
添加兼容 Prometheus 的运行指标收集导出功能, 通过内置 HttpServer 将其导出. 默认关闭.
This commit is contained in:
@ -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 {
|
||||
|
Reference in New Issue
Block a user