Skip to content

Commit d422f7d

Browse files
committedJul 14, 2017
Use Sentry to report exceptions
1 parent 13c8477 commit d422f7d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
 

‎landoapi/app.py

+48
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
# This Source Code Form is subject to the terms of the Mozilla Public
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
import json
45
import os
56

67
import click
78
import connexion
89
import logging
910

1011
from connexion.resolver import RestyResolver
12+
from raven.contrib.flask import Sentry
13+
1114
from landoapi.dockerflow import dockerflow
1215
from landoapi.models.storage import alembic, db
1316
from mozlogging import MozLogFormatter
1417

1518
logger = logging.getLogger(__name__)
1619

20+
sentry = Sentry()
21+
1722

1823
def create_app(version_path):
1924
"""Construct an application instance."""
@@ -24,9 +29,17 @@ def create_app(version_path):
2429

2530
# Get the Flask app being wrapped by the Connexion app.
2631
flask_app = app.app
32+
2733
flask_app.config['VERSION_PATH'] = version_path
2834
log_config_change('VERSION_PATH', version_path)
2935

36+
version_info = json.load(open(version_path))
37+
logger.info(version_info, 'app.version')
38+
39+
this_app_version = version_info['commit']
40+
environment = version_info['build']
41+
initialize_sentry(flask_app, this_app_version, environment)
42+
3043
db_uri = flask_app.config.setdefault(
3144
'SQLALCHEMY_DATABASE_URI', os.environ.get('DATABASE_URL', 'sqlite://')
3245
)
@@ -46,6 +59,41 @@ def create_app(version_path):
4659
return app
4760

4861

62+
def initialize_sentry(flask_app, release, environment):
63+
"""Initialize Sentry application monitoring.
64+
65+
See https://docs.sentry.io/clients/python/advanced/#client-arguments for
66+
details about what this function's arguments mean to Sentry.
67+
68+
Args:
69+
flask_app: A Flask() instance.
70+
release: A string representing this application release number (such as
71+
a git sha). Will be used as the Sentry "release" identifier. See
72+
the Sentry client configuration docs for details.
73+
environment: A string representing the application environment, such
74+
as 'dev', 'stage', or 'prod'. Will be used as the Sentry
75+
"environment" identifier. See the Sentry client configuration docs
76+
for details.
77+
"""
78+
# This will automatically read the SENTRY_DSN environment variable.
79+
sentry_dsn = os.environ.get('SENTRY_DSN', None)
80+
if sentry_dsn:
81+
log_config_change('SENTRY_DSN', sentry_dsn)
82+
else:
83+
log_config_change('SENTRY_DSN', 'none (sentry disabled)')
84+
85+
# Do this last so if there is a DSN URL parsing error the logs will record
86+
# the configured value before the Sentry client kills the app.
87+
sentry.init_app(flask_app)
88+
89+
# We have to set these attributes directly because their keyword
90+
# arguments can't be passed into sentry.init_app().
91+
sentry.client.release = release
92+
log_config_change('SENTRY_LOG_RELEASE_AS', release)
93+
sentry.client.environment = environment
94+
log_config_change('SENTRY_LOG_ENVIRONMENT_AS', environment)
95+
96+
4997
def initialize_logging():
5098
"""Initialize application-wide logging."""
5199
mozlog_handler = logging.StreamHandler()

‎requirements/common.txt

+8
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@ Flask-Alembic==2.0.1 \
7373
mozlogging==0.1.0 \
7474
--hash=sha256:2fc3d520a17b048c8723abdba19f6c01f2bcaf4182944aaac5906f28bd5b7d77 \
7575
--hash=sha256:2e1362b80418b845164d8d47337da838dade05720dbaf17956d1cafebc511b94
76+
raven[flask]==6.1.0 \
77+
--hash=sha256:56dc9062dd42bca97350e5048ff417c914376366caa3b1b5f788b27ddc0a34b7 \
78+
--hash=sha256:02cabffb173b99d860a95d4908e8b1864aad1b8452146e13fd7e212aa576a884
79+
contextlib2==0.5.5 \
80+
--hash=sha256:f5260a6e679d2ff42ec91ec5252f4eeffdcf21053db9113bd0a8e4d953769c00 \
81+
--hash=sha256:509f9419ee91cdd00ba34443217d5ca51f5a364a404e1dce9e8979cea969ca48
82+
blinker==1.4 \
83+
--hash=sha256:471aee25f3992bd325afa3772f1063dbdbbca947a041b8b89466dc00d606f8b6

0 commit comments

Comments
 (0)
Failed to load comments.