aboutsummaryrefslogtreecommitdiffstats
path: root/arthur
diff options
context:
space:
mode:
authorGravatar ChrisJL <[email protected]>2024-02-14 09:17:02 +0000
committerGravatar GitHub <[email protected]>2024-02-14 09:17:02 +0000
commit6d5ba1374d925100bf850dc5c4a6a99fd03c20e9 (patch)
tree85bed8ad6d7e7d881c2568181df54d95b11601eb /arthur
parentCorrect typos in Grafana API wrapper doc strings (diff)
parentadd a sentry release workflow (diff)
Merge pull request #149 from python-discord/integrate-sentry
Integrate sentry into king arthur
Diffstat (limited to 'arthur')
-rw-r--r--arthur/__init__.py4
-rw-r--r--arthur/__main__.py5
-rw-r--r--arthur/bot.py13
-rw-r--r--arthur/config.py6
-rw-r--r--arthur/exts/grafana/team_sync.py2
-rw-r--r--arthur/log.py27
6 files changed, 50 insertions, 7 deletions
diff --git a/arthur/__init__.py b/arthur/__init__.py
index 269a320..a163be5 100644
--- a/arthur/__init__.py
+++ b/arthur/__init__.py
@@ -2,17 +2,13 @@
import asyncio
import os
-from functools import partial
from typing import TYPE_CHECKING
-import loguru
from pydis_core.utils import apply_monkey_patches
if TYPE_CHECKING:
from arthur.bot import KingArthur
-logger = loguru.logger.opt(colors=True)
-logger.opt = partial(logger.opt, colors=True)
apply_monkey_patches()
diff --git a/arthur/__main__.py b/arthur/__main__.py
index 53e1fb3..041399b 100644
--- a/arthur/__main__.py
+++ b/arthur/__main__.py
@@ -9,6 +9,7 @@ from discord.ext import commands
import arthur
from arthur.bot import KingArthur
from arthur.config import CONFIG
+from arthur.log import logger, setup_sentry
async def main() -> None:
@@ -34,5 +35,7 @@ async def main() -> None:
await bot.start(CONFIG.token.get_secret_value())
-with arthur.logger.catch():
+setup_sentry()
+
+with logger.catch():
asyncio.run(main())
diff --git a/arthur/bot.py b/arthur/bot.py
index 8acf6fe..9a51298 100644
--- a/arthur/bot.py
+++ b/arthur/bot.py
@@ -8,9 +8,11 @@ from discord.ext import commands
from kubernetes_asyncio import config
from kubernetes_asyncio.config.kube_config import KUBE_CONFIG_DEFAULT_LOCATION
from pydis_core import BotBase
+from sentry_sdk import push_scope
-from arthur import exts, logger
+from arthur import exts
from arthur.config import CONFIG
+from arthur.log import logger
class KingArthur(BotBase):
@@ -60,3 +62,12 @@ class KingArthur(BotBase):
return False
return CONFIG.devops_role in [r.id for r in user.roles]
+
+ async def on_error(self, event_name: str, *args: Any, **kwargs: Any) -> None:
+ """Log errors raised in event listeners."""
+ with push_scope() as scope:
+ scope.set_tag("event", event_name)
+ scope.set_extra("args", args)
+ scope.set_extra("kwargs", kwargs)
+
+ logger.exception(f"Unhandled exception during event: {event_name}.")
diff --git a/arthur/config.py b/arthur/config.py
index e9f80d9..c4f7e1a 100644
--- a/arthur/config.py
+++ b/arthur/config.py
@@ -1,5 +1,7 @@
"""Utilities for interacting with the config for King Arthur."""
+from os import environ
+
import pydantic
from pydantic_settings import BaseSettings
@@ -23,6 +25,10 @@ class Config(
devops_role: int = 409416496733880320
guild_id: int = 267624335836053506
+ sentry_dsn: str = ""
+
+
+GIT_SHA = environ.get("GIT_SHA", "development")
CONFIG = Config()
diff --git a/arthur/exts/grafana/team_sync.py b/arthur/exts/grafana/team_sync.py
index 289cebd..20e059e 100644
--- a/arthur/exts/grafana/team_sync.py
+++ b/arthur/exts/grafana/team_sync.py
@@ -4,9 +4,9 @@ import aiohttp
import discord
from discord.ext import commands, tasks
-from arthur import logger
from arthur.apis import github, grafana
from arthur.bot import KingArthur
+from arthur.log import logger
@dataclass(frozen=True)
diff --git a/arthur/log.py b/arthur/log.py
new file mode 100644
index 0000000..c382479
--- /dev/null
+++ b/arthur/log.py
@@ -0,0 +1,27 @@
+from functools import partial
+
+import loguru
+import sentry_sdk
+from sentry_sdk.integrations.loguru import LoggingLevels, LoguruIntegration
+
+from arthur.config import CONFIG, GIT_SHA
+
+logger = loguru.logger.opt(colors=True)
+logger.opt = partial(logger.opt, colors=True)
+
+
+def setup_sentry() -> None:
+ """Set up the Sentry logging integrations."""
+ loguru_integration = LoguruIntegration(
+ level=LoggingLevels.DEBUG.value, event_level=LoggingLevels.WARNING.value
+ )
+
+ sentry_sdk.init(
+ dsn=CONFIG.sentry_dsn,
+ integrations=[
+ loguru_integration,
+ ],
+ release=f"king-arthur@{GIT_SHA}",
+ traces_sample_rate=0.5,
+ profiles_sample_rate=0.5,
+ )