storage-service/storage_service/app.py

84 lines
2.1 KiB
Python
Raw Normal View History

2020-03-25 00:19:12 +01:00
#!/usr/bin/env python3
2020-03-31 19:47:12 +02:00
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
2020-03-25 00:19:12 +01:00
from flask import Flask
from werkzeug.middleware.proxy_fix import ProxyFix
2020-11-19 01:49:40 +01:00
from healthcheck import HealthCheck
2020-03-25 00:19:12 +01:00
# import stuff
2020-11-19 01:49:40 +01:00
from utils import register_all_error_handlers, storage, health_database_status
2020-03-25 00:19:12 +01:00
# import views
from views import ObjectView
2021-08-09 13:55:42 +02:00
from config import Config
2021-08-09 15:56:32 +02:00
# Tracing stuffs
import jaeger_client
import opentracing
from flask_opentracing import FlaskTracing
2020-03-31 19:47:12 +02:00
# Setup sentry
2021-08-09 13:55:42 +02:00
if Config.SENTRY_DSN:
2020-03-31 19:47:12 +02:00
sentry_sdk.init(
2021-08-09 13:55:42 +02:00
dsn=Config.SENTRY_DSN,
2020-03-31 19:47:12 +02:00
integrations=[FlaskIntegration()],
2021-08-09 13:55:42 +02:00
_experiments={"auto_enabling_integrations": True},
traces_sample_rate=0,
2020-03-31 19:47:12 +02:00
send_default_pii=True,
2021-08-09 13:55:42 +02:00
release=Config.RELEASE_ID,
environment=Config.RELEASEMODE
2020-03-31 19:47:12 +02:00
)
2020-03-25 00:19:12 +01:00
# create flask app
app = Flask(__name__)
2021-08-09 13:55:42 +02:00
app.config.from_object(Config)
2020-03-25 00:19:12 +01:00
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1)
2020-03-25 01:57:54 +01:00
# init stuff
storage.init_app(app)
2020-12-06 11:14:40 +01:00
health = HealthCheck()
2020-11-19 01:49:40 +01:00
2020-03-25 00:19:12 +01:00
# register error handlers
register_all_error_handlers(app)
2021-08-09 15:56:32 +02:00
# Setup tracing
def initialize_tracer():
app.logger.info("Initializing jaeger...")
jaeger_cfg = jaeger_client.Config(config={}, service_name='storage-service', validate=True)
tracer = jaeger_cfg.initialize_tracer()
return tracer
tracing = FlaskTracing(initialize_tracer, True, app)
2020-03-25 00:19:12 +01:00
# register views
for view in [ObjectView]:
2020-03-25 01:57:54 +01:00
view.register(app, trailing_slash=False)
2020-03-25 00:19:12 +01:00
2020-11-19 01:49:40 +01:00
health.add_check(health_database_status)
2020-12-06 11:14:40 +01:00
app.add_url_rule("/healthz", "healthcheck", view_func=lambda: health.run())
2020-11-19 01:49:40 +01:00
2020-03-25 01:57:54 +01:00
# start debugging if needed
2020-03-25 00:19:12 +01:00
if __name__ == "__main__":
2020-03-25 01:57:54 +01:00
app.run(debug=True)
2021-08-09 15:48:19 +02:00
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")