def _start_trace(self): if not self.initialized: self.tracer = self.cfg.initialize_tracer() self.ftracer = FlaskTracer(self.tracer, False, self.app) self.initialized = True span = self.ftracer.get_span(request) if span is None: span = self.tracer.start_span(request.endpoint) mgr = RequestContextManager(span=span) mgr.__enter__() request.span = span request.mgr = mgr
def rest_prediction_server(): app = seldon_microservice.get_rest_microservice(user_object) if args.tracing: from flask_opentracing import FlaskTracer tracing = FlaskTracer(tracer, True, app) app.run(host='0.0.0.0', port=port)
def rest_prediction_server(): app = seldon_microservice.get_rest_microservice(user_object) try: user_object.load() except (NotImplementedError, AttributeError): pass if args.tracing: from flask_opentracing import FlaskTracer tracing = FlaskTracer(tracer, True, app) app.run(host='0.0.0.0', port=port)
def rest_prediction_server(): app = seldon_microservice.get_rest_microservice(user_object) try: user_object.load() except (NotImplementedError, AttributeError): pass if args.tracing: logger.info('Tracing branch is active') from flask_opentracing import FlaskTracer logger.info('Set JAEGER_EXTRA_TAGS %s', jaeger_extra_tags) tracing = FlaskTracer(tracer, True, app, jaeger_extra_tags) app.run(host='0.0.0.0', port=port)
def create_app(): app = Flask(__name__) setup_metrics(app) json_logging.ENABLE_JSON_LOGGING = True json_logging.init(framework_name='flask') json_logging.init_request_instrument(app) logger = logging.getLogger("app-logger") logger.setLevel(logging.INFO) logger.addHandler(logging.StreamHandler(sys.stdout)) cors = CORS(app, resources={r"/api/*": {"origins": "*"}}) api = Api(app) @app.route('/ping') def ping(): logger.info("pinged", extra={'tags': ['role:web', 'env:prod']}) return jsonify(ping='pong') @app.route("/healthz", methods=["GET"]) def healthz(): logger.info("health-checked", extra={'tags': ['role:web', 'env:prod']}) return jsonify({"status": "SUCCESS"}) @app.route('/metrics') def metrics(): return Response(prometheus_client.generate_latest(), mimetype=CONTENT_TYPE_LATEST) api.add_resource(TaskList, "/tasks") api.add_resource(Task, "/tasks/<int:id>") def initialize_tracer(): config = Config(config={ 'sampler': { 'type': 'const', 'param': 1 }, 'local_agent': { 'reporting_host': "cicdnode-0.vdigital.io", 'reporting_port': 6831 } }, service_name='task-service') return config.initialize_tracer() # also sets opentracing.tracer flask_tracer = FlaskTracer(initialize_tracer, True, app) return app
def create_app(self): """Initialize the Flask app, register blueprints and initialize all libraries like Swagger, database, the trace system... return the app and the database objects. :return: """ config = get_conf(service=self.service) app = connexion.App(__name__, specification_dir=os.path.join( self.path, 'swagger')) app.add_api('swagger.yaml', arguments={'title': config.APP_NAME}, base_path=config.APPLICATION_ROOT) self.application = app.app self.application._connexion_app = app self.application.config.from_object(config) self.application.tracer = None # Initialize Blueprints self.application.register_blueprint(healthcheck_blueprint) self.init_libs() self.add_error_handlers() # Inject Modules formatter = CustomJsonFormatter( '(timestamp) (level) (name) (module) (funcName) (lineno) (message)' ) if not self.application.config["TESTING"]: log_handler = logging.StreamHandler() self.application.tracer = FlaskTracer(init_jaeger_tracer(), True, self.application) formatter.add_service_name(self.application.config["APP_NAME"]) formatter.add_trace_span(self.application.tracer) log_handler.setFormatter(formatter) self.application.logger.addHandler(log_handler) self.application.logger.propagate = False self.application.logger.setLevel(logging.INFO) return self.application
from flask import Flask from flask_opentracing import FlaskTracer import lightstep.tracer import opentracing import urllib2 app = Flask(__name__) # one-time tracer initialization code ls_tracer = lightstep.tracer.init_tracer(group_name="example client", access_token="{your_lightstep_token}") # this tracer does not trace all requests, so the @tracer.trace() decorator must be used tracer = FlaskTracer(ls_tracer) @app.route("/") def index(): ''' Index page, has no tracing. ''' return "Index Page" @app.route("/request/<script>/<int:numrequests>") @tracer.trace("url") def send_multiple_requests(script, numrequests): ''' Traced function that makes a request to the server Injects the current span into headers to continue trace ''' span = tracer.get_span() def send_request(): url = "http://localhost:5000/"+str(script)
logging.basicConfig(format='%(message)s', level=logging.DEBUG) config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name=service, ) # this call also sets opentracing.tracer return config.initialize_tracer() def get_outbound_headers(flask_tracer): headers = {} active_span = flask_tracer.get_span() opentracing.tracer.inject(active_span.context, opentracing.Format.HTTP_HEADERS, headers) return headers open_tracer = init_tracer('client') tracer = FlaskTracer(open_tracer, True, app) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=80)
def get_tracer(app): global tracer if tracer is None: tracer = FlaskTracer(opentracing.tracer, True, app, ["boilerplate-service"]) return tracer
todos[todo_id] = request.form['data'] span.set_tag("put_todo_data", request.form['data']) return {todo_id: todos[todo_id]} if __name__ == '__main__': # Create flask instance app = Flask(__name__) # Create Restful instance api = Api(app) # Set the opentracing configuration tracer = init_tracer('hello-world') # Add the tracer to the app flask_tracer = FlaskTracer(tracer, True, app) # Create parent span parent_span = flask_tracer.get_span() # Add default route to HelloWorld api.add_resource(HelloWorld, '/') # Add route /todo to TodoSimple api.add_resource(TodoSimple, '/todo/<string:todo_id>') # Start the flask app app.run(debug=False, host='0.0.0.0')
config = Config( config={ # usually read from some yaml config 'enabled': True, 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='user-service', validate=True, ) tracer = config.initialize_tracer flask_tracer = FlaskTracer(tracer, True, app) db = SQLAlchemy(app) from user_service.models import User, Token def check_required_fields(req_fields, input_list): """Check if the required fields are present or not in a given list. Keyword arguments: req_fields -- The list of fields required input_list -- The input list to check for Returns: Boolean
from flask import (Flask, request) import opentracing from flask_opentracing import FlaskTracer app = Flask(__name__) test_app = app.test_client() empty_tracer = opentracing.Tracer() tracer_all = FlaskTracer(empty_tracer, True, app, ['url']) tracer = FlaskTracer(empty_tracer) def flush_spans(tcr): for req in tcr._current_spans: tcr._current_spans[req].finish() tcr._current_spans = {} @app.route('/test') def check_test_works(): return "Success" @app.route('/another_test') @tracer.trace('url', 'url_rule') def decorated_fn(): return "Success again" @app.route('/wire') def send_request():
from flask import (Flask, request) import opentracing from flask_opentracing import FlaskTracer app = Flask(__name__) test_app = app.test_client() empty_tracer = opentracing.Tracer() tracer_all = FlaskTracer(empty_tracer, True, app, ['url']) tracer = FlaskTracer(empty_tracer) tracer_deferred = FlaskTracer(lambda: opentracing.Tracer(), True, app, ['url']) def flush_spans(tcr): for req in tcr._current_spans: tcr._current_spans[req].finish() tcr._current_spans = {} @app.route('/test') def check_test_works(): return "Success" @app.route('/another_test') @tracer.trace('url', 'url_rule') def decorated_fn(): return "Success again" @app.route('/wire')
from flask import Flask from flask_opentracing import FlaskTracer import lightstep.tracer import opentracing import urllib2 app = Flask(__name__) # one-time tracer initialization code ls_tracer = lightstep.tracer.init_tracer(group_name="example server", access_token="{your_lightstep_token}") # This tracer traces all requests tracer = FlaskTracer(ls_tracer, True, app, ["url_rule"]) @app.route("/simple") def simple_response(): ''' This request will be automatically traced. ''' return "Hello, world!" @app.route("/childspan") def create_child_span(): ''' This request will also be automatically traced. This is a more complicated example of accessing the current request from within a handler and creating new spans manually. ''' parent_span = tracer.get_span() child_span = ls_tracer.start_span("inside create_child_span", opentracing.child_of(parent_span.context)) ans = calculate_some_stuff()
'local_agent': { 'reporting_host': os.environ['DECL_JAEGER_AGENT_HOST'], 'reporting_port': os.environ['DECL_JAEGER_AGENT_COMPACT_PORT'], }, 'logging': True, }, service_name=service_name, ) tracer = config.initialize_tracer() return tracer app = Flask(__name__) app.logger.addHandler(logging.StreamHandler()) app.logger.setLevel(logging.INFO) flask_tracer = FlaskTracer(create_tracer('flask'), True, app) review_channel = grpc.insecure_channel('com-declayer-store-reviews:5000') review_stub = ReviewsStub(channel=review_channel) product_channel = grpc.insecure_channel('com-declayer-store-products:5000') product_stub = ProductsStub(channel=product_channel) similar_channel = grpc.insecure_channel( 'com-declayer-store-similarproducts:5000') similar_stub = SimilarProductsStub(channel=similar_channel) @REQUEST_TIME.time() def process_request(): # app.logger.info("Count received = %s", count)
class GrTFlaskMiddleware(object): def __init__(self, app, name, host='localhost', rate=0, header='GROFERS_TRACE_ID', patches=[]): self.cfg = Config(config={ 'sampler': { 'type': 'const', 'param': rate }, 'local_agent': { 'reporting_host': host }, 'trace_id_header': header }, service_name=name) self.app = app self.initialized = False app.before_request(self._start_trace) app.after_request(self._end_trace) permitted_patches = [ 'mysqldb', 'psycopg2', 'strict_redis', 'sqlalchemy', 'tornado_http', 'urllib', 'urllib2', 'requests', ] final_patches = [] for patch in patches: if not patch in permitted_patches: raise ValueError('{} is not a valid patch'.format(patch)) else: final_patches.append( 'opentracing_instrumentation.client_hooks.{}.install_patches' .format(patch)) install_patches(final_patches) def _start_trace(self): if not self.initialized: self.tracer = self.cfg.initialize_tracer() self.ftracer = FlaskTracer(self.tracer, False, self.app) self.initialized = True span = self.ftracer.get_span(request) if span is None: span = self.tracer.start_span(request.endpoint) mgr = RequestContextManager(span=span) mgr.__enter__() request.span = span request.mgr = mgr def _end_trace(self, response): request.span.finish() request.mgr.__exit__() return response
from tracingexample.tracer_helper2 import TracerHelper2 logging.getLogger('').handlers = [] logging.basicConfig(format='%(message)s', level=logging.DEBUG) flask_app_name = os.getenv('FLASK_APP_NAME') app = Flask(flask_app_name) api = Api(app) config = Config( config={ 'enabled': True, 'sampler': { 'type': 'const', 'param': 1 }, 'logging': True, }, service_name=flask_app_name, ) jaeger_tracer = config.initialize_tracer flask_tracer = FlaskTracer(jaeger_tracer, True, app, ['url', 'url_rule', 'environ.HTTP_X_REAL_IP', 'path']) #tracer_helper contains some small helper functions tracer_helper = TracerHelper2(flask_tracer) print('wiring up routes') from tracingexample.api_customers import ApiCustomers from tracingexample.api_customersbackend import ApiCustomersBackend from tracingexample.api_orders import ApiOrders from tracingexample.api_ordersbackend import ApiOrdersBackend
config = Config(config={ 'sampler': { 'type': 'const', 'param': 1 }, 'logging': True, 'propagation': 'b3', 'local_agent': { 'reporting_host': JAEGER_HOST } }, service_name=JAEGER_SERVICE_NAME) jaeger_tracer = config.initialize_tracer() tracer = FlaskTracer(jaeger_tracer, True, app, ['url']) @app.route("/analyse") @tracer.trace() def analyse_sentiment(): span = tracer.get_span() request = stack.top.request headers = {} for k, v in request.headers: headers[k.lower()] = v tracer._tracer.inject(span.context, opentracing.Format.TEXT_MAP, headers) span_ctx = tracer._tracer.extract(opentracing.Format.TEXT_MAP, headers) with tracer._tracer.start_span('Send Message', child_of=span_ctx) as span: time.sleep(2)
logging.basicConfig(format='%(message)s', level=logging.DEBUG) config = Config( config={ # usually read from some yaml config 'sampler': {'type': 'const', 'param': 1, }, 'logging': True, 'reporter_batch_size': 1, }, service_name=service, ) return config.initialize_tracer() app = Flask(__name__) tracer = init_tracer('meaning') flask_tracer = FlaskTracer(tracer, True, app, ['url','url_rule','method','path','environ.HTTP_X_REAL_IP']) @app.route("/meaning") def meaning(): parent_span = flask_tracer.get_span() with flask_tracer._tracer.start_span('calculate-meaning-of-life', child_of=parent_span) as span: span.set_tag("ANSWER","42") rand_time = 0.01*random.randint(0,150) time.sleep(rand_time) span.set_tag("DURATION", rand_time) return '42' install_all_patches() if __name__ == "__main__": app.run(port=8084, debug=True)
'sampler': { 'type': 'const', 'param': 1 }, 'logging': True, 'local_agent': # Also, provide a hostname of Jaeger instance to send traces to. { 'reporting_host': JAEGER_HOST } }, # Service name can be arbitrary string describing this particular web service. service_name="app-python") jaeger_tracer = config.initialize_tracer() tracer = FlaskTracer(jaeger_tracer, True, app) class BadRequest(Exception): """Custom exception class to be thrown when local error occurs.""" def __init__(self, message, status=400, payload=None): self.message = message self.status = status self.payload = payload @app.errorhandler(BadRequest) def handle_bad_request(error): """Catch BadRequest exception globally, serialize into JSON, and respond with 400.""" payload = dict(error.payload or ()) payload['status'] = error.status
from flask import Flask from flask_opentracing import FlaskTracer app = Flask(__name__) tracer = FlaskTracer(tracer=some_opentracing_tracer) @app.route("/traced-path") @tracer.trace() def traced_endpoint(): return "This endpoint is traced!" @app.route("/untraced-path") def untraced_endpoint(): return "This endpoint is not traced."
def create_app(): # instantiate the app swagger_config = { "headers": [], "specs": [{ "endpoint": 'swagger', "route": '/swagger.json', "rule_filter": lambda rule: True, # all in "model_filter": lambda tag: True, # all in }], "static_url_path": "/flasgger_static", # "static_folder": "static", # must be set by user "specs_route": "/swagger/" } app = Flask(__name__) app.config['SWAGGER'] = { "swagger": "3.0", 'uiversion': "3", "info": { "title": "BevRand Playlist Api", "version": "1.0" } } Swagger(app, config=swagger_config) log_level = logging.DEBUG logging.getLogger('').handlers = [] logging.basicConfig(format='%(asctime)s %(message)s', level=log_level) # set config env = getenv('APP_SETTINGS') if env is None: env = 'Test' app_settings = 'api.config.' + env app.config.from_object(app_settings) config = Config( config={ 'sampler': { 'type': 'probabilistic', 'param': 0.1 }, 'logging': True, 'local_agent': # Also, provide a hostname of Jaeger instance to send traces to. { 'reporting_host': JAEGER_HOST } }, # Service name can be arbitrary string describing this particular web service. service_name="PlaylistApi") jaeger_tracer = config.initialize_tracer() tracer = FlaskTracer(jaeger_tracer) global FLASK_TRACER FLASK_TRACER = tracer install_all_patches() # register blueprints # coverage needs to be started as soon as possible to include more lines if env == 'Coverage': api.cov = coverage.Coverage(config_file=".coveragerc") api.cov.start() from api.controllers.coverage_controller import coverage_blueprint app.register_blueprint(coverage_blueprint, url_prefix='/api/v1/coverage') from api.controllers.private_controller import private_blueprint app.register_blueprint(private_blueprint, url_prefix='/api/v1/private') from api.controllers.public_controller import public_blueprint app.register_blueprint(public_blueprint, url_prefix='/api/v1/public') return app
import sys import json import requests from flask import Flask from flask import request from lib.tracing import init_tracer, flask_to_scope import opentracing from opentracing.ext import tags from opentracing_instrumentation.client_hooks import install_all_patches from flask_opentracing import FlaskTracer app = Flask('py-6-hello') init_tracer('py-6-hello') install_all_patches() flask_tracer = FlaskTracer(opentracing.tracer, True, app) @app.route("/sayHello/<name>") def say_hello(name): with flask_to_scope(flask_tracer, request) as scope: person = get_person(name) resp = format_greeting(person) opentracing.tracer.active_span.set_tag('response', resp) return resp def get_person(name): with opentracing.tracer.start_active_span('get-person', ) as scope: url = 'http://localhost:8081/getPerson/%s' % name res = _get(url) person = json.loads(res)
'enabled': True, 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name='todo-service', validate=True, ) return config.initialize_tracer() install_all_patches() flask_tracer = FlaskTracer(init_tracer, True, app) db = SQLAlchemy(app) user_service = app.config['USER_SERVICE_ENDPOINT'] from todo_service.models import List, Item def check_required_fields(req_fields, input_list): """Check if the required fields are present inside the input list. Keyword arguments: req_fields -- The list of required fields input_list -- The list to validate for required fields
config={ 'sampler': { 'type': 'const', 'param': 1 }, 'logging': True, 'local_agent': # Also, provide a hostname of Jaeger instance to send traces to. { 'reporting_host': JAEGER_HOST } }, # Service name can be arbitrary string describing this particular web service. service_name="jaeger_opentracing_example") jaeger_tracer = config.initialize_tracer() tracer = FlaskTracer(jaeger_tracer) @app.route('/log') @tracer.trace() # Indicate that /log endpoint should be traced def log(): parent_span = tracer.get_span(request) # Extract the span information for request object. with jaeger_tracer.start_span( "python webserver internal span of log method", child_of=parent_span) as span: # Perform some computations to be traced. a = 1 b = 2 c = a + b
logging.getLogger('').handlers = [] logging.basicConfig(format='%(message)s', level=logging.DEBUG) config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'logging': True, }, service_name=service, ) # this call also sets opentracing.tracer return config.initialize_tracer() def get_outbound_headers(flask_tracer): headers = {} active_span = flask_tracer.get_span() opentracing.tracer.inject(active_span.context, opentracing.Format.HTTP_HEADERS, headers) return headers tracer = FlaskTracer(init_tracer('server'), True, app) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=8000)
service_name='app-backend' ) return config.initialize_tracer() app = Flask(__name__) app.config['JSON_SORT_KEYS'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://{user}:{passwd}@{host}/{db}'.format( user=MYSQL_USER, passwd=MYSQL_PASSWORD, host=MYSQL_HOST, db=MYSQL_DATABASE ) flask_tracer = FlaskTracer(initialize_tracer, True, app) db = SQLAlchemy(app) class Inventory(db.Model): __tablename__ = 'inventory' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20)) category = db.Column(db.String(1000)) class InventorySchema(Schema): class Meta: fields = ('id', 'name', 'category') ordered = True id = fields.Int(dump_only=True)
def init_tracer(): return Config(config={ 'logging': True, 'local_agent': { 'reporting_host': os.getenv("JAEGER_HOST", "localhost") }, 'sampler': { 'type': 'const', 'param': 1 } }, service_name='powersource', validate=True).initialize_tracer() tracer = FlaskTracer(init_tracer, True, app=powersource) def load_characters(): with open(os.path.join(curdir, "characters.json")) as f: characters.extend(json.load(f)) for index, c in enumerate(characters): thumbnail = c["thumbnail"] if thumbnail["path"] == THUMBNAIL_NOT_FOUND: continue indexes.append(index) @powersource.route("/<int:character_id>") @cache.cached(timeout=600) def index(character_id: int):
config = Config( config={ 'sampler': { 'type': 'const', 'param': 1, }, 'local_agent': { 'reporting_host': JAEGER_HOST, 'reporting_port': JAEGER_PORT, }, 'logging': True, }, service_name=JAEGER_SVC_NAME, ) opentracing_tracer = config.initialize_tracer() tracer = FlaskTracer(opentracing_tracer) class Users(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(200), unique=True, nullable=False) password = db.Column(db.String(20), nullable=False) @app.route('/auth', methods=['POST']) @tracer.trace() def login(): if request.method == 'POST': logging.info("Request headers: {}".format(request.headers)) span = tracer.get_span()
def sigterm_handler(_signo, _stack_frame): sys.exit(0) signal.signal(signal.SIGTERM, sigterm_handler) # initialize the configuration parser with all the existing environment variables parser = SafeConfigParser(os.environ) # default location of ./feature.flags is used if the environment variable isn’t set config_file = Path(os.environ.get('CONFIG_FILE', './feature.flags')) # verify file exists before attempting to read and extend the configuration if config_file.is_file(): parser.read(os.environ.get('CONFIG_FILE')) redis_store = None app = Flask(__name__) flask_tracer = FlaskTracer(initialize_tracer, True, app, ["url_rule"]) @app.route('/') def index(): return "Index Page" @app.route('/activeconfig') def activeconfig(): output = "" for each_section in parser.sections(): output += "SECTION: "+each_section+"\n" for (each_key, each_val) in parser.items(each_section): output += each_key+" : "+each_val+"\n" return output @app.route('/hello')