def app() -> Flask: app = create_app('myapp.config.TestConfig') prometheus_client.REGISTRY = prometheus_client.CollectorRegistry( auto_describe=True) myapp_extensions.metrics = GunicornInternalPrometheusMetrics.for_app_factory( group_by="endpoint") ctx = app.app_context() ctx.push() yield app ctx.pop()
def init_prometheus_flask_exporter(self, app): enable_exporter_flask = app.config.get( "PROMETHEUS_ENABLE_EXPORTER_FLASK", False) if not enable_exporter_flask: LOGGER.debug( f"Prometheus Flask exporter is not enabled for {app.name}.") return prefix = app.name metrics_flask = GunicornInternalPrometheusMetrics.for_app_factory( defaults_prefix=prefix, group_by=url_rule) metrics_flask.init_app(app) LOGGER.debug( f"Prometheus Flask exporter is initialized with prefix {prefix}.")
def init_metrics(app): """ Initialize the Prometheus Flask Exporter. :return: a Prometheus Flash Metrics object :rtype: PrometheusMetrics """ registry = CollectorRegistry() multiproc_temp_dir = app.config["PROMETHEUS_METRICS_TEMP_DIR"] hostname = socket.gethostname() if not os.path.isdir(multiproc_temp_dir): os.makedirs(multiproc_temp_dir) multiprocess.MultiProcessCollector(registry, path=multiproc_temp_dir) metrics = GunicornInternalPrometheusMetrics.for_app_factory( default_labels={"host": hostname}, group_by="endpoint", defaults_prefix="cachito_flask" ) metrics.init_app(app)
docs.register(date) @app.route("/api/predict/coreness", methods=["POST"]) @use_kwargs({ 'title': fields.Str(required=True), 'abstract': fields.Str(required=True) }) @marshal_with(serializers.ClassifierOutputSerializer) def core_classifier(**kwargs): """Endpoint for the CORE classifier.""" return predict_coreness(kwargs['title'], kwargs['abstract']) docs.register(core_classifier) return app @app.errorhandler(404) def page_not_found(e): return {"errors": [str(e)]}, 404 app = create_app() if app.config.get('PROMETHEUS_ENABLE_EXPORTER_FLASK'): logging.info("Starting prometheus metrics exporter") metrics = GunicornInternalPrometheusMetrics.for_app_factory() metrics.init_app(app) if __name__ == '__main__': app.run(host='0.0.0.0')
app = Flask(__name__) metrics.init_app(app) # Add prometheus wsgi middleware to route /metrics requests app.wsgi_app = DispatcherMiddleware( app.wsgi_app, {'/metrics': make_wsgi_app(registry=REGISTRY)}) init() @app.route('/test') def main(): return 'Ok' return app REGISTRY.register(CustomCollector()) metrics = GunicornInternalPrometheusMetrics.for_app_factory(path='/metrics', static_labels={ 'node': 'xxx', 'pod': 'xx', 'version': 'xx' }, registry=REGISTRY) if __name__ == '__main__': options = {'bind': ['0.0.0.0:9200'], 'workers': 4, 'loglevel': 'debug'} std_app = StandaloneApplication(create_app(), options) std_app.run()
from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics metrics = GunicornInternalPrometheusMetrics.for_app_factory( group_by="endpoint") def setup_extensions(app): metrics.init_app(app) return app