1
1
# This Source Code Form is subject to the terms of the Mozilla Public
2
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
+ import json
4
5
import os
5
6
6
7
import click
7
8
import connexion
8
9
import logging
9
10
10
11
from connexion .resolver import RestyResolver
12
+ from raven .contrib .flask import Sentry
13
+
11
14
from landoapi .dockerflow import dockerflow
12
15
from landoapi .models .storage import alembic , db
13
16
from mozlogging import MozLogFormatter
14
17
15
18
logger = logging .getLogger (__name__ )
16
19
20
+ sentry = Sentry ()
21
+
17
22
18
23
def create_app (version_path ):
19
24
"""Construct an application instance."""
@@ -24,9 +29,16 @@ def create_app(version_path):
24
29
25
30
# Get the Flask app being wrapped by the Connexion app.
26
31
flask_app = app .app
32
+
27
33
flask_app .config ['VERSION_PATH' ] = version_path
28
34
log_config_change ('VERSION_PATH' , version_path )
29
35
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
+
30
42
db_uri = flask_app .config .setdefault (
31
43
'SQLALCHEMY_DATABASE_URI' , os .environ .get ('DATABASE_URL' , 'sqlite://' )
32
44
)
@@ -46,6 +58,39 @@ def create_app(version_path):
46
58
return app
47
59
48
60
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
+
49
94
def initialize_logging ():
50
95
"""Initialize application-wide logging."""
51
96
mozlog_handler = logging .StreamHandler ()
0 commit comments