diff --git a/src/hostcalls.rs b/src/hostcalls.rs index 1d7509d7..076285fc 100644 --- a/src/hostcalls.rs +++ b/src/hostcalls.rs @@ -678,6 +678,66 @@ pub fn done() -> Result<(), Status> { } } +extern "C" { + fn proxy_define_metric( + metric_type: MetricType, + name_data: *const u8, + name_size: usize, + return_id: *mut u32, + ) -> Status; +} + +pub fn define_metric(metric_type: MetricType, name: &str) -> Result { + let mut return_id: u32 = 0; + unsafe { + match proxy_define_metric(metric_type, name.as_ptr(), name.len(), &mut return_id) { + Status::Ok => Ok(return_id), + status => panic!("unexpected status: {}", status as u32), + } + } +} + +extern "C" { + fn proxy_get_metric(metric_id: u32, return_value: *mut i64) -> Status; +} + +pub fn get_metric(metric_id: u32) -> Result { + let mut return_value: i64 = 0; + unsafe { + match proxy_get_metric(metric_id, &mut return_value) { + Status::Ok => Ok(return_value), + Status::BadArgument => Err(Status::BadArgument), + status => panic!("unexpected status: {}", status as u32), + } + } +} + +extern "C" { + fn proxy_record_metric(metric_id: u32, value: i64) -> Status; +} + +pub fn record_metric(metric_id: u32, value: i64) -> Result<(), Status> { + unsafe { + match proxy_record_metric(metric_id, value) { + Status::Ok => Ok(()), + status => panic!("unexpected status: {}", status as u32), + } + } +} + +extern "C" { + fn proxy_increment_metric(metric_id: u32, offset: i64) -> Status; +} + +pub fn increment_metric(metric_id: u32, offset: i64) -> Result<(), Status> { + unsafe { + match proxy_increment_metric(metric_id, offset) { + Status::Ok => Ok(()), + status => panic!("unexpected status: {}", status as u32), + } + } +} + mod utils { use crate::types::Bytes; use std::convert::TryFrom; diff --git a/src/types.rs b/src/types.rs index ba9749c3..a951f78f 100644 --- a/src/types.rs +++ b/src/types.rs @@ -76,4 +76,12 @@ pub enum PeerType { Remote = 2, } +#[repr(u32)] +#[derive(Debug)] +pub enum MetricType { + Counter = 0, + Gauge = 1, + Histogram = 2, +} + pub type Bytes = Vec;