Skip to content

Commit

Permalink
Add prometheus /metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Ledez committed Oct 4, 2023
1 parent c717f13 commit 0bd4fa8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ install_requires =
OpenTimelineIO==0.15.0
orjson==3.9.7
pillow==10.0.1
prometheus-flask-exporter==0.22.4
psutil==5.9.5
psycopg[binary]==3.1.12
pygelf==0.4.2
Expand Down
16 changes: 16 additions & 0 deletions zou/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,24 @@ def add_permissions(_, payload):

def load_api():
from zou.app import api
from zou import __version__ as zou_version

api.configure(app)
try:
from prometheus_flask_exporter.multiprocess import (
GunicornPrometheusMetrics,
)

metrics = GunicornPrometheusMetrics(
app, defaults_prefix="zou", group_by="url_rule"
)
except ValueError:
from prometheus_flask_exporter import RESTfulPrometheusMetrics

metrics = RESTfulPrometheusMetrics(
app, api, defaults_prefix="zou", group_by="url_rule"
)
metrics.info("zou_info", "Application info", version=zou_version)

fs.mkdir_p(app.config["TMP_DIR"])
configure_auth()
Expand Down
29 changes: 19 additions & 10 deletions zou/app/services/stats_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,26 @@


def get_main_stats():
metric_number_of_video_previews = metrics.gauge(
"zou_number_of_video_previews", "Number of video previews"
)
number_of_video_previews = PreviewFile.query.filter(
PreviewFile.extension == "mp4"
).count()
number_of_picture_previews = PreviewFile.query.filter(
PreviewFile.extension == "png"
).count()
number_of_model_previews = PreviewFile.query.filter(
PreviewFile.extension == "obj"
).count()
number_of_comments = Comment.query.count()

metric_number_of_video_previews.set(number_of_video_previews)
return {
"number_of_video_previews": PreviewFile.query.filter(
PreviewFile.extension == "mp4"
).count(),
"number_of_picture_previews": PreviewFile.query.filter(
PreviewFile.extension == "png"
).count(),
"number_of_model_previews": PreviewFile.query.filter(
PreviewFile.extension == "obj"
).count(),
"number_of_comments": Comment.query.count(),
"number_of_video_previews": number_of_video_previews,
"number_of_picture_previews": number_of_picture_previews,
"number_of_model_previews": number_of_model_previews,
"number_of_comments": number_of_comments,
}


Expand Down

0 comments on commit 0bd4fa8

Please sign in to comment.