Пример #1
0
from flask import Flask
from .models import db
from .config import DB_URI
from flask import request
from .rbac_interface import ensure_has_permission
from .config import get_logger
from prometheus_flask_exporter import RESTfulPrometheusMetrics

# Since we're using flask_sqlalchemy, we must create the flask app in both processor and web api
app = Flask(__name__)
# Initalize database connection
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
metrics = RESTfulPrometheusMetrics.for_app_factory(defaults_prefix='ros')
db.init_app(app)


@app.before_request
def ensure_rbac():
    if request.endpoint not in ['status', 'prometheus_metrics']:
        ensure_has_permission(permissions=["ros:*:*", "ros:*:read"],
                              application="ros",
                              app_name="ros",
                              request=request,
                              logger=get_logger(__name__))
Пример #2
0
from flask import Flask
from flask_restful import Resource, Api, reqparse, abort, marshal, fields
from flask_cors import CORS
from prometheus_flask_exporter import RESTfulPrometheusMetrics

# Initialize Flask
app = Flask(__name__)
CORS(app)
api = Api(app)

metrics = RESTfulPrometheusMetrics(app, api)

metrics.info('app_info', 'Application info', version='1.0', app_name='devops-bookstore-api')

# A List of Dicts to store all of the books
books = [{
        "bookTitle": "Learning Docker" ,
        "bookImage": "https://itbook.store/img/books/9781784397937.png",
        "bookDescription": "Docker is a next-generation platform for simplifying application containerization life-cycle. Docker allows you to create a robust and resilient environment in which you can generate portable, composable, scalable, and stable application containers.",
        "bookAuthors" : "Pethuru Raj, Jeeva S. Chelladhurai, Vinod Singh"
    },
    {
        "bookTitle": "Kubernetes Best Practices" ,
        "bookImage": "https://itbook.store/img/books/9781492056478.png",
        "bookDescription": "In this practical guide, four Kubernetes professionals with deep experience in distributed systems, enterprise application development, and open source will guide you through the process of building applications with container orchestration.",
        "bookAuthors" : "Brendan Burns, Eddie Villalba"
    },
    {
        "bookTitle": "Site Reliability Engineering" ,
        "bookImage": "https://itbook.store/img/books/9781491929124.png",
        "bookDescription": "The overwhelming majority of a software system's lifespan is spent in use, not in design or implementation. So, why does conventional wisdom insist that software engineers focus primarily on the design and development of large-scale computing systems?",
Пример #3
0
from flask import Flask, request
from flask_restful import Resource, Api
from prometheus_flask_exporter import RESTfulPrometheusMetrics

app = Flask(__name__)
restful_api = Api(app)

metrics = RESTfulPrometheusMetrics.for_app_factory()


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 None, 400
        else:
            return None, 200


restful_api.add_resource(Test, '/api/v1/test', endpoint='test')

if __name__ == '__main__':
    metrics.init_app(app, restful_api)
    app.run('0.0.0.0', 4000)