Skip to content

Commit

Permalink
Merge remote-tracking branch 'amCap1712/sentry'
Browse files Browse the repository at this point in the history
  • Loading branch information
alastair committed Mar 31, 2021
2 parents 21bf932 + 3ea6d1a commit 27a1623
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 75 deletions.
17 changes: 0 additions & 17 deletions brainzutils/flask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def init_debug_toolbar(self):

def init_loggers(self,
file_config=None,
email_config=None,
sentry_config=None):
"""This method attaches loggers to the Flask app.
Expand All @@ -63,20 +62,6 @@ def init_loggers(self,
'backup_count': 100, # optional
}
email_config (dict): Dictionary with the following structure::
{
'mail_server': 'localhost',
'mail_port': 25,
'mail_from_host': 'example.org',
'log_email_recipients': [
'[email protected]',
'[email protected]',
],
'log_email_topic': 'Error occurred',
'level': 'ERROR', # optional
}
sentry_config (dict): Dictionary with the following structure::
{
Expand All @@ -86,7 +71,5 @@ def init_loggers(self,
"""
if file_config:
loggers.add_file_handler(self, **file_config)
if email_config:
loggers.add_email_handler(self, **email_config)
if sentry_config:
loggers.add_sentry(self, **sentry_config)
64 changes: 7 additions & 57 deletions brainzutils/flask/loggers.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
# pylint: disable=invalid-name
import logging
from logging.handlers import RotatingFileHandler, SMTPHandler
import raven
import raven.base
import raven.contrib.flask
import raven.transport.threaded_requests


class MissingRavenClient(raven.Client):
"""Raven client class that is used as a placeholder.
This is done to make sure that calls to functions in the client don't fail
even if the client is not initialized. Sentry server might be missing, but
we don't want to check if it actually exists in every place exception is
captured.
"""
captureException = lambda self, *args, **kwargs: None
captureMessage = lambda self, *args, **kwargs: None


_sentry_client = MissingRavenClient() # type: raven.Client
import sentry_sdk
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.flask import FlaskIntegration


def add_file_handler(app, filename, max_bytes=512 * 1024, backup_count=100):
Expand All @@ -32,50 +20,12 @@ def add_file_handler(app, filename, max_bytes=512 * 1024, backup_count=100):
app.logger.addHandler(file_handler)


def add_email_handler(app, mail_server, mail_port, mail_from_host,
log_email_recipients, log_email_topic,
level=logging.ERROR):
"""Adds email notifications about captured logs."""
mail_handler = SMTPHandler(
(mail_server, mail_port),
"logs@" + mail_from_host,
log_email_recipients,
log_email_topic
)
mail_handler.setLevel(level)
mail_handler.setFormatter(logging.Formatter('''
Message type: %(levelname)s
Location: %(pathname)s:%(lineno)d
Module: %(module)s
Function: %(funcName)s
Time: %(asctime)s
Message:
%(message)s
'''))
app.logger.addHandler(mail_handler)


def add_sentry(app, dsn, level=logging.WARNING, **options):
"""Adds Sentry logging.
Sentry is a realtime event logging and aggregation platform. Additional
information about it is available at https://sentry.readthedocs.org/.
We use Raven as a client for Sentry. More info about Raven is available at
https://raven.readthedocs.org/.
"""
app.config["SENTRY_TRANSPORT"] = raven.transport.threaded_requests.ThreadedRequestsHTTPTransport
app.config["SENTRY_CONFIG"] = options
global _sentry_client
_sentry_client = raven.contrib.flask.Sentry(
app=app,
dsn=dsn,
level=level,
logging=True,
)


def get_sentry_client():
return _sentry_client
sentry_sdk.init(dsn, integrations=[LoggingIntegration(level=level), FlaskIntegration(), RedisIntegration(),
SqlalchemyIntegration()])
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Jinja2>=2.11.2
werkzeug>=1.0.1
Flask-DebugToolbar>=0.11.0
Flask-UUID>=0.2
raven[flask]>=6.10.0
sentry-sdk[flask]>=0.20.2
certifi
redis>=3.5,<4.0
msgpack-python==0.5.6
Expand Down

0 comments on commit 27a1623

Please sign in to comment.