def initialise_prometheus(app, log=None): if os.environ.get("PROMETHEUS_MULTIPROC_DIR", False): metrics = GunicornInternalPrometheusMetrics(app) if log: log.info("Prometheus metrics enabled") return metrics return None
def enable_prometheus(): # Enable deployment specific code for Prometheus metrics if os.environ.get("prometheus_multiproc_dir", False): from prometheus_flask_exporter.multiprocess import ( GunicornInternalPrometheusMetrics, ) metrics = GunicornInternalPrometheusMetrics(app) _LOG.info(f"Prometheus metrics enabled : {metrics}")
def initialise_prometheus(app, log=None): # Prometheus if os.environ.get("prometheus_multiproc_dir", False): metrics = GunicornInternalPrometheusMetrics(app) if log: log.info("Prometheus metrics enabled") return metrics return None
def app() -> Flask: app = create_app('myapp.config.TestConfig') prometheus_client.REGISTRY = prometheus_client.CollectorRegistry( auto_describe=True) myapp_extensions.metrics = GunicornInternalPrometheusMetrics( app=None, group_by="endpoint") ctx = app.app_context() ctx.push() yield app ctx.pop()
def activate_prometheus_exporter(app): def mlflow_version(_: request): return VERSION metrics = GunicornInternalPrometheusMetrics( app, export_defaults=True, defaults_prefix="mlflow", excluded_paths=["/health"], group_by=mlflow_version, ) return metrics
def activate_prometheus_exporter(app): metrics = GunicornInternalPrometheusMetrics(app, export_defaults=False) endpoint = app.view_functions histogram = metrics.histogram( 'mlflow_requests_by_status_and_path', 'Request latencies and count by status and path', labels={ 'status': lambda r: r.status_code, 'path': lambda: change_path_for_metric(request.path) }) for func_name, func in endpoint.items(): if func_name in [ "_search_runs", "_log_metric", "_log_param", "_set_tag", "_create_run" ]: app.view_functions[func_name] = histogram(func) return app
def create_app(): """Create Connexion/Flask application.""" root = os.path.dirname(rhub.__path__[0]) connexion_app = connexion.App(__name__) flask_app = connexion_app.app flask_app.url_map.strict_slashes = False if os.getenv('PROMETHEUS_MULTIPROC_DIR'): GunicornInternalPrometheusMetrics(flask_app) from . import _config flask_app.config.from_object(_config) parser = prance.ResolvingParser( os.path.join(root, 'openapi', 'openapi.yml')) connexion_app.add_api( parser.specification, validate_responses=True, strict_validation=True, pythonic_params=True, ) # Enable CORS (Cross-Origin Resource Sharing) # https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS CORS(flask_app) flask_app.cli.add_command(init_command) db.init_app(flask_app) migrate.init_app(flask_app, db) try: import coloredlogs coloredlogs.install(level=flask_app.config['LOG_LEVEL']) except ImportError: logging.basicConfig(level=flask_app.config['LOG_LEVEL']) RHUB_RETURN_INITIAL_FLASK_APP = os.getenv('RHUB_RETURN_INITIAL_FLASK_APP', 'False') if str(RHUB_RETURN_INITIAL_FLASK_APP).lower() == 'true': return flask_app FlaskInjector( app=flask_app, injector=di, modules=[ KeycloakModule(flask_app), VaultModule(flask_app), SchedulerModule(flask_app), ], ) # Try to retrieve Tower notification webhook creds from vault try: with flask_app.app_context(): vault = di.get(Vault) webhookCreds = vault.read(flask_app.config['WEBHOOK_VAULT_PATH']) if webhookCreds: flask_app.config['WEBHOOK_USER'] = webhookCreds['username'] flask_app.config['WEBHOOK_PASS'] = webhookCreds['password'] else: raise Exception( 'Missing tower webhook notification credentials; ' f'{vault} {flask_app.config["WEBHOOK_VAULT_PATH"]}') except Exception as e: logger.error( f'Failed to load {flask_app.config["WEBHOOK_VAULT_PATH"]} tower' f' webhook notification credentials {e!s}.') return flask_app
from flask import Flask from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics app = Flask(__name__) metrics = GunicornInternalPrometheusMetrics(app) @app.route('/test') def index(): return 'Hello world' @app.route('/error') def error(): raise Exception('Fail') if __name__ == '__main__': app.run(debug=False, port=5000)
def __init__(self, app, config): self.config = config if os.getenv('prometheus_multiproc_dir'): self.gunicorn_metrics = GunicornInternalPrometheusMetrics( app, path="/metrics")
from flask import Flask from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics application = Flask(__name__) metrics = GunicornInternalPrometheusMetrics(application) # static information as metric metrics.info('app_info', 'Application info', version='1.0.3') @application.route('/test') def main(): raise Exception("Crashing") pass # requests tracked by default if __name__ == '__main__': application.run(debug=False, port=5000)
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} ) )
from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics metrics = GunicornInternalPrometheusMetrics(app=None, group_by="endpoint") def setup_extensions(app): metrics.init_app(app) return app
def init_metrics(app): myapp = GunicornInternalPrometheusMetrics(app, registry=CollectorRegistry())