Skip to content

Commit

Permalink
add sentry support
Browse files Browse the repository at this point in the history
  • Loading branch information
tschorr committed Aug 22, 2019
1 parent 7f5aca3 commit 8b48f87
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.rst
Expand Up @@ -311,6 +311,11 @@ access-log-custom
Like `event-log-custom`, a custom section for the access logger, to be able
to use another event logger than `logfile`. Used for ZServer only, not WSGI.

sentry_dsn
Provide a Sentry DSN here to enable basic Sentry logging documented
in `<https://docs.sentry.io/platforms/python/logging/>`_.
Available for WSGI only.

Load non-setuptools compatible Python libraries
-----------------------------------------------

Expand Down
6 changes: 6 additions & 0 deletions setup.py
Expand Up @@ -50,6 +50,9 @@
'test': [
'zope.testrunner',
],
'sentry': [
'sentry-sdk',
]
},
zip_safe=False,
entry_points={
Expand All @@ -60,5 +63,8 @@
'paste.server_factory': [
'main=plone.recipe.zope2instance.ctl:server_factory',
],
'paste.filter_factory': [
'sentry=plone.recipe.zope2instance.sentry:sdk_init',
],
},
)
20 changes: 17 additions & 3 deletions src/plone/recipe/zope2instance/recipe.py
Expand Up @@ -743,12 +743,21 @@ def build_wsgi_ini(self):
'access-log-level',
options.get('z2-log-level', 'INFO'))

pipeline = []
if accesslog_name.lower() == 'disable':
pipeline = '\n '.join(['egg:Zope#httpexceptions', 'zope'])
pipeline = [
'egg:Zope#httpexceptions']
event_handlers = ''
else:
pipeline = '\n '.join(
['translogger', 'egg:Zope#httpexceptions', 'zope'])
pipeline = [
'translogger', 'egg:Zope#httpexceptions']

sentry_dsn = options.get('sentry_dsn', '')
if sentry_dsn:
pipeline.append('sentry')

pipeline.append('zope')
pipeline = '\n '.join(pipeline)
options = {
'location': options['location'],
'http_address': listen,
Expand All @@ -761,6 +770,7 @@ def build_wsgi_ini(self):
'pipeline': pipeline,
'eventlog_level': eventlog_level,
'accesslog_level': accesslog_level,
'sentry_dsn': sentry_dsn,
}
wsgi_ini = wsgi_ini_template % options
with open(wsgi_ini_path, 'w') as f:
Expand Down Expand Up @@ -1302,6 +1312,10 @@ def render_file_storage(self, file_storage, blob_storage,
use = egg:Paste#translogger
setup_console_handler = False
[filter:sentry]
use = egg:plone.recipe.zope2instance#sentry
dsn = %(sentry_dsn)s
[pipeline:main]
pipeline =
%(pipeline)s
Expand Down
8 changes: 8 additions & 0 deletions src/plone/recipe/zope2instance/sentry.py
@@ -0,0 +1,8 @@
import sentry_sdk


def sdk_init(global_conf, dsn):
def filter(app):
sentry_sdk.init(dsn=dsn)
return app
return filter
55 changes: 54 additions & 1 deletion src/plone/recipe/zope2instance/tests/wsgi.txt
Expand Up @@ -90,7 +90,7 @@ The buildout has also created an INI file containing the waitress configuration:
[filter:translogger]
use = egg:Paste#translogger
setup_console_handler = False
<BLANKLINE>
...
[pipeline:main]
pipeline =
translogger
Expand Down Expand Up @@ -372,3 +372,56 @@ The buildout has updated our INI file:
handlers =
qualname = waitress
...

Sentry support
==============

We want to sent logging events to Sentry.
Let's create a buildout:

>>> write('buildout.cfg',
... '''
... [buildout]
... parts = instance
... find-links = %(sample_buildout)s/eggs
...
... [instance]
... recipe = plone.recipe.zope2instance
... eggs =
... plone.recipe.zope2instance[sentry]
... user = me:me
... sentry_dsn = https://f00ba4ba2@my.sentry.server/9999
... ''' % options)

Let's run it::

>>> print(system(join('bin', 'buildout'))),
Uninstalling instance.
Installing instance.
Getting distribution for 'sentry-sdk'.
...
Generated script '.../sample-buildout/bin/instance'.
...

The buildout has updated our INI file:

>>> instance = os.path.join(sample_buildout, 'parts', 'instance')
>>> wsgi_ini = open(os.path.join(instance, 'etc', 'wsgi.ini')).read()
>>> print(wsgi_ini)
[server:main]
paste.server_factory = plone.recipe.zope2instance:main
use = egg:plone.recipe.zope2instance#main
...
[filter:sentry]
use = egg:plone.recipe.zope2instance#sentry
dsn = https://f00ba4ba2@my.sentry.server/9999
<BLANKLINE>
[pipeline:main]
pipeline =
translogger
egg:Zope#httpexceptions
sentry
zope
<BLANKLINE>
[loggers]
...

0 comments on commit 8b48f87

Please sign in to comment.