Skip to content

Commit 40edf83

Browse files
Add Prometheus metrics endpoint and instrumentation (#21)
* Add Prometheus metrics endpoint and request instrumentation Expose operational metrics at GET /metrics (unauthenticated) using the metrics + metrics-exporter-prometheus crates. Instruments HTTP request counts/durations, active/streaming connections, cache hits/misses, signing durations, upstream latency, and error counters across all proxy modules. Existing GET /v1/metrics (backend passthrough) is unchanged. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * Address Copilot review feedback on metrics PR - cache.rs: Add comment that moka entry_count() is O(1) - signing.rs: Split signature duration histogram by algorithm for per-algo performance visibility - metrics_middleware.rs: Improve panic message in setup_metrics_recorder to help operators debug recorder conflicts - error.rs: Reorder upstream error branch for clarity (comment before counter, no functional change) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0fbbcf6 commit 40edf83

12 files changed

Lines changed: 368 additions & 9 deletions

File tree

Cargo.lock

Lines changed: 190 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ thiserror = "2"
4343
anyhow = "1"
4444
tracing = "0.1"
4545
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
46+
metrics = "0.24"
47+
metrics-exporter-prometheus = "0.16"
4648

4749
[dev-dependencies]
4850
wiremock = "0.6"

src/cache.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,19 @@ impl ChatCache {
2828
pub fn set_chat(&self, chat_id: &str, value: &str) {
2929
let key = self.make_key(chat_id);
3030
self.inner.insert(key, value.to_string());
31+
// moka::sync::Cache::entry_count() is O(1) (atomic counter), safe to call per insert
32+
metrics::gauge!("cache_size").set(self.inner.entry_count() as f64);
3133
}
3234

3335
pub fn get_chat(&self, chat_id: &str) -> Option<String> {
3436
let key = self.make_key(chat_id);
35-
self.inner.get(&key)
37+
let result = self.inner.get(&key);
38+
if result.is_some() {
39+
metrics::counter!("cache_hits_total").increment(1);
40+
} else {
41+
metrics::counter!("cache_misses_total").increment(1);
42+
}
43+
result
3644
}
3745
}
3846

0 commit comments

Comments
 (0)