feat: add Prometheus metrics for Redis cache

Adding a Prometheus counter to be able to monitor cache hits/misses
for search queries

Signed-off-by: moson-mo <mo-son@mailbox.org>
This commit is contained in:
moson-mo 2023-07-04 09:40:39 +02:00
parent 3acfb08a0f
commit 814ccf6b04
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
2 changed files with 51 additions and 2 deletions

40
test/test_metrics.py Normal file
View file

@ -0,0 +1,40 @@
import pytest
from prometheus_client import REGISTRY, generate_latest
from aurweb import db
from aurweb.cache import db_query_cache
from aurweb.models.account_type import USER_ID
from aurweb.models.user import User
@pytest.fixture(autouse=True)
def setup(db_test):
return
@pytest.fixture
def user() -> User:
with db.begin():
user = db.create(
User,
Username="test",
Email="test@example.org",
RealName="Test User",
Passwd="testPassword",
AccountTypeID=USER_ID,
)
yield user
@pytest.mark.asyncio
async def test_search_cache_metrics(user: User):
# Fire off 3 identical queries for caching
for _ in range(3):
await db_query_cache("key", db.query(User))
# Get metrics
metrics = str(generate_latest(REGISTRY))
# We should have 1 miss and 2 hits
assert 'search_requests_total{cache="miss"} 1.0' in metrics
assert 'search_requests_total{cache="hit"} 2.0' in metrics