Skip to content
Open
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
78 changes: 45 additions & 33 deletions backend/tests/test_metrics_service.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,79 @@
"""
Unit tests for metrics_service.py
Tests for backend/services/metrics_service.py
"""

import pytest
from prometheus_client import Histogram, Counter


class TestMetricsService:
def test_classifier_latency_histogram_exists(self):
from backend.services.metrics_service import CLASSIFIER_LATENCY
"""Test cases for Prometheus metrics service."""

assert isinstance(CLASSIFIER_LATENCY, Histogram)
assert CLASSIFIER_LATENCY._documentation == "Latency of DistilBERT classifier inference in seconds"
def test_classifier_latency_histogram_exists(self):
"""Test that CLASSIFIER_LATENCY histogram is properly configured."""
from prometheus_client import Histogram

def test_classifier_latency_labels(self):
from backend.services.metrics_service import CLASSIFIER_LATENCY

assert isinstance(CLASSIFIER_LATENCY, Histogram)
assert "Latency" in CLASSIFIER_LATENCY._documentation
assert "model" in CLASSIFIER_LATENCY._labelnames

def test_classifier_latency_buckets(self):
from backend.services.metrics_service import CLASSIFIER_LATENCY

expected = [0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]
assert CLASSIFIER_LATENCY._upper_bounds[:-1] == expected

def test_classifier_requests_counter_exists(self):
from backend.services.metrics_service import CLASSIFIER_REQUESTS
"""Test that CLASSIFIER_REQUESTS counter is properly configured."""
from prometheus_client import Counter

assert isinstance(CLASSIFIER_REQUESTS, Counter)
assert CLASSIFIER_REQUESTS._documentation == "Total number of classifier inference requests"

def test_classifier_requests_labels(self):
from backend.services.metrics_service import CLASSIFIER_REQUESTS

assert isinstance(CLASSIFIER_REQUESTS, Counter)
assert "Total" in CLASSIFIER_REQUESTS._documentation
assert "model" in CLASSIFIER_REQUESTS._labelnames
assert "status" in CLASSIFIER_REQUESTS._labelnames

def test_classifier_tokens_counter_exists(self):
"""Test that CLASSIFIER_TOKENS counter is properly configured."""
from prometheus_client import Counter

from backend.services.metrics_service import CLASSIFIER_TOKENS

assert isinstance(CLASSIFIER_TOKENS, Counter)
assert CLASSIFIER_TOKENS._documentation == "Total number of input tokens processed by the classifier"
assert "Total" in CLASSIFIER_TOKENS._documentation
assert "model" in CLASSIFIER_TOKENS._labelnames

def test_classifier_tokens_labels(self):
from backend.services.metrics_service import CLASSIFIER_TOKENS
def test_metrics_can_be_observed(self):
"""Test that metrics can be observed without error."""
from backend.services.metrics_service import CLASSIFIER_LATENCY, CLASSIFIER_REQUESTS, CLASSIFIER_TOKENS

assert "model" in CLASSIFIER_TOKENS._labelnames
CLASSIFIER_LATENCY.labels(model="distilbert").observe(0.05)
CLASSIFIER_REQUESTS.labels(model="distilbert", status="ok").inc()
CLASSIFIER_TOKENS.labels(model="distilbert").inc(100)

def test_metrics_labels(self):
"""Test that metric labels are accessible."""
from backend.services.metrics_service import CLASSIFIER_LATENCY, CLASSIFIER_REQUESTS, CLASSIFIER_TOKENS

def test_latency_observe(self):
latency_labels = CLASSIFIER_LATENCY._labelnames
requests_labels = CLASSIFIER_REQUESTS._labelnames
tokens_labels = CLASSIFIER_TOKENS._labelnames

assert "model" in latency_labels
assert "model" in requests_labels
assert "status" in requests_labels
assert "model" in tokens_labels

def test_multiple_model_labels(self):
"""Test that multiple model labels can be used simultaneously."""
from backend.services.metrics_service import CLASSIFIER_LATENCY

CLASSIFIER_LATENCY.labels(model="distilbert").observe(0.05)
CLASSIFIER_LATENCY.labels(model="distilbert").observe(0.1)
CLASSIFIER_LATENCY.labels(model="bert").observe(0.08)

def test_requests_counter_increment(self):
def test_counter_increment(self):
"""Test that counters can be incremented without error."""
from backend.services.metrics_service import CLASSIFIER_REQUESTS

CLASSIFIER_REQUESTS.labels(model="distilbert", status="ok").inc()
CLASSIFIER_REQUESTS.labels(model="distilbert", status="ok").inc()
CLASSIFIER_REQUESTS.labels(model="distilbert", status="error").inc()
CLASSIFIER_REQUESTS.labels(model="test", status="ok").inc()

def test_tokens_counter_increment(self):
from backend.services.metrics_service import CLASSIFIER_TOKENS
def test_histogram_observe(self):
"""Test that histogram can record observations without error."""
from backend.services.metrics_service import CLASSIFIER_LATENCY

CLASSIFIER_TOKENS.labels(model="distilbert").inc(100)
CLASSIFIER_TOKENS.labels(model="distilbert").inc(50)
CLASSIFIER_LATENCY.labels(model="test").observe(0.1)
Loading