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,8 @@
package net.lamgc.scalabot
import io.prometheus.client.Counter
import io.prometheus.client.Gauge
import io.prometheus.client.Summary
import mu.KotlinLogging
import org.eclipse.aether.artifact.Artifact
import org.telegram.abilitybots.api.bot.AbilityBot
@ -7,6 +10,7 @@ import org.telegram.abilitybots.api.db.DBContext
import org.telegram.abilitybots.api.toggle.BareboneToggle
import org.telegram.abilitybots.api.toggle.DefaultToggle
import org.telegram.telegrambots.bots.DefaultBotOptions
import org.telegram.telegrambots.meta.api.objects.Update
internal class ScalaBot(
name: String,
@ -22,6 +26,51 @@ internal class ScalaBot(
companion object {
@JvmStatic
private val log = KotlinLogging.logger { }
// ------------- Metrics -------------
@JvmStatic
private val botUpdateCounter = Counter.build()
.name("updates_total")
.help("Total number of updates received by all bots.")
.labelNames("bot_name")
.subsystem("telegrambots")
.register()
@JvmStatic
private val botUpdateGauge = Gauge.build()
.name("updates_in_progress")
.help("Number of updates in process by all bots.")
.labelNames("bot_name")
.subsystem("telegrambots")
.register()
@JvmStatic
private val onlineBotGauge = Gauge.build()
.name("bots_online")
.help("Number of bots Online.")
.subsystem("telegrambots")
.register()
@JvmStatic
private val updateProcessTime = Summary.build()
.name("update_process_duration_seconds")
.help(
"Time to process update. (This indicator includes the pre-processing of update by TelegrammBots, " +
"so it may be different from the actual execution time of ability. " +
"It is not recommended to use it as the accurate execution time of ability)"
)
.labelNames("bot_name")
.subsystem("telegrambots")
.register()
@JvmStatic
private val exceptionHandlingCounter = Counter.build()
.name("updates_exception_handling")
.help("Number of exceptions during processing.")
.labelNames("bot_name")
.subsystem("telegrambots")
.register()
}
private val extensionLoader = ExtensionLoader(this)
@ -38,4 +87,30 @@ internal class ScalaBot(
}
override fun creatorId(): Long = creatorId
override fun onUpdateReceived(update: Update?) {
botUpdateCounter.labels(botUsername).inc()
botUpdateGauge.labels(botUsername).inc()
val timer = updateProcessTime.labels(botUsername).startTimer()
try {
super.onUpdateReceived(update)
} catch (e: Exception) {
exceptionHandlingCounter.labels(botUsername).inc()
throw e
} finally {
timer.observeDuration()
botUpdateGauge.labels(botUsername).dec()
}
}
override fun onRegister() {
super.onRegister()
onlineBotGauge.inc()
}
override fun onClosing() {
super.onClosing()
onlineBotGauge.dec()
}
}