Skip to content

Commit

Permalink
Add metrics. (proxy-wasm#30)
Browse files Browse the repository at this point in the history
Fixes yskopets#4.

Signed-off-by: Piotr Sikora <piotrsikora@google.com>
  • Loading branch information
PiotrSikora authored Aug 15, 2020
1 parent a4d0e01 commit ba22ac0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
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>;

0 comments on commit ba22ac0

Please sign in to comment.