Skip to content

Commit

Permalink
Add metrics for announcement service
Browse files Browse the repository at this point in the history
  • Loading branch information
NovaFox161 committed Oct 14, 2023
1 parent c2fb96e commit 85ee29b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.reactor.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import kotlinx.coroutines.reactor.mono
import org.dreamexposure.discal.client.message.embed.AnnouncementEmbed
import org.dreamexposure.discal.core.business.MetricService
import org.dreamexposure.discal.core.database.DatabaseManager
import org.dreamexposure.discal.core.entities.Calendar
import org.dreamexposure.discal.core.entities.Event
Expand All @@ -26,14 +27,16 @@ import org.dreamexposure.discal.core.utils.GlobalVal
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component
import org.springframework.util.StopWatch
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.time.Duration
import java.util.concurrent.ConcurrentHashMap

@Component
class AnnouncementService(
private val discordClient: GatewayDiscordClient
private val discordClient: GatewayDiscordClient,
private val metricService: MetricService,
) : ApplicationRunner {
private val maxDifferenceMs = Duration.ofMinutes(5).toMillis()

Expand All @@ -50,7 +53,13 @@ class AnnouncementService(

// Runner
private fun doAnnouncementCycle(): Mono<Void> {
val taskTimer = StopWatch()
taskTimer.start()

return discordClient.guilds.flatMap { guild ->
val guildTimer = StopWatch()
guildTimer.start()

mono {
val announcements = DatabaseManager.getEnabledAnnouncements(guild.id).awaitSingle()
announcements.forEach { announcement ->
Expand All @@ -62,13 +71,20 @@ class AnnouncementService(
}
}.doOnError {
LOGGER.error(GlobalVal.DEFAULT, "Announcement error", it)
}.onErrorResume { Mono.empty() }
}.onErrorResume {
Mono.empty()
}.doFinally {
guildTimer.stop()
metricService.recordAnnouncementTaskDuration("guild", guildTimer.totalTimeMillis)
}
}.doOnError {
LOGGER.error(GlobalVal.DEFAULT, "Announcement error", it)
}.onErrorResume {
Mono.empty()
}.doFinally {
cached.clear()
taskTimer.stop()
metricService.recordAnnouncementTaskDuration("overall", taskTimer.totalTimeMillis)
}.then()
/*
// Get announcements for this shard, then group by guild to make caching easier
Expand Down Expand Up @@ -195,6 +211,8 @@ class AnnouncementService(
// Channel announcement should post to was deleted
.flatMap { DatabaseManager.deleteAnnouncement(announcement.id) }
.then(Mono.empty())
}.doFinally {
metricService.incrementAnnouncementPosted()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,17 @@ class MetricService(
listOf(Tag.of("handler", handler), Tag.of("type", type))
).record(Duration.ofMillis(duration))
}

fun recordAnnouncementTaskDuration(scope: String, duration: Long) {
meterRegistry.timer(
"bot.discal.announcement.task.duration",
listOf(Tag.of("scope", scope)),
).record(Duration.ofMillis(duration))
}

fun incrementAnnouncementPosted() {
meterRegistry.counter(
"bot.discal.announcement.posted",
).increment()
}
}

0 comments on commit 85ee29b

Please sign in to comment.