import os from flask import request from mlflow.pyfunc import scoring_server, load_model from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics app = scoring_server.init(load_model(os.getenv('MODEL_PATH'))) metrics = GunicornInternalPrometheusMetrics(app, defaults_prefix=os.getenv('STATSD_PREFIX')) metrics.register_default( metrics.counter( 'by_path_counter', 'Request count by request paths', labels={'path': lambda: request.path} ) )
@app.after_request def append_request_id(response): response.headers.add("X-REQUEST-ID", current_request_id()) return response @app.before_request def start_timer(): g.ogc_start_time = monotonic() @app.after_request def log_time_and_request_response(response): time_taken = int((monotonic() - g.ogc_start_time) * 1000) _LOG.info("request: %s returned status: %d and took: %d ms", request.url, response.status_code, time_taken) return response # Note: register your default metrics after all routes have been set up. # Also note, that Gauge metrics registered as default will track the /metrics endpoint, and this can't be disabled at the moment. if os.environ.get("prometheus_multiproc_dir", False): metrics.register_default( metrics.summary( 'flask_ows_request_full_url', 'Request summary by request url', labels={ 'query_request': lambda: request.args.get('request'), 'query_service': lambda: request.args.get('service'), 'query_layers': lambda: request.args.get('layers'), 'query_url': lambda: request.full_path } ) )