Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate prometheus metrics for flask and add metrics endpoint #399

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()