Skip to content

Commit a74cdc9

Browse files
committed
add prometheus exporter http server
1 parent 958fc08 commit a74cdc9

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

.idea/artifacts/moztw_space_bot_jar.xml

+13-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ dependencies {
3636
implementation("com.squareup.okhttp3", "okhttp", "4.10.0")
3737
implementation("com.beust", "klaxon", "5.5")
3838
implementation("io.prometheus", "simpleclient", "0.16.0")
39+
implementation("io.prometheus", "simpleclient_hotspot", "0.16.0")
40+
implementation("io.prometheus", "simpleclient_httpserver", "0.16.0")
41+
implementation("org.slf4j", "slf4j-api", "2.0.0")
42+
implementation("org.slf4j", "slf4j-simple", "2.0.0")
3943
testImplementation("org.junit.jupiter", "junit-jupiter-api", "5.8.2")
4044
}
4145

src/main/kotlin/org/moztw/bot/telegram/space/Bot.kt

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.moztw.bot.telegram.space
22

33
import io.prometheus.client.Counter
4+
import io.prometheus.client.Gauge
45
import okhttp3.OkHttpClient
56
import okhttp3.Request
67
import org.moztw.bot.telegram.space.co2.SpaceCo2
@@ -24,10 +25,11 @@ internal class Bot(val username: String, val token: String) : TelegramLongPollin
2425
updates.inc()
2526
println("* [$dateTime] Update received: $update")
2627

27-
if (!(update.hasMessage() && onMessageReceived(update.message) || update.hasCallbackQuery() && onCallbackQueryReceived(update.callbackQuery))) {
28-
updatesUnhandled.inc()
29-
println("* [$dateTime] Update not handled: $update")
30-
}
28+
if (update.hasMessage() && onMessageReceived(update.message)) return
29+
if (update.hasCallbackQuery() && onCallbackQueryReceived((update.callbackQuery))) return
30+
31+
updatesUnhandled.inc()
32+
println("* [$dateTime] Update not handled: $update")
3133
}
3234

3335
private fun onCallbackQueryReceived(callbackQuery: CallbackQuery): Boolean {
@@ -104,9 +106,12 @@ internal class Bot(val username: String, val token: String) : TelegramLongPollin
104106
}
105107
}
106108
}
109+
return true
107110
} else if (isAdminChat(message.chat)) {
108111
if (isCommandOpen(message.text)) {
109112
commandCalls.labels("space_open").inc()
113+
spaceDoorState.set(1.0)
114+
spaceDoorStateTime.setToCurrentTime()
110115
if (tryExecute(Caption().getCaptionOpened(chatId = generalChatId))
111116
&& tryExecute(Reply().getGeneralMessageOpen(chatId = generalChatId, operator = message.from))
112117
&& tryExecute(Reply().getMessageOpen(message = message))) {
@@ -118,6 +123,8 @@ internal class Bot(val username: String, val token: String) : TelegramLongPollin
118123
}
119124
} else if (isCommandClose(message.text)) {
120125
commandCalls.labels("space_close").inc()
126+
spaceDoorState.set(0.0)
127+
spaceDoorStateTime.setToCurrentTime()
121128
if (tryExecute(Caption().getCaptionClosed(chatId = generalChatId))
122129
&& tryExecute(Reply().getGeneralMessageClose(chatId = generalChatId, operator = message.from))
123130
&& tryExecute(Reply().getMessageClose(message = message))) {
@@ -218,5 +225,15 @@ internal class Bot(val username: String, val token: String) : TelegramLongPollin
218225
.help("Total Telegram Bots API calls.")
219226
.labelNames("method")
220227
.register()
228+
229+
val spaceDoorState: Gauge = Gauge.build()
230+
.name("moztw_space_door_state")
231+
.help("The state of MozTW Space Door. 0 = closed, 1 = opened")
232+
.register()
233+
234+
val spaceDoorStateTime: Gauge = Gauge.build()
235+
.name("moztw_space_door_state_time")
236+
.help("The update time for the state of MozTW Space Door.")
237+
.register()
221238
}
222239
}

src/main/kotlin/org/moztw/bot/telegram/space/Main.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.moztw.bot.telegram.space
22

3+
import io.prometheus.client.exporter.HTTPServer
34
import org.apache.commons.cli.*
45
import org.telegram.telegrambots.meta.TelegramBotsApi
56
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException
@@ -30,8 +31,10 @@ fun main(args: Array<String>) {
3031
val botToken = cmd.getOptionValue("token")
3132
val exporterListenPort = cmd.getOptionValue("port", "")
3233

33-
if (exporterListenPort.isNotBlank()){
34-
Exporter.run(exporterListenPort.toInt())
34+
if (exporterListenPort.isNotEmpty()) {
35+
HTTPServer.Builder()
36+
.withPort(exporterListenPort.toInt())
37+
.build()
3538
}
3639

3740
try {

0 commit comments

Comments
 (0)