Skip to content

Commit 6129bd9

Browse files
mars-fpurelogiq
authored andcommittedJul 17, 2017
Use Sentry to report exceptions
1 parent 13c8477 commit 6129bd9

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
 

‎docker-compose.yml

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ services:
1818
- TRANSPLANT_URL=https://stub.transplant.example.com
1919
- DATABASE_URL=sqlite:////db/sqlite.db
2020
- HOST_URL=https://lando-api.test
21+
- ENV=localdev
22+
- SENTRY_DSN=
2123
volumes:
2224
- ./:/app
2325
- ./.db/:/db/

‎landoapi/app.py

+45
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,16 @@ 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['version']
40+
initialize_sentry(flask_app, this_app_version)
41+
3042
db_uri = flask_app.config.setdefault(
3143
'SQLALCHEMY_DATABASE_URI', os.environ.get('DATABASE_URL', 'sqlite://')
3244
)
@@ -46,6 +58,39 @@ def create_app(version_path):
4658
return app
4759

4860

61+
def initialize_sentry(flask_app, release):
62+
"""Initialize Sentry application monitoring.
63+
64+
See https://docs.sentry.io/clients/python/advanced/#client-arguments for
65+
details about what this function's arguments mean to Sentry.
66+
67+
Args:
68+
flask_app: A Flask() instance.
69+
release: A string representing this application release number (such as
70+
a git sha). Will be used as the Sentry "release" identifier. See
71+
the Sentry client configuration docs for details.
72+
"""
73+
sentry_dsn = os.environ.get('SENTRY_DSN', None)
74+
if sentry_dsn:
75+
log_config_change('SENTRY_DSN', sentry_dsn)
76+
else:
77+
log_config_change('SENTRY_DSN', 'none (sentry disabled)')
78+
79+
# Do this after logging the DSN so if there is a DSN URL parsing error
80+
# the logs will record the configured value before the Sentry client
81+
# kills the app.
82+
sentry = Sentry(flask_app, dsn=sentry_dsn)
83+
84+
# Set these attributes directly because their keyword arguments can't be
85+
# passed into Sentry.__init__() or make_client().
86+
sentry.client.release = release
87+
log_config_change('SENTRY_LOG_RELEASE_AS', release)
88+
89+
environment = os.environ.get('ENV', None)
90+
sentry.client.environment = environment
91+
log_config_change('SENTRY_LOG_ENVIRONMENT_AS', environment)
92+
93+
4994
def initialize_logging():
5095
"""Initialize application-wide logging."""
5196
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.