Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Add Sentry integration #3

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions lokiunimore/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,16 @@ def OIDC_EMAIL_REGEX(val: str) -> re.Pattern:
return re.compile(val)


@config.optional()
def SENTRY_DSN(val: str | None) -> str | None:
"""
The DSN to use to connect to Sentry.
If not specified, the Sentry integration is disabled.
https://docs.sentry.io/platforms/python/
"""
return val


__all__ = (
"config",
"MATRIX_HOMESERVER",
Expand All @@ -223,4 +233,5 @@ def OIDC_EMAIL_REGEX(val: str) -> re.Pattern:
"OIDC_API_BASE_URL",
"OIDC_SCOPES",
"OIDC_EMAIL_REGEX",
"SENTRY_DSN",
)
3 changes: 3 additions & 0 deletions lokiunimore/matrix/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
from lokiunimore.utils.logs import install_log_handler
from lokiunimore.matrix.client import LokiClient
from lokiunimore.config import MATRIX_USER_SECRET, MATRIX_HOMESERVER, MATRIX_USER_ID, SQLALCHEMY_DATABASE_URL
from lokiunimore.utils.errors import install_sentry

install_log_handler()
install_sentry()

loop = asyncio.new_event_loop()


Expand Down
333 changes: 170 additions & 163 deletions lokiunimore/matrix/client.py

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions lokiunimore/utils/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sentry_sdk
import sentry_sdk._types as sentry_typing
import sentry_sdk.integrations.flask
import sentry_sdk.integrations.sqlalchemy
import pkg_resources
import socket

from lokiunimore.config import SENTRY_DSN


version = pkg_resources.get_distribution("lokiunimore").version


def clean_events(event: sentry_typing.Event, hint: sentry_typing.Hint):
# TODO: Clean events so that they do not leak personal information
...


def install_sentry():
sentry_sdk.init(
dsn=SENTRY_DSN.__wrapped__,
server_name=socket.gethostname(),
debug=__debug__,
release=f"{version}+dev" if __debug__ else version,
environment="Development" if __debug__ else "Production",
integrations=(
sentry_sdk.integrations.flask.FlaskIntegration(),
sentry_sdk.integrations.sqlalchemy.SqlalchemyIntegration(),
),
before_send=clean_events,
)


__all__ = (
"sentry"
)
2 changes: 2 additions & 0 deletions lokiunimore/web/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import flask.logging
import lokiunimore.utils.logs

from lokiunimore.utils.errors import install_sentry
from .app import app

lokiunimore.utils.logs.install_log_handler(app.logger)
app.logger.removeHandler(flask.logging.default_handler)
install_sentry()

app.run(host="127.0.0.1", port=30008, debug=True)
52 changes: 51 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ psycopg2 = "^2.9.3"
matrix-nio = "^0.19.0"
cfig = {extras = ["cli"], version = "^0.3.0"}
python-dotenv = "^0.21.0"
sentry-sdk = {extras = ["flask"], version = "^1.9.8"}



Expand Down