Pull count of detection events by label into prometheus metrics (#20119)

* pull count of detection events by label into prometheus metrics

* format changes with ruff

* remove unneeded f-string

* fix imports format

---------

Co-authored-by: iesad <iesad>
This commit is contained in:
iesad
2025-09-19 06:27:20 -06:00
committed by GitHub
co-authored by iesad <iesad>
parent 1408abb050
commit dc96940eb9
4 changed files with 136 additions and 46 deletions
+10 -3
View File
@@ -11,7 +11,7 @@ from datetime import datetime, timedelta
from functools import reduce
from io import StringIO
from pathlib import Path as FilePath
from typing import Any, Optional
from typing import Any, Dict, List, Optional
import aiofiles
import requests
@@ -21,7 +21,7 @@ from fastapi.encoders import jsonable_encoder
from fastapi.params import Depends
from fastapi.responses import JSONResponse, PlainTextResponse, StreamingResponse
from markupsafe import escape
from peewee import SQL, operator
from peewee import SQL, fn, operator
from pydantic import ValidationError
from frigate.api.auth import require_role
@@ -130,7 +130,14 @@ def metrics(request: Request):
"""Expose Prometheus metrics endpoint and update metrics with latest stats"""
# Retrieve the latest statistics and update the Prometheus metrics
stats = request.app.stats_emitter.get_latest_stats()
update_metrics(stats)
# query DB for count of events by camera, label
event_counts: List[Dict[str, Any]] = (
Event.select(Event.camera, Event.label, fn.Count())
.group_by(Event.camera, Event.label)
.dicts()
)
update_metrics(stats=stats, event_counts=event_counts)
content, content_type = get_metrics()
return Response(content=content, media_type=content_type)