model-service/model_service/app.py
marcsello 17927b472d
All checks were successful
continuous-integration/drone/push Build is passing
Disabled sentry tracing
2021-08-05 16:43:48 +02:00

93 lines
2.3 KiB
Python

#!/usr/bin/env python3
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
from healthcheck import HealthCheck
# import stuff
from model import db
from utils import register_all_error_handlers, storage, health_database_status
# import views
from views import SVMView, CNNView, RootView
from config import Config
# Tracing stuffs
import jaeger_client
import opentracing
from flask_opentracing import FlaskTracing
if Config.SENTRY_DSN:
sentry_sdk.init(
dsn=Config.SENTRY_DSN,
integrations=[FlaskIntegration()],
_experiments={"auto_enabling_integrations": True},
traces_sample_rate=0,
send_default_pii=True,
release=Config.RELEASE_ID,
environment=Config.RELEASEMODE
)
# create flask app
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1)
# load config
app.config.from_object(Config)
# initialize stuff
db.init_app(app)
storage.init_app(app)
health = HealthCheck()
@app.before_first_request
def create_db():
db.create_all()
# register error handlers
register_all_error_handlers(app)
# Setup tracing
def initialize_tracer():
app.logger.info("Initializing jaeger...")
jaeger_cfg = jaeger_client.Config(config={}, service_name='model-service', validate=True)
tracer = jaeger_cfg.initialize_tracer()
return tracer
tracing = FlaskTracing(initialize_tracer, True, app)
# register views
for view in [SVMView, CNNView, RootView]:
view.register(app, trailing_slash=False, route_prefix='/model')
health.add_check(health_database_status)
app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run())
# start debuggig if needed
if __name__ == "__main__":
app.run(debug=True)
else:
import os
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")