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,17 @@ 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 ['commit' ]
40
+ environment = version_info ['build' ]
41
+ initialize_sentry (flask_app , this_app_version , environment )
42
+
30
43
db_uri = flask_app .config .setdefault (
31
44
'SQLALCHEMY_DATABASE_URI' , os .environ .get ('DATABASE_URL' , 'sqlite://' )
32
45
)
@@ -46,6 +59,41 @@ def create_app(version_path):
46
59
return app
47
60
48
61
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
+
49
97
def initialize_logging ():
50
98
"""Initialize application-wide logging."""
51
99
mozlog_handler = logging .StreamHandler ()
0 commit comments