mirror of
https://github.com/sprockets/sprockets.http.git
synced 2024-11-14 11:19:26 +00:00
Add optional sentry integration
This commit is contained in:
parent
62dd020d42
commit
b9e2c3a7c5
4 changed files with 51 additions and 2 deletions
|
@ -1,4 +1,4 @@
|
|||
-e .
|
||||
-e '.[sentry]'
|
||||
-r testing.txt
|
||||
-r docs.txt
|
||||
flake8==3.7.8
|
||||
|
|
3
setup.py
3
setup.py
|
@ -34,6 +34,9 @@ setuptools.setup(
|
|||
entry_points={
|
||||
'distutils.commands': ['httprun=sprockets.http.runner:RunCommand'],
|
||||
},
|
||||
extras_require={
|
||||
'sentry': ['sentry-sdk>=1.5.4,<2'],
|
||||
},
|
||||
classifiers=[
|
||||
'Development Status :: 5 - Production/Stable',
|
||||
'Environment :: No Input/Output (Daemon)',
|
||||
|
|
|
@ -3,6 +3,19 @@ import logging.config
|
|||
import os
|
||||
import warnings
|
||||
|
||||
try:
|
||||
import sentry_sdk
|
||||
import sentry_sdk.integrations.logging
|
||||
import sentry_sdk.integrations.tornado
|
||||
|
||||
_sentry_integrations = [
|
||||
sentry_sdk.integrations.logging.LoggingIntegration(
|
||||
event_level=logging.CRITICAL),
|
||||
sentry_sdk.integrations.tornado.TornadoIntegration(),
|
||||
]
|
||||
except ModuleNotFoundError:
|
||||
sentry_sdk = None
|
||||
|
||||
|
||||
version_info = (2, 2, 0)
|
||||
__version__ = '.'.join(str(v) for v in version_info)
|
||||
|
@ -77,8 +90,16 @@ def run(create_application, settings=None, log_config=_unspecified):
|
|||
|
||||
port_number = int(app_settings.pop('port', os.environ.get('PORT', 8000)))
|
||||
num_procs = int(app_settings.pop('number_of_procs', '0'))
|
||||
server = runner.Runner(create_application(**app_settings))
|
||||
app = create_application(**app_settings)
|
||||
|
||||
if sentry_sdk is not None:
|
||||
sentry_sdk.init(
|
||||
integrations=_sentry_integrations,
|
||||
release=app.settings.get('version'),
|
||||
environment=app.settings.get('environment'),
|
||||
)
|
||||
|
||||
server = runner.Runner(app)
|
||||
server.run(port_number, num_procs)
|
||||
|
||||
|
||||
|
|
25
tests.py
25
tests.py
|
@ -304,6 +304,31 @@ class RunTests(MockHelper, unittest.TestCase):
|
|||
self.assertEqual(len(captured), 1)
|
||||
self.assertTrue(issubclass(captured[0].category, DeprecationWarning))
|
||||
|
||||
@mock.patch('sentry_sdk.init')
|
||||
def test_that_sentry_is_initialized(self, mock_sentry_init):
|
||||
app = mock.Mock()
|
||||
app.settings = {
|
||||
'environment': 'whatever',
|
||||
'version': 'a.b.c',
|
||||
}
|
||||
sprockets.http.run(lambda *_, **__: app)
|
||||
mock_sentry_init.assert_called_once_with(
|
||||
integrations=sprockets.http._sentry_integrations,
|
||||
release='a.b.c',
|
||||
environment='whatever',
|
||||
)
|
||||
|
||||
@mock.patch('sentry_sdk.init')
|
||||
def test_that_sentry_is_initialized_with_defaults(self, mock_sentry_init):
|
||||
app = mock.Mock()
|
||||
app.settings = {}
|
||||
sprockets.http.run(lambda *_, **__: app)
|
||||
mock_sentry_init.assert_called_once_with(
|
||||
integrations=sprockets.http._sentry_integrations,
|
||||
release=None,
|
||||
environment=None,
|
||||
)
|
||||
|
||||
|
||||
class CallbackTests(MockHelper, unittest.TestCase):
|
||||
|
||||
|
|
Loading…
Reference in a new issue