oxibonsai-serve 0.1.4

Standalone OpenAI-compatible inference server for OxiBonsai
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Hand-rolled Prometheus text exposition.
//!
//! Implements just enough of the 0.0.4 spec to emit counters, histograms and
//! gauges without pulling in the `prometheus` crate (which pulls in a
//! thread-local protobuf runtime we don't need).
//!
//! # Model
//!
//! A [`MetricsRegistry`] owns three kinds of metric families:
//!
//! - **counters** — monotonic `u64` counters, one per `(name, labels)` tuple.
//!   Rendered with a `_total` suffix.
//! - **histograms** — observation-based; each family has a fixed set of
//!   cumulative buckets (in seconds, by convention).  Rendered as
//!   `name_bucket{le=...}`, `name_sum` and `name_count`.
//! - **gauges** — last-write-wins `i64`.
//!
//! Metrics are keyed by a `(name, labels)` tuple.  Labels are
//! lexicographically sorted on insert to guarantee stable rendering.
//!
//! # Example
//!
//! ```
//! use oxibonsai_serve::metrics::MetricsRegistry;
//!
//! let reg = MetricsRegistry::new();
//! reg.inc_counter("oxibonsai_requests_total", &[("endpoint", "/health"), ("status", "200")]);
//! reg.observe_histogram(
//!     "oxibonsai_request_duration_seconds",
//!     &[("endpoint", "/health")],
//!     0.012,
//! );
//! reg.set_gauge("oxibonsai_inflight_requests", &[], 3);
//! let body = reg.render();
//! assert!(body.contains("oxibonsai_requests_total"));
//! assert!(body.contains("oxibonsai_request_duration_seconds_bucket"));
//! ```

use std::collections::BTreeMap;
use std::sync::RwLock;

// ─── Constants ────────────────────────────────────────────────────────────

/// Default histogram buckets (in seconds).
pub const DEFAULT_HISTOGRAM_BUCKETS: &[f64] = &[0.01, 0.05, 0.1, 0.5, 1.0, 5.0, 10.0, 30.0];

// ─── Key type ─────────────────────────────────────────────────────────────

/// A metric key: `(name, sorted labels)`.
type LabelPairs = Vec<(String, String)>;
type Key = (String, LabelPairs);

fn make_key(name: &str, labels: &[(&str, &str)]) -> Key {
    let mut labels: LabelPairs = labels
        .iter()
        .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
        .collect();
    labels.sort_by(|a, b| a.0.cmp(&b.0));
    (name.to_string(), labels)
}

fn format_labels(labels: &[(String, String)]) -> String {
    if labels.is_empty() {
        return String::new();
    }
    let inside = labels
        .iter()
        .map(|(k, v)| format!("{}=\"{}\"", k, escape_label_value(v)))
        .collect::<Vec<_>>()
        .join(",");
    format!("{{{inside}}}")
}

/// Escape backslash, double-quote and newline in a label value per the
/// Prometheus text-format spec.
fn escape_label_value(value: &str) -> String {
    let mut out = String::with_capacity(value.len());
    for ch in value.chars() {
        match ch {
            '\\' => out.push_str("\\\\"),
            '"' => out.push_str("\\\""),
            '\n' => out.push_str("\\n"),
            other => out.push(other),
        }
    }
    out
}

// ─── Histogram value ──────────────────────────────────────────────────────

/// Internal state of a single histogram time series.
#[derive(Debug, Clone)]
struct HistogramValue {
    buckets: Vec<f64>,
    counts: Vec<u64>,
    sum: f64,
    count: u64,
}

impl HistogramValue {
    fn new(buckets: &[f64]) -> Self {
        let mut b = buckets.to_vec();
        // Ensure sorted ascending and that each bucket is finite.
        b.retain(|x| x.is_finite());
        b.sort_by(|a, c| a.partial_cmp(c).unwrap_or(std::cmp::Ordering::Equal));
        let counts = vec![0u64; b.len()];
        Self {
            buckets: b,
            counts,
            sum: 0.0,
            count: 0,
        }
    }

    fn observe(&mut self, value: f64) {
        if !value.is_finite() {
            return;
        }
        self.sum += value;
        self.count = self.count.saturating_add(1);
        for (i, b) in self.buckets.iter().enumerate() {
            if value <= *b {
                self.counts[i] = self.counts[i].saturating_add(1);
            }
        }
    }
}

// ─── Registry ─────────────────────────────────────────────────────────────

/// Inner mutable state guarded by a single [`RwLock`].
#[derive(Debug, Default)]
struct Inner {
    counters: BTreeMap<Key, u64>,
    gauges: BTreeMap<Key, i64>,
    histograms: BTreeMap<Key, HistogramValue>,
    /// Default buckets used when a histogram is first observed.
    default_buckets: Vec<f64>,
}

/// Thread-safe Prometheus metrics registry.
#[derive(Debug)]
pub struct MetricsRegistry {
    inner: RwLock<Inner>,
}

impl Default for MetricsRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl MetricsRegistry {
    /// Build a new empty registry using [`DEFAULT_HISTOGRAM_BUCKETS`].
    pub fn new() -> Self {
        Self {
            inner: RwLock::new(Inner {
                counters: BTreeMap::new(),
                gauges: BTreeMap::new(),
                histograms: BTreeMap::new(),
                default_buckets: DEFAULT_HISTOGRAM_BUCKETS.to_vec(),
            }),
        }
    }

    /// Build a registry with a custom default bucket set.
    pub fn with_buckets(buckets: &[f64]) -> Self {
        Self {
            inner: RwLock::new(Inner {
                counters: BTreeMap::new(),
                gauges: BTreeMap::new(),
                histograms: BTreeMap::new(),
                default_buckets: buckets.to_vec(),
            }),
        }
    }

    /// Increment a counter by 1.
    pub fn inc_counter(&self, name: &str, labels: &[(&str, &str)]) {
        self.add_counter(name, labels, 1);
    }

    /// Add `value` to a counter.
    pub fn add_counter(&self, name: &str, labels: &[(&str, &str)], value: u64) {
        let key = make_key(name, labels);
        let mut guard = match self.inner.write() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        let entry = guard.counters.entry(key).or_insert(0);
        *entry = entry.saturating_add(value);
    }

    /// Set a gauge to `value`.
    pub fn set_gauge(&self, name: &str, labels: &[(&str, &str)], value: i64) {
        let key = make_key(name, labels);
        let mut guard = match self.inner.write() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.gauges.insert(key, value);
    }

    /// Add `delta` to a gauge (may be negative).
    pub fn add_gauge(&self, name: &str, labels: &[(&str, &str)], delta: i64) {
        let key = make_key(name, labels);
        let mut guard = match self.inner.write() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        let entry = guard.gauges.entry(key).or_insert(0);
        *entry = entry.saturating_add(delta);
    }

    /// Observe a histogram value.  The histogram is created lazily with the
    /// registry's default buckets on first observation.
    pub fn observe_histogram(&self, name: &str, labels: &[(&str, &str)], value: f64) {
        let key = make_key(name, labels);
        let mut guard = match self.inner.write() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        let buckets = guard.default_buckets.clone();
        let entry = guard
            .histograms
            .entry(key)
            .or_insert_with(|| HistogramValue::new(&buckets));
        entry.observe(value);
    }

    /// Get a counter value (0 if absent).
    pub fn counter_value(&self, name: &str, labels: &[(&str, &str)]) -> u64 {
        let key = make_key(name, labels);
        let guard = match self.inner.read() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.counters.get(&key).copied().unwrap_or(0)
    }

    /// Get a gauge value (0 if absent).
    pub fn gauge_value(&self, name: &str, labels: &[(&str, &str)]) -> i64 {
        let key = make_key(name, labels);
        let guard = match self.inner.read() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.gauges.get(&key).copied().unwrap_or(0)
    }

    /// Get histogram count (number of observations).  Returns 0 if the
    /// histogram has not been observed yet.
    pub fn histogram_count(&self, name: &str, labels: &[(&str, &str)]) -> u64 {
        let key = make_key(name, labels);
        let guard = match self.inner.read() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard.histograms.get(&key).map(|h| h.count).unwrap_or(0)
    }

    /// Get the per-bucket counts for a histogram, in the same order as the
    /// bucket vector supplied at creation.  Returns an empty vector if the
    /// histogram does not exist.
    pub fn histogram_bucket_counts(&self, name: &str, labels: &[(&str, &str)]) -> Vec<u64> {
        let key = make_key(name, labels);
        let guard = match self.inner.read() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };
        guard
            .histograms
            .get(&key)
            .map(|h| h.counts.clone())
            .unwrap_or_default()
    }

    /// Render all metrics as a Prometheus 0.0.4 text-format string.
    pub fn render(&self) -> String {
        let guard = match self.inner.read() {
            Ok(g) => g,
            Err(poisoned) => poisoned.into_inner(),
        };

        let mut out = String::new();

        // Group by name so we emit HELP/TYPE once per family.
        let mut counter_names: BTreeMap<String, Vec<(&LabelPairs, &u64)>> = BTreeMap::new();
        for ((n, l), v) in &guard.counters {
            counter_names.entry(n.clone()).or_default().push((l, v));
        }
        for (name, series) in &counter_names {
            out.push_str(&format!(
                "# HELP {name} Counter auto-registered by oxibonsai-serve.\n"
            ));
            out.push_str(&format!("# TYPE {name} counter\n"));
            for (labels, value) in series {
                out.push_str(&format!("{}{} {}\n", name, format_labels(labels), value));
            }
        }

        let mut gauge_names: BTreeMap<String, Vec<(&LabelPairs, &i64)>> = BTreeMap::new();
        for ((n, l), v) in &guard.gauges {
            gauge_names.entry(n.clone()).or_default().push((l, v));
        }
        for (name, series) in &gauge_names {
            out.push_str(&format!(
                "# HELP {name} Gauge auto-registered by oxibonsai-serve.\n"
            ));
            out.push_str(&format!("# TYPE {name} gauge\n"));
            for (labels, value) in series {
                out.push_str(&format!("{}{} {}\n", name, format_labels(labels), value));
            }
        }

        let mut hist_names: BTreeMap<String, Vec<(&LabelPairs, &HistogramValue)>> = BTreeMap::new();
        for ((n, l), v) in &guard.histograms {
            hist_names.entry(n.clone()).or_default().push((l, v));
        }
        for (name, series) in &hist_names {
            out.push_str(&format!(
                "# HELP {name} Histogram auto-registered by oxibonsai-serve.\n"
            ));
            out.push_str(&format!("# TYPE {name} histogram\n"));
            for (labels, hv) in series {
                // Bucket lines.
                for (i, b) in hv.buckets.iter().enumerate() {
                    let mut ls = (*labels).clone();
                    ls.push(("le".to_string(), format_float(*b)));
                    ls.sort_by(|a, c| a.0.cmp(&c.0));
                    out.push_str(&format!(
                        "{}_bucket{} {}\n",
                        name,
                        format_labels(&ls),
                        hv.counts[i]
                    ));
                }
                // +Inf bucket.
                let mut inf_labels = (*labels).clone();
                inf_labels.push(("le".to_string(), "+Inf".to_string()));
                inf_labels.sort_by(|a, c| a.0.cmp(&c.0));
                out.push_str(&format!(
                    "{}_bucket{} {}\n",
                    name,
                    format_labels(&inf_labels),
                    hv.count
                ));
                // _sum
                out.push_str(&format!(
                    "{}_sum{} {}\n",
                    name,
                    format_labels(labels),
                    format_float(hv.sum)
                ));
                // _count
                out.push_str(&format!(
                    "{}_count{} {}\n",
                    name,
                    format_labels(labels),
                    hv.count
                ));
            }
        }

        out
    }
}

/// Format a floating-point value for Prometheus output.
///
/// Integer-valued floats are printed without a trailing `.0` to match what the
/// official Prometheus client libraries emit (e.g. `1`, not `1.0`).
fn format_float(v: f64) -> String {
    if v.is_nan() {
        return "NaN".to_string();
    }
    if v.is_infinite() {
        return if v > 0.0 {
            "+Inf".to_string()
        } else {
            "-Inf".to_string()
        };
    }
    if v.fract() == 0.0 && v.abs() < 1.0e15 {
        format!("{}", v as i64)
    } else {
        format!("{v}")
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn counter_roundtrip() {
        let r = MetricsRegistry::new();
        r.inc_counter("foo_total", &[("a", "x")]);
        r.inc_counter("foo_total", &[("a", "x")]);
        assert_eq!(r.counter_value("foo_total", &[("a", "x")]), 2);
    }

    #[test]
    fn gauge_set() {
        let r = MetricsRegistry::new();
        r.set_gauge("inflight", &[], 3);
        assert_eq!(r.gauge_value("inflight", &[]), 3);
        r.set_gauge("inflight", &[], 1);
        assert_eq!(r.gauge_value("inflight", &[]), 1);
    }

    #[test]
    fn histogram_observe_count() {
        let r = MetricsRegistry::new();
        r.observe_histogram("lat", &[("endpoint", "/h")], 0.005);
        r.observe_histogram("lat", &[("endpoint", "/h")], 0.20);
        assert_eq!(r.histogram_count("lat", &[("endpoint", "/h")]), 2);
    }

    #[test]
    fn render_includes_type_and_help() {
        let r = MetricsRegistry::new();
        r.inc_counter("x_total", &[]);
        let body = r.render();
        assert!(body.contains("# HELP x_total"));
        assert!(body.contains("# TYPE x_total counter"));
        assert!(body.contains("x_total 1"));
    }

    #[test]
    fn render_histogram_has_buckets_sum_count() {
        let r = MetricsRegistry::new();
        r.observe_histogram("lat_seconds", &[], 0.02);
        let body = r.render();
        assert!(body.contains("lat_seconds_bucket{le=\"0.01\"}"));
        assert!(body.contains("lat_seconds_bucket{le=\"+Inf\"}"));
        assert!(body.contains("lat_seconds_sum"));
        assert!(body.contains("lat_seconds_count"));
    }

    #[test]
    fn label_escape() {
        assert_eq!(escape_label_value("a\"b"), "a\\\"b");
        assert_eq!(escape_label_value("a\\b"), "a\\\\b");
        assert_eq!(escape_label_value("a\nb"), "a\\nb");
    }
}