Skip to content

Commit

Permalink
Generate prometheus metrics for flask and add metrics endpoint (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
plars authored Nov 20, 2024
1 parent 35d4122 commit 7fd2a50
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ urllib3 = "^2.2.1"
pymongo = "<4.9.0"
pyjwt = "^2.8.0"
bcrypt = "^4.2.0"
prometheus-flask-exporter = "^0.23.1"

[tool.poetry.dev-dependencies]
pytest = "^8.1.2"
Expand Down
9 changes: 7 additions & 2 deletions server/src/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@
from . import schemas


jobs_metric = Counter("jobs", "Number of jobs", ["queue"])
jobs_metric = Counter(
"jobs", "Number of jobs", ["queue"], namespace="testflinger"
)
reservations_metric = Counter(
"reservations", "Number of reservations", ["queue"]
"reservations",
"Number of reservations",
["queue"],
namespace="testflinger",
)


Expand Down
4 changes: 4 additions & 0 deletions server/src/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from src.api.v1 import v1
from src.providers import ISODatetimeProvider
from src.views import views
from .extensions import metrics

try:
import sentry_sdk
Expand Down Expand Up @@ -63,6 +64,9 @@ def create_flask_app(config=None):
dsn=sentry_dsn, integrations=[FlaskIntegration()]
)

metrics.group_by = "endpoint"
metrics.init_app(tf_app)

@tf_app.errorhandler(NotFound)
def handle_404(exc):
tf_log.error("[404] Not found: %s", request.url)
Expand Down
7 changes: 7 additions & 0 deletions server/src/extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Flask extensions that may be imported by other modules
"""

from prometheus_flask_exporter import PrometheusMetrics

metrics = PrometheusMetrics.for_app_factory()

0 comments on commit 7fd2a50

Please sign in to comment.