Skip to content

Commit e7a6ad0

Browse files
authored
Add metrics. (#30)
Fixes #4. Signed-off-by: Piotr Sikora <piotrsikora@google.com>
1 parent 6f32085 commit e7a6ad0

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

src/hostcalls.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,70 @@ pub fn done() -> Result<(), Status> {
678678
}
679679
}
680680

681+
extern "C" {
682+
fn proxy_define_metric(
683+
metric_type: MetricType,
684+
name_data: *const u8,
685+
name_size: usize,
686+
return_id: *mut u32,
687+
) -> Status;
688+
}
689+
690+
pub fn define_metric(metric_type: MetricType, name: &str) -> Result<u32, Status> {
691+
let mut return_id: u32 = 0;
692+
unsafe {
693+
match proxy_define_metric(metric_type, name.as_ptr(), name.len(), &mut return_id) {
694+
Status::Ok => Ok(return_id),
695+
status => panic!("unexpected status: {}", status as u32),
696+
}
697+
}
698+
}
699+
700+
extern "C" {
701+
fn proxy_get_metric(metric_id: u32, return_value: *mut u64) -> Status;
702+
}
703+
704+
pub fn get_metric(metric_id: u32) -> Result<u64, Status> {
705+
let mut return_value: u64 = 0;
706+
unsafe {
707+
match proxy_get_metric(metric_id, &mut return_value) {
708+
Status::Ok => Ok(return_value),
709+
Status::NotFound => Err(Status::NotFound),
710+
Status::BadArgument => Err(Status::BadArgument),
711+
status => panic!("unexpected status: {}", status as u32),
712+
}
713+
}
714+
}
715+
716+
extern "C" {
717+
fn proxy_record_metric(metric_id: u32, value: u64) -> Status;
718+
}
719+
720+
pub fn record_metric(metric_id: u32, value: u64) -> Result<(), Status> {
721+
unsafe {
722+
match proxy_record_metric(metric_id, value) {
723+
Status::Ok => Ok(()),
724+
Status::NotFound => Err(Status::NotFound),
725+
status => panic!("unexpected status: {}", status as u32),
726+
}
727+
}
728+
}
729+
730+
extern "C" {
731+
fn proxy_increment_metric(metric_id: u32, offset: i64) -> Status;
732+
}
733+
734+
pub fn increment_metric(metric_id: u32, offset: i64) -> Result<(), Status> {
735+
unsafe {
736+
match proxy_increment_metric(metric_id, offset) {
737+
Status::Ok => Ok(()),
738+
Status::NotFound => Err(Status::NotFound),
739+
Status::BadArgument => Err(Status::BadArgument),
740+
status => panic!("unexpected status: {}", status as u32),
741+
}
742+
}
743+
}
744+
681745
mod utils {
682746
use crate::types::Bytes;
683747
use std::convert::TryFrom;

src/types.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,12 @@ pub enum PeerType {
7676
Remote = 2,
7777
}
7878

79+
#[repr(u32)]
80+
#[derive(Debug)]
81+
pub enum MetricType {
82+
Counter = 0,
83+
Gauge = 1,
84+
Histogram = 2,
85+
}
86+
7987
pub type Bytes = Vec<u8>;

0 commit comments

Comments
 (0)