import logging import os import time from flask import Flask, request from prometheus_flask_exporter import PrometheusMetrics app = Flask(__name__) metrics = PrometheusMetrics(app) metrics.register_default( metrics.counter('by_path_counter', 'Request count by request paths', labels={'path': lambda: request.path})) @app.route('/handler') def handler(): return 'OK' if __name__ == '__main__': logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG) app.run(host='0.0.0.0', port=os.getenv("HTTP_PORT", 8081))
# "collection": "IMC", # } dataMongo = { "url": "mongodb://*****:*****@app.route('/') @app.route('/index') @contador_por_path def index(): entries = MongoAPI(dataMongo).read() return render_template('index.html', entries=entries, meuhost=meuhost)
from pymongo import MongoClient from bson.json_util import dumps from bson import json_util from prometheus_flask_exporter import PrometheusMetrics app = Flask(__name__) metrics = PrometheusMetrics(app) metrics.info("flask_app_info", "App Info, this can be anything you want", version="1.0.0") cache = redis.Redis(host='redis', port=6379) # custom metric to be applied to multiple endpoints common_counter = metrics.counter('flask_by_endpoint_counter', 'Request count by endpoints', labels={'endpoint': lambda: request.endpoint}) def get_hit_count(): time.sleep(random() * 0.5) retries = 5 while True: try: return cache.incr('hits') except redis.exceptions.ConnectionError as exc: if retries == 0: raise exc retries -= 1 time.sleep(0.5)
import os from flask import Flask, render_template, redirect from flask.globals import request from prometheus_flask_exporter import PrometheusMetrics app = Flask(__name__) metrics = PrometheusMetrics(app) metrics.info('app_info', 'Application info', version='1.0.3') buying = metrics.counter('buy_request', 'A user have buyed the product', labels={ 'status': lambda r: r.status_code, 'path': lambda: request.path }) @app.route('/') def index(): return render_template("index.html") @app.route('/buy/<product_id>') @buying def buy(product_id=''): return redirect('/') if __name__ == "__main__": app.run(host=os.environ["APP_HOST"], port=os.environ["APP_PORT"])