Skip to content

Commit

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

Signed-off-by: Piotr Sikora <piotrsikora@google.com>
  • Loading branch information
PiotrSikora committed Aug 14, 2020
1 parent b5ec8dd commit 3f313f1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/hostcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 i64) -> Status;
}

pub fn get_metric(metric_id: u32) -> Result<i64, Status> {
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;
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 3f313f1

Please sign in to comment.