1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
from flask import Flask, Blueprint, request
from flask_restful import Resource, Api
from prometheus_flask_exporter import PrometheusMetrics
app = Flask(__name__)
blueprint = Blueprint('api_v1', __name__, url_prefix='/api/v1')
restful_api = Api(blueprint)
metrics = PrometheusMetrics(app)
class Test(Resource):
status = 200
@staticmethod
@metrics.summary('test_by_status', 'Test Request latencies by status', labels={
'code': lambda r: r.status_code
})
def get():
if 'fail' in request.args:
return 'Not OK', 400
else:
return 'OK'
restful_api.add_resource(Test, '/test', endpoint='test')
app.register_blueprint(blueprint)
if __name__ == '__main__':
app.run('0.0.0.0', 4000)
|