44
55from flasgger import Swagger
66from flask import Flask
7- from flask_opentracing import FlaskTracer
8- from jaeger_client import Config
7+ from flask_injector import FlaskInjector
8+ from injector import Injector
99
1010from project .config import CONFIG
11+ from pyms .healthcheck import healthcheck_blueprint
12+ from pyms .models import db
13+ from pyms .tracer .main import TracerModule
1114
1215__author__ = "Alberto Vara"
1316__email__ = "a.vara.1986@gmail.com"
4750}
4851
4952
50- def init_jaeger_tracer (service_name = 'your-app-name' ):
51- """This scaffold is configured whith `Jeager <https://github.com/jaegertracing/jaeger>`_ but you can use
52- one of the `opentracing tracers <http://opentracing.io/documentation/pages/supported-tracers.html>`_
53- :param service_name: the name of your application to register in the tracer
54- :return: opentracing.Tracer
55- """
56- config = Config (config = {
57- 'sampler' : {'type' : 'const' , 'param' : 1 }
58- }, service_name = service_name )
59- return config .initialize_tracer ()
60-
61-
6253class PrefixMiddleware (object ):
6354 """Set a prefix path to all routes. This action is needed if you have a stack of microservices and each of them
6455 exist in the same domain but different path. Por example:
6556 * mydomain.com/ms1/
6657 * mydomain.com/ms2/
6758 """
59+
6860 def __init__ (self , app , prefix = '' ):
6961 self .app = app
7062 self .prefix = prefix
@@ -86,20 +78,14 @@ def create_app():
8678 return the app and the database objects.
8779 :return:
8880 """
89- from project . models import db
81+
9082 from project .views import views_bp as views_blueprint
91- from project .views import views_hc as views_hc_blueprint
9283 environment = os .environ .get ("ENVIRONMENT" , "default" )
9384
9485 app = Flask (__name__ )
9586 app .config .from_object (CONFIG [environment ])
9687 app .wsgi_app = PrefixMiddleware (app .wsgi_app , prefix = app .config ["APPLICATION_ROOT" ])
9788
98-
99- if not app .config ["TESTING" ] and not app .config ["DEBUG" ]:
100- j_tracer = init_jaeger_tracer (app .config ["APP_NAME" ])
101- FlaskTracer (j_tracer , True , app )
102-
10389 db .init_app (app )
10490
10591 # Initialize Swagger
@@ -119,7 +105,13 @@ def create_app():
119105
120106 # Initialize Blueprints
121107 app .register_blueprint (views_blueprint )
122- app .register_blueprint (views_hc_blueprint )
108+ app .register_blueprint (healthcheck_blueprint )
109+
110+ # Inject Modules
111+ if not app .config ["TESTING" ] and not app .config ["DEBUG" ]:
112+ injector = Injector ([TracerModule (app )])
113+ FlaskInjector (app = app , injector = injector )
114+
123115 with app .test_request_context ():
124116 db .create_all ()
125117 return app , db
0 commit comments