From d0dc9cd36426c22f512ea9502510062fc2f8bed7 Mon Sep 17 00:00:00 2001 From: Alex Sadleir Date: Mon, 8 Apr 2019 13:20:40 +1000 Subject: [PATCH 1/7] Upgrade to new sentry SDK for python --- README.rst | 4 ++-- ckanext/sentry/plugins.py | 9 ++++++--- requirements.txt | 2 +- setup.py | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index f9ecb05..671f587 100644 --- a/README.rst +++ b/README.rst @@ -34,7 +34,7 @@ You must provide a Sentry DSN:: sentry.dsn = https://xxxxxx:xxxxxx@sentry.domain.com/1 -You can see a full list of supported options for the Sentry client on the `official Raven documentation`_. +You can see a full list of supported options for the Sentry client on the `official Sentry documentation`_. If you want Sentry to record your log messages, you can turn it on adding the following options:: @@ -55,5 +55,5 @@ The configuration also supports env vars named like the `ckanext-envvars`_ exten .. _Sentry: http://getsentry.com/ -.. _official Raven documentation: http://raven.readthedocs.org/en/latest/advanced.html#configuring-the-client +.. _official Sentry documentation: https://docs.sentry.io/error-reporting/configuration/?platform=python .. _ckanext-envvars: https://github.com/okfn/ckanext-envvars diff --git a/ckanext/sentry/plugins.py b/ckanext/sentry/plugins.py index ac7b978..523c815 100644 --- a/ckanext/sentry/plugins.py +++ b/ckanext/sentry/plugins.py @@ -4,8 +4,9 @@ import os import logging -from raven.contrib.pylons import Sentry -from raven.handlers.logging import SentryHandler +import sentry_sdk +from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware +from sentry_sdk.integrations.logging import SentryHandler from ckan import plugins @@ -44,7 +45,9 @@ def make_error_log_middleware(self, app, config): self._configure_logging(config) log.debug('Adding Sentry middleware...') - return Sentry(app, config) + sentry_sdk.init(dsn=config.get('sentry.dsn')) + sentry = SentryWsgiMiddleware(app) + return app def _configure_logging(self, config): ''' diff --git a/requirements.txt b/requirements.txt index eb0d457..e2b135f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -raven==6.1.0 +sentry-sdk==0.7.10 diff --git a/setup.py b/setup.py index dde9b93..e5cfadb 100644 --- a/setup.py +++ b/setup.py @@ -50,7 +50,7 @@ def rst(filename): namespace_packages=['ckanext'], include_package_data=True, zip_safe=False, - install_requires=['raven'], + install_requires=['sentry_sdk'], entry_points={ 'ckan.plugins': [ 'sentry = ckanext.sentry.plugins:SentryPlugin', From 82b668069fdcdf08f0d1cf7bb0e03af68b0e2581 Mon Sep 17 00:00:00 2001 From: Rae Knowler Date: Thu, 12 Aug 2021 13:11:21 +0200 Subject: [PATCH 2/7] Remove sentry.dsn from SentryHandler instantiation The DSN is no longer used here and causes an error if we include it. --- ckanext/sentry/plugins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/sentry/plugins.py b/ckanext/sentry/plugins.py index 523c815..9a3112c 100644 --- a/ckanext/sentry/plugins.py +++ b/ckanext/sentry/plugins.py @@ -56,7 +56,7 @@ def _configure_logging(self, config): Based on @rshk work on https://github.com/opendatatrentino/ckanext-sentry ''' - handler = SentryHandler(config.get('sentry.dsn')) + handler = SentryHandler() handler.setLevel(logging.NOTSET) loggers = ['', 'ckan', 'ckanext', 'sentry.errors'] From 4a7bebbfb25a5e2a5d35e9d63d4df91d36c58588 Mon Sep 17 00:00:00 2001 From: Rae Knowler Date: Thu, 12 Aug 2021 13:11:40 +0200 Subject: [PATCH 3/7] Bump sentry-sdk version to 1.3.1 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index e2b135f..06f1d4a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -sentry-sdk==0.7.10 +sentry-sdk==1.3.1 From 3115664fa743345afc57478062222a5ade92df90 Mon Sep 17 00:00:00 2001 From: Rae Knowler Date: Thu, 12 Aug 2021 15:00:12 +0200 Subject: [PATCH 4/7] Use FlaskIntegration and RqIntegration instead of SentryWsgiMiddleware --- ckanext/sentry/plugins.py | 9 ++++++--- requirements.txt | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ckanext/sentry/plugins.py b/ckanext/sentry/plugins.py index 9a3112c..438e2af 100644 --- a/ckanext/sentry/plugins.py +++ b/ckanext/sentry/plugins.py @@ -5,8 +5,9 @@ import logging import sentry_sdk -from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware from sentry_sdk.integrations.logging import SentryHandler +from sentry_sdk.integrations.flask import FlaskIntegration +from sentry_sdk.integrations.rq import RqIntegration from ckan import plugins @@ -45,8 +46,10 @@ def make_error_log_middleware(self, app, config): self._configure_logging(config) log.debug('Adding Sentry middleware...') - sentry_sdk.init(dsn=config.get('sentry.dsn')) - sentry = SentryWsgiMiddleware(app) + sentry_sdk.init( + dsn=config.get('sentry.dsn'), + integrations=[FlaskIntegration(), RqIntegration()] + ) return app def _configure_logging(self, config): diff --git a/requirements.txt b/requirements.txt index 06f1d4a..8c2e42a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ +blinker==1.4 sentry-sdk==1.3.1 From 5dabeafeb9a29fc1b4d513a20b9a533201c517ca Mon Sep 17 00:00:00 2001 From: Rae Knowler Date: Mon, 16 Aug 2021 13:26:11 +0200 Subject: [PATCH 5/7] Set log level for LoggingIntegration during setup The LoggingIntegration is enabled by default, and handles logs that are not handled by other Sentry integrations. Its default log level is INFO, so if we want to set our log level to a different value, we have to set it in this integration explicitly. --- ckanext/sentry/plugins.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ckanext/sentry/plugins.py b/ckanext/sentry/plugins.py index 438e2af..83b972e 100644 --- a/ckanext/sentry/plugins.py +++ b/ckanext/sentry/plugins.py @@ -5,7 +5,7 @@ import logging import sentry_sdk -from sentry_sdk.integrations.logging import SentryHandler +from sentry_sdk.integrations.logging import LoggingIntegration, SentryHandler from sentry_sdk.integrations.flask import FlaskIntegration from sentry_sdk.integrations.rq import RqIntegration @@ -46,9 +46,14 @@ def make_error_log_middleware(self, app, config): self._configure_logging(config) log.debug('Adding Sentry middleware...') + sentry_log_level = config.get('sentry.log_level', logging.INFO) sentry_sdk.init( dsn=config.get('sentry.dsn'), - integrations=[FlaskIntegration(), RqIntegration()] + integrations=[ + FlaskIntegration(), + LoggingIntegration(level=sentry_log_level), + RqIntegration() + ] ) return app From 960611cfb91e272a775106b08cfdc0dbc1e79832 Mon Sep 17 00:00:00 2001 From: Rae Knowler Date: Thu, 16 Sep 2021 14:20:33 +0200 Subject: [PATCH 6/7] Fix logging the same thing multiple times Ported from Datopian's fix at https://github.com/datopian/ckanext-sentry/commit/777c872adaf7175285f868f2e12063b92b26fb9a --- ckanext/sentry/plugins.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ckanext/sentry/plugins.py b/ckanext/sentry/plugins.py index 83b972e..fcc0fc5 100644 --- a/ckanext/sentry/plugins.py +++ b/ckanext/sentry/plugins.py @@ -69,10 +69,14 @@ def _configure_logging(self, config): loggers = ['', 'ckan', 'ckanext', 'sentry.errors'] sentry_log_level = config.get('sentry.log_level', logging.INFO) - for name in loggers: - logger = logging.getLogger(name) - logger.addHandler(handler) - logger.setLevel(sentry_log_level) + logger = logging.getLogger() + # ensure we haven't already registered the handler + if SentryHandler not in map(lambda x: x.__class__, logger.handlers): + logger.addHandler(SentryHandler()) + # Add StreamHandler to sentry's default so you can catch missed exceptions + logger = logging.getLogger('sentry.errors') + logger.propagate = False + logger.addHandler(logging.StreamHandler()) log.debug('Setting up Sentry logger with level {0}'.format( sentry_log_level)) From c1ba01f39c71138eaf2152312e08ee1d8548ff80 Mon Sep 17 00:00:00 2001 From: Rae Knowler Date: Thu, 16 Sep 2021 16:40:40 +0200 Subject: [PATCH 7/7] Use the configured sentry log level still --- ckanext/sentry/plugins.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ckanext/sentry/plugins.py b/ckanext/sentry/plugins.py index fcc0fc5..0e25cb4 100644 --- a/ckanext/sentry/plugins.py +++ b/ckanext/sentry/plugins.py @@ -67,12 +67,12 @@ def _configure_logging(self, config): handler = SentryHandler() handler.setLevel(logging.NOTSET) - loggers = ['', 'ckan', 'ckanext', 'sentry.errors'] sentry_log_level = config.get('sentry.log_level', logging.INFO) logger = logging.getLogger() # ensure we haven't already registered the handler if SentryHandler not in map(lambda x: x.__class__, logger.handlers): - logger.addHandler(SentryHandler()) + logger.addHandler(handler) + logger.setLevel(sentry_log_level) # Add StreamHandler to sentry's default so you can catch missed exceptions logger = logging.getLogger('sentry.errors') logger.propagate = False