feat(metrics): prometheus /metrics + generation timing in usage

- /metrics endpoint: request/token/latency counters, tok/s gauge, build info (std-only, no deps)
- usage.duration_ms + usage.tokens_per_second in non-streaming and streaming responses
- /health now reports uptime_s, n_ctx, version
- MAX_TOKENS env config (hard cap, default 2048; 0 = unlimited)
- metrics unit tests (counters, prometheus shape, uptime monotonic)
This commit is contained in:
asepharyana
2026-08-03 11:44:21 +07:00
parent 7cec411cba
commit e8f90fc9b1
8 changed files with 891 additions and 102 deletions
+15
View File
@@ -109,6 +109,12 @@ pub struct Usage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
/// Wall-clock duration of the generation, in milliseconds.
#[serde(skip_serializing_if = "Option::is_none")]
pub duration_ms: Option<u64>,
/// Generated tokens per second (completion_tokens / seconds).
#[serde(skip_serializing_if = "Option::is_none")]
pub tokens_per_second: Option<f64>,
}
// ═══════════════════════════════════════════════════════════════
@@ -278,4 +284,13 @@ pub struct ModelInfo {
pub struct HealthResponse {
pub status: String,
pub model: String,
/// Server process uptime in seconds.
#[serde(skip_serializing_if = "Option::is_none")]
pub uptime_s: Option<u64>,
/// llama.cpp context size (n_ctx).
#[serde(skip_serializing_if = "Option::is_none")]
pub n_ctx: Option<u32>,
/// Server binary version (from CARGO_PKG_VERSION).
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
}