Skip to content

Commit

Permalink
Add promethes-flask-exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
jawadqur committed May 6, 2024
1 parent a47e104 commit 7b2019a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
40 changes: 8 additions & 32 deletions fence/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@
import fence.blueprints.register
import fence.blueprints.ga4gh


# for some reason the temp dir does not get created properly if we move
# this statement to `_setup_prometheus()`
PROMETHEUS_TMP_COUNTER_DIR = tempfile.TemporaryDirectory()
# Prometheus metrics exporter
from prometheus_flask_exporter import PrometheusMetrics


app = flask.Flask(__name__)
CORS(app=app, headers=["content-type", "accept"], expose_headers="*")


metrics = PrometheusMetrics(app, path=None)

# Serve metrics on port 9090
metrics.start_http_server(9090)


def warn_about_logger():
raise Exception(
"Flask 0.12 will remove and replace all of our log handlers if you call "
Expand Down Expand Up @@ -366,13 +370,6 @@ def app_config(
_load_keys(app, root_dir)
_set_authlib_cfgs(app)

app.prometheus_counters = {}
if config["ENABLE_PROMETHEUS_METRICS"]:
logger.info("Enabling Prometheus metrics...")
_setup_prometheus(app)
else:
logger.info("Prometheus metrics are NOT enabled.")

app.storage_manager = StorageManager(config["STORAGE_CREDENTIALS"], logger=logger)

app.debug = config["DEBUG"]
Expand Down Expand Up @@ -511,27 +508,6 @@ def _setup_audit_service_client(app):
)


def _setup_prometheus(app):
# This environment variable MUST be declared before importing the
# prometheus modules (or unit tests fail)
# More details on this awkwardness: https://github.com/prometheus/client_python/issues/250
os.environ["prometheus_multiproc_dir"] = PROMETHEUS_TMP_COUNTER_DIR.name

from prometheus_client import (
CollectorRegistry,
multiprocess,
make_wsgi_app,
)

app.prometheus_registry = CollectorRegistry()
multiprocess.MultiProcessCollector(app.prometheus_registry)

# Add prometheus wsgi middleware to route /metrics requests
app.wsgi_app = DispatcherMiddleware(
app.wsgi_app, {"/metrics": make_wsgi_app(registry=app.prometheus_registry)}
)


@app.errorhandler(Exception)
def handle_error(error):
"""
Expand Down
31 changes: 29 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ cachelib = "^0.2.0"
azure-storage-blob = "^12.6.0"
Flask-WTF = "^1.0.0"
boto = "*"
prometheus-flask-exporter = "^0.23.0"

[tool.poetry.dev-dependencies]
addict = "^2.2.1"
Expand Down

0 comments on commit 7b2019a

Please sign in to comment.