model-service/model_service/app.py

93 lines
2.3 KiB
Python
Raw Normal View History

2020-04-13 21:14:06 +02:00
#!/usr/bin/env python3
2020-04-14 13:02:17 +02:00
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
2020-04-13 21:14:06 +02:00
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
2020-11-19 01:28:21 +01:00
from healthcheck import HealthCheck
2020-04-13 21:14:06 +02:00
# import stuff
from model import db
2020-11-19 01:28:21 +01:00
from utils import register_all_error_handlers, storage, health_database_status
2020-04-13 21:14:06 +02:00
# import views
2020-10-02 03:28:40 +02:00
from views import SVMView, CNNView, RootView
2020-04-13 21:14:06 +02:00
2021-08-04 16:42:28 +02:00
from config import Config
2021-08-04 16:57:27 +02:00
# Tracing stuffs
import jaeger_client
import opentracing
from flask_opentracing import FlaskTracing
2021-08-04 16:42:28 +02:00
if Config.SENTRY_DSN:
2020-04-14 13:02:17 +02:00
sentry_sdk.init(
2021-08-04 16:42:28 +02:00
dsn=Config.SENTRY_DSN,
2020-04-14 13:02:17 +02:00
integrations=[FlaskIntegration()],
2020-10-19 22:30:34 +02:00
_experiments={"auto_enabling_integrations": True},
2021-08-05 16:43:48 +02:00
traces_sample_rate=0,
2020-04-14 13:02:17 +02:00
send_default_pii=True,
2021-08-04 16:42:28 +02:00
release=Config.RELEASE_ID,
environment=Config.RELEASEMODE
2020-04-14 13:02:17 +02:00
)
2020-04-13 21:14:06 +02:00
# create flask app
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1)
2021-08-04 16:42:28 +02:00
# load config
app.config.from_object(Config)
2020-04-13 21:14:06 +02:00
# initialize stuff
db.init_app(app)
2020-04-14 17:03:15 +02:00
storage.init_app(app)
2020-12-06 11:15:11 +01:00
health = HealthCheck()
2020-04-13 21:14:06 +02:00
2020-10-01 20:20:55 +02:00
@app.before_first_request
def create_db():
2020-04-13 21:14:06 +02:00
db.create_all()
2020-10-01 20:20:55 +02:00
2020-04-13 21:14:06 +02:00
# register error handlers
register_all_error_handlers(app)
2021-08-04 16:57:27 +02:00
# Setup tracing
def initialize_tracer():
2021-08-05 15:08:20 +02:00
app.logger.info("Initializing jaeger...")
jaeger_cfg = jaeger_client.Config(config={}, service_name='model-service', validate=True)
2021-08-04 16:57:27 +02:00
tracer = jaeger_cfg.initialize_tracer()
return tracer
tracing = FlaskTracing(initialize_tracer, True, app)
2020-04-13 21:14:06 +02:00
# register views
2020-10-02 03:28:40 +02:00
for view in [SVMView, CNNView, RootView]:
2020-07-28 20:19:52 +02:00
view.register(app, trailing_slash=False, route_prefix='/model')
2020-04-13 21:14:06 +02:00
2020-11-19 01:28:21 +01:00
health.add_check(health_database_status)
2020-12-06 11:15:11 +01:00
app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run())
2020-11-19 01:28:21 +01:00
2020-04-13 21:14:06 +02:00
# start debuggig if needed
if __name__ == "__main__":
app.run(debug=True)
2021-08-04 16:42:28 +02:00
else:
2021-08-05 15:55:03 +02:00
import os
2021-08-04 16:42:28 +02:00
2021-08-05 15:55:03 +02:00
if "gunicorn" in os.environ.get("SERVER_SOFTWARE", ""):
import logging
gunicorn_logger = logging.getLogger('gunicorn.error')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
jaeger_logger = logging.getLogger('jaeger_tracing')
jaeger_logger.handlers = gunicorn_logger.handlers
jaeger_logger.setLevel(gunicorn_logger.level)
app.logger.info("Gunicorn environment detected!")
else:
app.logger.info("Not gunicorn")