Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: truncate webhook error max size + log webhook send exceptions #621

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion canary/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import aiosqlite
import contextlib
import logging
import sys
import traceback

from canary.config import Config
Expand Down Expand Up @@ -71,10 +72,22 @@ def __init__(self, webhook_id, webhook_token, username=None):
self.username = username or "Bot Logs"
logging.Handler.__init__(self)
self.webhook = Webhook.partial(webhook_id, webhook_token, adapter=RequestsWebhookAdapter())
self.max_webhook_payload_size: int = 1800

def emit(self, record):
msg = self.format(record)
self.webhook.send(f"```\n{msg}```", username=self.username)
try:
self.webhook.send(
f"```\n{msg[:self.max_webhook_payload_size]}"
f"{'[...]' if len(msg) > self.max_webhook_payload_size else ''}```",
username=self.username,
)
except Exception as e:
logger.critical(
"An exception (%s) was encountered while trying to send a log message to a webhook:", str(e)
)
logger.critical(traceback.format_exc())
logger.critical("The attempted log message was: %s", msg)


if config.dev_log_webhook_id and config.dev_log_webhook_token:
Expand Down
Loading