Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

pub mod hostcalls;
pub mod stats;
pub mod traits;
pub mod types;

Expand Down
107 changes: 107 additions & 0 deletions src/stats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::hostcalls;
use crate::traits;
use crate::types;

pub struct Counter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this abstraction 🚀

id: u32,
name: String,
}

impl Counter {
pub fn counter(name: String) -> Counter {
let returned_id = hostcalls::define_metric(types::MetricType::Counter, &name).unwrap();
Counter {
id: returned_id,
name,
}
}
}

impl traits::Metric for Counter {
fn id(&self) -> u32 {
self.id
}

fn name(&self) -> &str {
self.name.as_str()
}

fn record(&self, _: u64) {
// A Counter can only be incremented.
return;
}
}

pub struct Gauge {
id: u32,
name: String,
}

impl Gauge {
pub fn gauge(name: String) -> Gauge {
let returned_id = hostcalls::define_metric(types::MetricType::Gauge, &name).unwrap();
Gauge {
id: returned_id,
name,
}
}
}

impl traits::Metric for Gauge {
fn id(&self) -> u32 {
self.id
}

fn name(&self) -> &str {
self.name.as_str()
}

fn increment(&self, _: i64) {
// A gauge can only be recorded.
return;
}
}

pub struct Histogram {
id: u32,
name: String,
}

impl Histogram {
pub fn histogram(name: String) -> Histogram {
let returned_id = hostcalls::define_metric(types::MetricType::Histogram, &name).unwrap();
Histogram {
id: returned_id,
name,
}
}
}

impl traits::Metric for Histogram {
fn id(&self) -> u32 {
self.id
}

fn name(&self) -> &str {
self.name.as_str()
}

fn increment(&self, _: i64) {
// A Histogram can only be recorded.
return;
}
}
29 changes: 29 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,32 @@ pub trait HttpContext: Context {

fn on_log(&mut self) {}
}

pub trait Metric {
fn name(&self) -> &str;
fn id(&self) -> u32;

fn value(&self) -> u64 {
match hostcalls::get_metric(self.id()) {
Ok(value) => value,
Err(Status::NotFound) => panic!("metric not found: {}", self.name()),
Err(err) => panic!("unexpected status: {:?}", err),
}
}

fn record(&self, value: u64) {
match hostcalls::record_metric(self.id(), value) {
Ok(_) => return,
Err(Status::NotFound) => panic!("metric not found: {}", self.name()),
Err(err) => panic!("unexpected status: {:?}", err),
}
}

fn increment(&self, offset: i64) {
match hostcalls::increment_metric(self.id(), offset) {
Ok(_) => return,
Err(Status::NotFound) => panic!("metric not found: {}", self.name()),
Err(err) => panic!("unexpected status: {:?}", err),
}
}
}