Skip to content

Commit 6543af3

Browse files
committed
Add simple stats API
Signed-off-by: José Ulises Niño Rivera <[email protected]>
1 parent 89b48bd commit 6543af3

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
pub mod hostcalls;
16+
pub mod stats;
1617
pub mod traits;
1718
pub mod types;
1819

src/stats.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use crate::hostcalls;
16+
use crate::traits;
17+
use crate::types;
18+
19+
pub struct Counter {
20+
id: u32,
21+
name: String,
22+
}
23+
24+
impl Counter {
25+
pub fn counter(name: String) -> Counter {
26+
let returned_id = hostcalls::define_metric(types::MetricType::Counter, &name).unwrap();
27+
Counter {
28+
id: returned_id,
29+
name,
30+
}
31+
}
32+
}
33+
34+
impl traits::Metric for Counter {
35+
fn id(&self) -> u32 {
36+
self.id
37+
}
38+
39+
fn name(&self) -> &str {
40+
self.name.as_str()
41+
}
42+
43+
fn record(&self, _: u64) {
44+
// A Counter can only be incremented.
45+
return;
46+
}
47+
}
48+
49+
pub struct Gauge {
50+
id: u32,
51+
name: String,
52+
}
53+
54+
impl Gauge {
55+
pub fn gauge(name: String) -> Gauge {
56+
let returned_id = hostcalls::define_metric(types::MetricType::Gauge, &name).unwrap();
57+
Gauge {
58+
id: returned_id,
59+
name,
60+
}
61+
}
62+
}
63+
64+
impl traits::Metric for Gauge {
65+
fn id(&self) -> u32 {
66+
self.id
67+
}
68+
69+
fn name(&self) -> &str {
70+
self.name.as_str()
71+
}
72+
73+
fn increment(&self, _: i64) {
74+
// A gauge can only be recorded.
75+
return;
76+
}
77+
}
78+
79+
pub struct Histogram {
80+
id: u32,
81+
name: String,
82+
}
83+
84+
impl Histogram {
85+
pub fn histogram(name: String) -> Histogram {
86+
let returned_id = hostcalls::define_metric(types::MetricType::Histogram, &name).unwrap();
87+
Histogram {
88+
id: returned_id,
89+
name,
90+
}
91+
}
92+
}
93+
94+
impl traits::Metric for Histogram {
95+
fn id(&self) -> u32 {
96+
self.id
97+
}
98+
99+
fn name(&self) -> &str {
100+
self.name.as_str()
101+
}
102+
103+
fn increment(&self, _: i64) {
104+
// A Histogram can only be recorded.
105+
return;
106+
}
107+
}

src/traits.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,32 @@ pub trait HttpContext: Context {
534534

535535
fn on_log(&mut self) {}
536536
}
537+
538+
pub trait Metric {
539+
fn name(&self) -> &str;
540+
fn id(&self) -> u32;
541+
542+
fn value(&self) -> u64 {
543+
match hostcalls::get_metric(self.id()) {
544+
Ok(value) => value,
545+
Err(Status::NotFound) => panic!("metric not found: {}", self.name()),
546+
Err(err) => panic!("unexpected status: {:?}", err),
547+
}
548+
}
549+
550+
fn record(&self, value: u64) {
551+
match hostcalls::record_metric(self.id(), value) {
552+
Ok(_) => return,
553+
Err(Status::NotFound) => panic!("metric not found: {}", self.name()),
554+
Err(err) => panic!("unexpected status: {:?}", err),
555+
}
556+
}
557+
558+
fn increment(&self, offset: i64) {
559+
match hostcalls::increment_metric(self.id(), offset) {
560+
Ok(_) => return,
561+
Err(Status::NotFound) => panic!("metric not found: {}", self.name()),
562+
Err(err) => panic!("unexpected status: {:?}", err),
563+
}
564+
}
565+
}

0 commit comments

Comments
 (0)