python-flask icon indicating copy to clipboard operation
python-flask copied to clipboard

Question: How use opentracing.global_tracer() when using jaeger_client lib

Open jan25 opened this issue 5 years ago • 3 comments

This example https://github.com/opentracing-contrib/python-flask/blob/master/example/server.py#L28 uses the tracer returned by jaeger_client's initialize_tracer method. This method internally sets the opentracing.tracer to jaeger tracer object, this means for any request handler there after can do opentracing.global_tracer().start_span(...) and this sends traces to jaeger

I noticed when i run a flask dev server(using app.run) opentracing.global_tracer() is not the same as jaeger tracer i initialised earlier(as in the the link above) when starting up the server. Is there something i'm missing on flask side?

I could use returned jaeger_tracer from initialize_tracer(), which would mean i'd have to pass this around or create my own process/request local storage. I suppose according to jaeger tracer docs https://github.com/jaegertracing/jaeger-client-python#production, the initialised jaeger tracer must be available via opentracing.global_tracer()

jan25 avatar Jun 08 '19 19:06 jan25

I was able to solve this by setting up a uwsgi.ini to start up the server, and use @postfork from uwsgidecorators to ensure the tracer is initialized once per forked process. I suppose, this is more of a production way to set things up

The dev server issue still remains though, Afaik the app.run way to start up flask dev server creates one process so i expect the tracer to work there without @postfork. I even tried app.run(threaded=False, ...) with no luck

jan25 avatar Jun 09 '19 23:06 jan25

I was able to solve this by setting up a uwsgi.ini to start up the server, and use @postfork from uwsgidecorators to ensure the tracer is initialized once per forked process. I suppose, this is more of a production way to set things up

@jan25 , Could you please share an example of a @postfork function that worked? I have tried the following and it fails:

@postfork
def set_tracer_globals():
    global jaeger_tracer, flask_tracer
    jaeger_tracer = init_tracer('my_service')
    flask_tracer = FlaskTracing(jaeger_tracer, True, app)

Where init_tracer() returns config.initialize_tracer()

donnlee avatar Oct 14 '20 07:10 donnlee

@donnlee i have the example here https://github.com/jan25/hotrod-python/blob/master/services/frontend/server.py#L14

jan25 avatar Oct 14 '20 12:10 jan25