コード例 #1
0
logger = logging.getLogger("sso-dashboard")

app = Flask(__name__)
everett_config = get_config()
# Enable monitoring endpoint
if (everett_config("enable_prometheus_monitoring",
                   namespace="sso-dashboard",
                   default="False") == "True"):
    os.environ["prometheus_multiproc_dir"] = "/tmp"
    registry = CollectorRegistry()
    multiprocess.MultiProcessCollector(registry, path="/tmp")
    metrics = PrometheusMetrics(app)
    metrics.start_http_server(
        int(
            everett_config("prometheus_monitoring_port",
                           namespace="sso-dashboard",
                           default="9000")))

talisman = Talisman(app,
                    content_security_policy=DASHBOARD_CSP,
                    force_https=False)

app.config.from_object(config.Config(app).settings)
app_list = S3Transfer(config.Config(app).settings)
app_list.sync_config()

assets = Environment(app)
js = Bundle("js/base.js", filters="jsmin", output="js/gen/packed.js")
assets.register("js_all", js)
コード例 #2
0
def create_app(runtime_environment):
    connexion_options = {"swagger_ui": True, "uri_parser_class": customURIParser}
    # This feels like a hack but it is needed.  The logging configuration
    # needs to be setup before the flask app is initialized.
    configure_logging()

    app_config = Config(runtime_environment)
    app_config.log_configuration()

    connexion_app = connexion.App("inventory", specification_dir="./swagger/", options=connexion_options)

    parser = TranslatingParser(SPECIFICATION_FILE)
    parser.parse()
    for api_url in app_config.api_urls:
        if api_url:
            connexion_app.add_api(
                parser.specification,
                arguments={"title": "RestyResolver Example"},
                resolver=RestyResolver("api"),
                validate_responses=True,
                strict_validation=True,
                base_path=api_url,
                validator_map=VALIDATOR_MAP,
            )
            logger.info("Listening on API: %s", api_url)

    # Add an error handler that will convert our top level exceptions
    # into error responses
    connexion_app.add_error_handler(InventoryException, render_exception)

    flask_app = connexion_app.app

    flask_app.config["SQLALCHEMY_ECHO"] = False
    flask_app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    flask_app.config["SQLALCHEMY_DATABASE_URI"] = app_config.db_uri
    flask_app.config["SQLALCHEMY_POOL_SIZE"] = app_config.db_pool_size
    flask_app.config["SQLALCHEMY_POOL_TIMEOUT"] = app_config.db_pool_timeout

    flask_app.config["INVENTORY_CONFIG"] = app_config

    flask_app.config["SYSTEM_PROFILE_SPEC"] = process_system_profile_spec()

    db.init_app(flask_app)

    register_shutdown(db.get_engine(flask_app).dispose, "Closing database")

    flask_app.register_blueprint(monitoring_blueprint, url_prefix=app_config.mgmt_url_path_prefix)

    @flask_app.before_request
    def set_request_id():
        threadctx.request_id = request.headers.get(REQUEST_ID_HEADER, UNKNOWN_REQUEST_ID_VALUE)

    if runtime_environment.event_producer_enabled:
        flask_app.event_producer = EventProducer(app_config)
        register_shutdown(flask_app.event_producer.close, "Closing EventProducer")
    else:
        logger.warning(
            "WARNING: The event producer has been disabled.  "
            "The message queue based event notifications have been disabled."
        )

    payload_tracker_producer = None
    if not runtime_environment.payload_tracker_enabled:
        # If we are running in "testing" mode, then inject the NullProducer.
        payload_tracker_producer = payload_tracker.NullProducer()

        logger.warning(
            "WARNING: Using the NullProducer for the payload tracker producer.  "
            "No payload tracker events will be sent to to payload tracker."
        )

    payload_tracker.init_payload_tracker(app_config, producer=payload_tracker_producer)

    # HTTP request metrics
    if runtime_environment.metrics_endpoint_enabled:
        metrics = PrometheusMetrics(
            flask_app,
            defaults_prefix="inventory",
            group_by="url_rule",
            excluded_paths=["^/metrics$", "^/health$", "^/version$", r"^/favicon\.ico$"],
        )
        metrics.start_http_server(app_config.metrics_port, endpoint=app_config.metrics_path)

    # initialize metrics to zero
    initialize_metrics(app_config)

    return flask_app
コード例 #3
0
import logging
from flask import Flask
from flask import jsonify
from prometheus_flask_exporter import PrometheusMetrics

logging.basicConfig(level=logging.INFO)
logging.info("Setting LOGLEVEL to INFO")
app = Flask(__name__)

metrics = PrometheusMetrics(app)
metrics.info("app_info",
             "App Info, this can be anything you want",
             version="1.0.0")
metrics.start_http_server(5099)


@app.route("/flask-prometheus-grafana-example/")
def hello():
    return jsonify(say_hello())


@app.route("/")
def say_hello():
    return {"message": "hello"}


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)