From 3f6e94a80a3765ed04b0458c2515935979094734 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Fri, 16 Aug 2024 16:23:24 +0100 Subject: [PATCH] Add Prometheus metrics --- Makefile | 2 +- poetry.lock | 31 ++++++++++++++++++++++++++++++- pyproject.toml | 1 + simple_webfinger/app.py | 6 ++++++ 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index f62e927..fbe765c 100644 --- a/Makefile +++ b/Makefile @@ -14,4 +14,4 @@ serve-uwsgi: SIMPLE_WEBFINGER_CONFIG_FILE="examples/example-config.yaml" python3 -m poetry run uwsgi -w "simple_webfinger.app:create_app()" --master --http 0.0.0.0:8000 serve: - SIMPLE_WEBFINGER_CONFIG_FILE="examples/example-config.yaml" FLASK_DEBUG=1 FLASK_APP="simple_webfinger.app:create_app()" python3 -m poetry run flask run + DEBUG_METRICS=1 SIMPLE_WEBFINGER_CONFIG_FILE="examples/example-config.yaml" FLASK_DEBUG=1 FLASK_APP="simple_webfinger.app:create_app()" python3 -m poetry run flask run diff --git a/poetry.lock b/poetry.lock index 66064d9..b3b76cb 100644 --- a/poetry.lock +++ b/poetry.lock @@ -203,6 +203,35 @@ files = [ dev = ["pre-commit", "tox"] testing = ["pytest", "pytest-benchmark"] +[[package]] +name = "prometheus-client" +version = "0.20.0" +description = "Python client for the Prometheus monitoring system." +optional = false +python-versions = ">=3.8" +files = [ + {file = "prometheus_client-0.20.0-py3-none-any.whl", hash = "sha256:cde524a85bce83ca359cc837f28b8c0db5cac7aa653a588fd7e84ba061c329e7"}, + {file = "prometheus_client-0.20.0.tar.gz", hash = "sha256:287629d00b147a32dcb2be0b9df905da599b2d82f80377083ec8463309a4bb89"}, +] + +[package.extras] +twisted = ["twisted"] + +[[package]] +name = "prometheus-flask-exporter" +version = "0.23.1" +description = "Prometheus metrics exporter for Flask" +optional = false +python-versions = "*" +files = [ + {file = "prometheus_flask_exporter-0.23.1-py3-none-any.whl", hash = "sha256:ab49b2c40b57cd35cd51e91e59b3c306b3754477095c4f3cf679034c5122398c"}, + {file = "prometheus_flask_exporter-0.23.1.tar.gz", hash = "sha256:587c770a1061e93d72c5cbcdefbd7b633fb764e39dffd7dd16932c9124559244"}, +] + +[package.dependencies] +flask = "*" +prometheus-client = "*" + [[package]] name = "pydantic" version = "2.8.2" @@ -526,4 +555,4 @@ watchdog = ["watchdog (>=2.3)"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "b042304154f4d2a7ccbe9fffcebc6db3c38549d73e080d317b7d039c33a28fa3" +content-hash = "b221de707ffd32f063004ac38e899c2065608fbd7717d0e8885e42fb554e4b14" diff --git a/pyproject.toml b/pyproject.toml index 2e8d77a..98502e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ Flask = "^3.0.3" PyYAML = "^6.0.2" uwsgi = "^2.0.26" pydantic = "^2.8.2" +prometheus-flask-exporter = "^0.23.1" [tool.poetry.group.dev.dependencies] pytest = "^8.0.0" diff --git a/simple_webfinger/app.py b/simple_webfinger/app.py index 28d5511..55241da 100644 --- a/simple_webfinger/app.py +++ b/simple_webfinger/app.py @@ -2,6 +2,8 @@ from urllib.parse import urlparse import yaml from flask import Flask, abort, request +from prometheus_flask_exporter import PrometheusMetrics + from simple_webfinger.models.webfinger import JSONResourceDefinition @@ -62,6 +64,10 @@ def filter_links(links: dict[str, str], rel: list[str]) -> list: def create_app(config={}): app = Flask("simple_webfinger") + + metrics = PrometheusMetrics(app) + metrics.info('app_info', 'Application info', version='0.1.0') + app.webfinger_config = { "domain": None, "accounts": {},