Kord-Extensions/kord-extensions

Sentry Integration MessageUpdateEvent sending multiple requests

Closed this issue · 4 comments

Description

When receiving a MessageUpdateEvent while having sentry enabled, it seems that sentry makes a lot of requests for something. This happens for messages that have been sent before the bot was turned on.
Adding any checks to this event will make even more requests.

This problem causes the program to spam Discord's API and gets us rate-limited really quickly.

Versions

Kotlin 1.5, Kord-ex 1.4.1 Snapshot

Reproduction

This problem can be reproduced with these pieces of code

suspend fun main() {
    val bot = ExtensibleBot(env("token")!!) {
        intents {
            +Intent.Guilds
            +Intent.GuildMessages
        }

        extensions {
            add(::TestExtension)
        }

        hooks {
            afterKoinSetup {
                getKoin().get<SentryAdapter>().init {
                    dsn = env("sentry")!!
                    tracesSampleRate = 1.0
                    environment = "production"
                }
            }
        }
    }

    bot.start()
}
class TestExtension: Extension() {
    override val name: String
        get() = "test"

    override suspend fun setup() {
        event<MessageUpdateEvent> {
            action {
                println("message updated")
            }
        }
    }
}

Reproduction steps:

  1. Send a message (before starting the bot)
  2. Start the bot
  3. Edit the message

These are my logs when I receive a MessageUpdateEvent from a message that has been sent before the bot has been loaded.
https://hastebin.com/iwodimovug.sql

These are my logs when I receive a MesasgeUpdateEvent from a message that has been sent before the bot has been loaded, but with the sentry hook removed.
https://hastebin.com/bisobetote.css

Having talked about this with the Kord team yesterday, the issue is that Kord does not cache entities retrieved from REST. This means that the .asMessage() that is sometimes called under the hood will get the message from REST, and just never cache it because.. well, Kord doesn't.

I think @BartArys may be looking into alternative approaches, but for the time being there isn't a whole ton we can do about this. While it is technically possible for the Sentry integration to add the message object to the cache, this seems like it would be way out of scope and an integration of this type really should not be changing how the cache works.

Kord updated to add a rest-caching fallback strategy, which is now the default in KordEx. See if you still have this problem with 1.4.2-SNAPSHOT, would you?

It seems to have solved the problem, the bot isn't spamming requests anymore

Excellent, thanks for checking!