tritonserver_rs/
metrics.rs

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
use std::ptr::null;

use bitflags::bitflags;

use crate::{sys, Error};

bitflags! {
    /// Metric format types.
    pub struct Format: u32 {
        /// Base points to a single multiline
        /// string that gives a text representation of the metrics in
        /// prometheus format.
        const PROMETHEUS = sys::tritonserver_metricformat_enum_TRITONSERVER_METRIC_PROMETHEUS;
    }
}

/// Server metrics object.
pub struct Metrics(pub(crate) *mut sys::TRITONSERVER_Metrics);

impl Metrics {
    /// Get a buffer containing the metrics in the specified format.
    pub fn formatted(&self, format: Format) -> Result<&[u8], Error> {
        let mut ptr = null::<i8>();
        let mut size: usize = 0;

        triton_call!(sys::TRITONSERVER_MetricsFormatted(
            self.0,
            format.bits(),
            &mut ptr as *mut _,
            &mut size as *mut _,
        ))?;

        assert!(!ptr.is_null());
        Ok(unsafe { std::slice::from_raw_parts(ptr as *const u8, size) })
    }
}

impl Drop for Metrics {
    fn drop(&mut self) {
        if !self.0.is_null() {
            unsafe {
                sys::TRITONSERVER_MetricsDelete(self.0);
            }
        }
    }
}