Skip to content

Add metrics. #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,70 @@ 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<u32, Status> {
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 u64) -> Status;
}

pub fn get_metric(metric_id: u32) -> Result<u64, Status> {
let mut return_value: u64 = 0;
unsafe {
match proxy_get_metric(metric_id, &mut return_value) {
Status::Ok => Ok(return_value),
Status::NotFound => Err(Status::NotFound),
Status::BadArgument => Err(Status::BadArgument),
status => panic!("unexpected status: {}", status as u32),
}
}
}

extern "C" {
fn proxy_record_metric(metric_id: u32, value: u64) -> Status;
}

pub fn record_metric(metric_id: u32, value: u64) -> Result<(), Status> {
unsafe {
match proxy_record_metric(metric_id, value) {
Status::Ok => Ok(()),
Status::NotFound => Err(Status::NotFound),
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::NotFound => Err(Status::NotFound),
Status::BadArgument => Err(Status::BadArgument),
status => panic!("unexpected status: {}", status as u32),
}
}
}

mod utils {
use crate::types::Bytes;
use std::convert::TryFrom;
Expand Down
8 changes: 8 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>;