Skip to content

Commit

Permalink
Add explicit Sentry logging integration (#3262)
Browse files Browse the repository at this point in the history
* Add explicit Sentry logging integration

* Arrange code comments according @grahamalama's feedback
  • Loading branch information
leplatrem authored Sep 6, 2023
1 parent aa0fc87 commit dfd5e05
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ This document describes changes between each past release.
16.0.1 (unreleased)
-------------------

- Nothing changed yet.
**New features**

- Send logging warnings to Sentry, with logging debugs as breadcrumbs. Configure levels with ``kinto.sentry_breadcrumbs_min_level`` and ``kinto.sentry_events_min_level`` settings.


16.0.0 (2023-05-30)
Expand Down
4 changes: 4 additions & 0 deletions docs/configuration/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ Sentry reporting can be enabled via the following settings:
kinto.sentry_dsn = https://[email protected]/1
kinto.sentry_env = stage
# Integrate logging with Sentry.
# kinto.sentry_breadcrumbs_min_level = 10 # DEBUG
# kinto.sentry_events_min_level = 30 # WARNING
Or the equivalent environment variables:

::
Expand Down
2 changes: 2 additions & 0 deletions kinto/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
"settings_prefix": "",
"sentry_dsn": None,
"sentry_env": None,
"sentry_breadcrumbs_min_level": logging.DEBUG,
"sentry_events_min_level": logging.WARNING,
"statsd_backend": "kinto.core.statsd",
"statsd_prefix": "kinto.core",
"statsd_url": None,
Expand Down
9 changes: 9 additions & 0 deletions kinto/core/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
ProfilerMiddleware = False
try:
import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.pyramid import PyramidIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
except ImportError: # pragma: no cover
Expand Down Expand Up @@ -284,11 +285,19 @@ def setup_sentry(config):
if env:
env_options["environment"] = env

breadcrumbs_level = settings["sentry_breadcrumbs_min_level"]
events_level = settings["sentry_events_min_level"]
sentry_sdk.init(
dsn,
integrations=[
PyramidIntegration(),
SqlalchemyIntegration(),
LoggingIntegration(
# Logs to be captured as breadcrumbs (debug and above by default)
level=breadcrumbs_level,
# Logs to be catpured as events (warning and above by default)
event_level=events_level,
),
],
**env_options,
)
Expand Down

0 comments on commit dfd5e05

Please sign in to comment.