tritonserver_rs/
metrics.rs

1use std::ptr::null;
2
3use crate::{sys, Error};
4
5#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
6#[repr(u32)]
7/// Metric format types.
8pub enum Format {
9    /// Base points to a single multiline
10    /// string that gives a text representation of the metrics in
11    /// prometheus format.
12    Prometheus = sys::tritonserver_metricformat_enum_TRITONSERVER_METRIC_PROMETHEUS,
13}
14
15/// Server metrics object.
16pub struct Metrics(pub(crate) *mut sys::TRITONSERVER_Metrics);
17
18impl Metrics {
19    /// Get a buffer containing the metrics in the specified format.
20    pub fn formatted(&self, format: Format) -> Result<&[u8], Error> {
21        let mut ptr = null::<i8>();
22        let mut size: usize = 0;
23
24        triton_call!(sys::TRITONSERVER_MetricsFormatted(
25            self.0,
26            format as _,
27            &mut ptr as *mut _,
28            &mut size as *mut _,
29        ))?;
30
31        assert!(!ptr.is_null());
32        Ok(unsafe { std::slice::from_raw_parts(ptr as *const u8, size) })
33    }
34}
35
36impl Drop for Metrics {
37    fn drop(&mut self) {
38        if !self.0.is_null() {
39            unsafe {
40                sys::TRITONSERVER_MetricsDelete(self.0);
41            }
42        }
43    }
44}