Skip to content

Personal AI, On Personal Devices

Python

fix(server): repair /metrics Prometheus endpoint (#803)

havolline11c0d3b2b66e1
diff --git a/src/openjarvis/server/api_routes.py b/src/openjarvis/server/api_routes.py
index be18b288..d84981ca 100644
--- a/src/openjarvis/server/api_routes.py
+++ b/src/openjarvis/server/api_routes.py
@@ -6,6 +6,7 @@ import asyncio
import inspect
import json
import logging
+from pathlib import Path
from typing import Any, Dict, List, Optional
from fastapi import APIRouter, HTTPException, Request, WebSocket, WebSocketDisconnect
@@ -409,16 +410,34 @@ async def get_trace(trace_id: str, request: Request):
telemetry_router = APIRouter(prefix="/v1/telemetry", tags=["telemetry"])
+def _telemetry_db_path(request: Request) -> Path:
+ """Resolve telemetry storage from the active app configuration.
+
+ ``DEFAULT_CONFIG_DIR`` is fixed when :mod:`openjarvis.core.config` is
+ imported, so it cannot honor a later ``OPENJARVIS_HOME`` override. The
+ running app's config is authoritative; lightweight apps that include these
+ routes directly fall back to the env-aware path resolver.
+ """
+ config = getattr(request.app.state, "config", None)
+ telemetry = getattr(config, "telemetry", None)
+ configured_path = getattr(telemetry, "db_path", None)
+ if configured_path:
+ return Path(configured_path).expanduser()
+
+ from openjarvis.core.paths import get_config_dir
+
+ return get_config_dir() / "telemetry.db"
+
+
@telemetry_router.get("/stats")
async def telemetry_stats(request: Request):
"""Get aggregated telemetry statistics."""
try:
from dataclasses import asdict
- from openjarvis.core.config import DEFAULT_CONFIG_DIR
from openjarvis.telemetry.aggregator import TelemetryAggregator
- db_path = DEFAULT_CONFIG_DIR / "telemetry.db"
+ db_path = _telemetry_db_path(request)
if not db_path.exists():
return {"total_requests": 0, "total_tokens": 0}
@@ -441,10 +460,9 @@ async def telemetry_stats(request: Request):
async def telemetry_energy(request: Request):
"""Get energy monitoring data."""
try:
- from openjarvis.core.config import DEFAULT_CONFIG_DIR
from openjarvis.telemetry.aggregator import TelemetryAggregator
- db_path = DEFAULT_CONFIG_DIR / "telemetry.db"
+ db_path = _telemetry_db_path(request)
if not db_path.exists():
return {
"total_energy_j": 0,
@@ -591,28 +609,36 @@ metrics_router = APIRouter(tags=["metrics"])
async def prometheus_metrics(request: Request):
"""Prometheus-compatible metrics endpoint."""
try:
- from openjarvis.core.config import DEFAULT_CONFIG_DIR
from openjarvis.telemetry.aggregator import TelemetryAggregator
- db_path = DEFAULT_CONFIG_DIR / "telemetry.db"
+ db_path = _telemetry_db_path(request)
if not db_path.exists():
from starlette.responses import PlainTextResponse
return PlainTextResponse("# no telemetry data\n", media_type="text/plain")
agg = TelemetryAggregator(db_path)
- stats = agg.summary()
+ try:
+ stats = agg.summary()
+ finally:
+ agg.close()
+
+ avg_latency_ms = (
+ (stats.total_latency / stats.total_calls) * 1000
+ if stats.total_calls
+ else 0.0
+ )
lines = [
"# HELP openjarvis_requests_total Total requests processed",
"# TYPE openjarvis_requests_total counter",
- f"openjarvis_requests_total {stats.get('total_requests', 0)}",
+ f"openjarvis_requests_total {stats.total_calls}",
"# HELP openjarvis_tokens_total Total tokens generated",
"# TYPE openjarvis_tokens_total counter",
- f"openjarvis_tokens_total {stats.get('total_tokens', 0)}",
+ f"openjarvis_tokens_total {stats.total_tokens}",
"# HELP openjarvis_latency_avg_ms Average latency in milliseconds",
"# TYPE openjarvis_latency_avg_ms gauge",
- f"openjarvis_latency_avg_ms {stats.get('avg_latency_ms', 0)}",
+ f"openjarvis_latency_avg_ms {avg_latency_ms}",
]
from starlette.responses import PlainTextResponse
diff --git a/tests/server/test_api_routes.py b/tests/server/test_api_routes.py
index 330e1b77..1a7d8ed8 100644
--- a/tests/server/test_api_routes.py
+++ b/tests/server/test_api_routes.py
@@ -1,5 +1,7 @@
"""Tests for extended API routes."""
+from types import SimpleNamespace
+
import pytest
fastapi = pytest.importorskip("fastapi")
@@ -127,9 +129,7 @@ class TestMetricsRoute:
machine's real telemetry database and makes any other response a
regression rather than accepting all possible production states.
"""
- import openjarvis.core.config as config_mod
-
- monkeypatch.setattr(config_mod, "DEFAULT_CONFIG_DIR", tmp_path)
+ monkeypatch.setenv("OPENJARVIS_HOME", str(tmp_path))
client = TestClient(_make_app())
resp = client.get("/metrics")
@@ -137,6 +137,97 @@ class TestMetricsRoute:
assert resp.text == "# no telemetry data\n"
assert resp.headers["content-type"].startswith("text/plain")
+ def test_metrics_endpoint_reports_real_stats(self, tmp_path, monkeypatch):
+ """Regression for #760: /metrics must report actual aggregated
+ stats instead of silently falling back to the placeholder text.
+
+ The handler called ``stats.get(...)`` on ``AggregatedStats``, a
+ dataclass with no ``.get()`` method -- every real request raised
+ ``AttributeError``, caught by the broad ``except Exception`` and
+ papered over as "No metrics available", even with real telemetry
+ data present in ``telemetry.db``.
+ """
+ import time
+
+ from openjarvis.core.types import TelemetryRecord
+ from openjarvis.telemetry.store import TelemetryStore
+
+ monkeypatch.setenv("OPENJARVIS_HOME", str(tmp_path))
+
+ db_path = tmp_path / "telemetry.db"
+ store = TelemetryStore(db_path)
+ store.record(
+ TelemetryRecord(
+ timestamp=time.time(),
+ model_id="test-model",
+ engine="ollama",
+ prompt_tokens=10,
+ completion_tokens=5,
+ total_tokens=15,
+ latency_seconds=2.0,
+ cost_usd=0.001,
+ )
+ )
+ store.close()
+
+ client = TestClient(_make_app())
+ resp = client.get("/metrics")
+ assert resp.status_code == 200
+ assert "No metrics available" not in resp.text
+ assert "openjarvis_requests_total 1" in resp.text
+ assert "openjarvis_tokens_total 15" in resp.text
+ assert "openjarvis_latency_avg_ms 2000" in resp.text
+
+ def test_metrics_endpoint_empty_db_no_division_by_zero(self, tmp_path, monkeypatch):
+ """An existing but empty telemetry.db (total_calls=0) must not
+ raise ZeroDivisionError when computing average latency."""
+ from openjarvis.telemetry.store import TelemetryStore
+
+ monkeypatch.setenv("OPENJARVIS_HOME", str(tmp_path))
+
+ db_path = tmp_path / "telemetry.db"
+ TelemetryStore(db_path).close() # creates the schema, no records
+
+ client = TestClient(_make_app())
+ resp = client.get("/metrics")
+ assert resp.status_code == 200
+ assert "No metrics available" not in resp.text
+ assert "openjarvis_requests_total 0" in resp.text
+ assert "openjarvis_latency_avg_ms 0" in resp.text
+
+ def test_metrics_endpoint_uses_active_app_telemetry_path(self, tmp_path):
+ import time
+
+ from openjarvis.core.types import TelemetryRecord
+ from openjarvis.telemetry.store import TelemetryStore
+
+ db_path = tmp_path / "runtime" / "custom-telemetry.sqlite"
+ store = TelemetryStore(db_path)
+ store.record(
+ TelemetryRecord(
+ timestamp=time.time(),
+ model_id="configured-model",
+ engine="ollama",
+ prompt_tokens=4,
+ completion_tokens=3,
+ total_tokens=7,
+ latency_seconds=0.5,
+ cost_usd=0.0,
+ )
+ )
+ store.close()
+
+ app = _make_app()
+ app.state.config = SimpleNamespace(
+ telemetry=SimpleNamespace(db_path=str(db_path))
+ )
+ resp = TestClient(app).get("/metrics")
+
+ assert resp.status_code == 200
+ assert "openjarvis_requests_total 1" in resp.text
+ assert "openjarvis_tokens_total 7" in resp.text
+ assert "openjarvis_latency_avg_ms 500" in resp.text
+
class TestSkillRoutes:
def test_list_skills(self):