From 3ea0f4eacb6856970822b7db5d8ed43762ea9a82 Mon Sep 17 00:00:00 2001 From: LamGC Date: Tue, 21 Feb 2023 18:32:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(metrics):=20=E6=8C=87=E6=A0=87=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E5=A2=9E=E5=8A=A0=20bot=5Fid=20label.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 由于 bot_name 不能作为机器人的唯一标识, 因此增加 bot_id 为监控平台提供唯一标识. --- scalabot-app/src/main/kotlin/ScalaBot.kt | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/scalabot-app/src/main/kotlin/ScalaBot.kt b/scalabot-app/src/main/kotlin/ScalaBot.kt index 1a395c4..dc72e17 100644 --- a/scalabot-app/src/main/kotlin/ScalaBot.kt +++ b/scalabot-app/src/main/kotlin/ScalaBot.kt @@ -50,6 +50,8 @@ internal class ScalaBot( extensionFinders = extensionFinders ) + private val accountIdString = accountId.toString() + init { log.info { "[Bot $botUsername] 正在加载扩展..." } val extensionEntries = extensionLoader.getExtensions() @@ -66,18 +68,18 @@ internal class ScalaBot( override fun creatorId(): Long = creatorId override fun onUpdateReceived(update: Update?) { - botUpdateCounter.labels(botUsername).inc() - botUpdateGauge.labels(botUsername).inc() + botUpdateCounter.labels(botUsername, accountIdString).inc() + botUpdateGauge.labels(botUsername, accountIdString).inc() - val timer = updateProcessTime.labels(botUsername).startTimer() + val timer = updateProcessTime.labels(botUsername, accountIdString).startTimer() try { super.onUpdateReceived(update) } catch (e: Exception) { - exceptionHandlingCounter.labels(botUsername).inc() + exceptionHandlingCounter.labels(botUsername, accountIdString).inc() throw e } finally { timer.observeDuration() - botUpdateGauge.labels(botUsername).dec() + botUpdateGauge.labels(botUsername, accountIdString).dec() } } @@ -134,7 +136,7 @@ internal class ScalaBot( private val botUpdateCounter = Counter.build() .name("updates_total") .help("Total number of updates received by all bots.") - .labelNames("bot_name") + .labelNames("bot_name", "bot_id") .namespace(Const.METRICS_NAMESPACE) .subsystem("telegrambots") .register() @@ -143,7 +145,7 @@ internal class ScalaBot( private val botUpdateGauge = Gauge.build() .name("updates_in_progress") .help("Number of updates in process by all bots.") - .labelNames("bot_name") + .labelNames("bot_name", "bot_id") .namespace(Const.METRICS_NAMESPACE) .subsystem("telegrambots") .register() @@ -164,7 +166,7 @@ internal class ScalaBot( "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") + .labelNames("bot_name", "bot_id") .namespace(Const.METRICS_NAMESPACE) .subsystem("telegrambots") .register() @@ -173,7 +175,7 @@ internal class ScalaBot( private val exceptionHandlingCounter = Counter.build() .name("updates_exception_handling") .help("Number of exceptions during processing.") - .labelNames("bot_name") + .labelNames("bot_name", "bot_id") .namespace(Const.METRICS_NAMESPACE) .subsystem("telegrambots") .register()