Skip to content

Instantly share code, notes, and snippets.

@PurpleBooth
Created April 6, 2019 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PurpleBooth/a1e80e8c86d34b4687b7e60c08a9e9fd to your computer and use it in GitHub Desktop.
Save PurpleBooth/a1e80e8c86d34b4687b7e60c08a9e9fd to your computer and use it in GitHub Desktop.
from functools import wraps
import jaeger_client
from flask import current_app
from flask_opentracing import FlaskTracer
from opentracing_instrumentation.client_hooks import requests, strict_redis, sqlalchemy
from opentracing_instrumentation.request_context import RequestContextManager
def install_tracer(service_name, flask_app=None):
config = jaeger_client.Config(
config={
'sampler': {
'type': 'const',
'param': 1,
},
'logging': True,
},
service_name=service_name,
validate=True)
tracer = config.initialize_tracer()
_install_patches()
if flask_app is not None:
_install_flask_tracer(flask_app, tracer)
def _install_patches():
requests.install_patches()
strict_redis.install_patches()
sqlalchemy.install_patches()
def _install_flask_tracer(flask_app, tracer):
flask_app.tracer = FlaskTracer(tracer, True, flask_app)
def controller_action(action):
@wraps(action)
def traced_action(*args, **kwds):
if hasattr(current_app, 'tracer'):
with RequestContextManager(span=current_app.tracer.get_span()):
return action(*args, **kwds)
else:
return action(*args, **kwds)
return traced_action
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment