-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
retain()
to Family to allow metrics filtering
Signed-off-by: John Howard <john.howard@solo.io>
- Loading branch information
1 parent
9a74e99
commit 72bc78d
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use prometheus_client::encoding::{text::encode, EncodeMetric, MetricEncoder}; | ||
use prometheus_client::metrics::counter::Atomic; | ||
use prometheus_client::metrics::family::Family; | ||
use prometheus_client::metrics::{MetricType, TypedMetric}; | ||
use prometheus_client::registry::Registry; | ||
use prometheus_client_derive_encode::EncodeLabelSet; | ||
use std::fmt::Error; | ||
use std::sync::atomic::AtomicU64; | ||
use std::sync::{Arc, Mutex}; | ||
use std::thread; | ||
use std::time::Duration; | ||
use tokio::time::Instant; | ||
|
||
#[derive(Default, Debug)] | ||
struct MyCounter { | ||
value: Arc<AtomicU64>, | ||
last_access: Arc<Mutex<Option<Instant>>>, | ||
} | ||
|
||
impl TypedMetric for MyCounter { | ||
const TYPE: MetricType = MetricType::Counter; | ||
} | ||
|
||
impl MyCounter { | ||
pub fn get(&self) -> u64 { | ||
self.value.get() | ||
} | ||
pub fn inc(&self) -> u64 { | ||
let mut last = self.last_access.lock().unwrap(); | ||
*last = Some(Instant::now()); | ||
self.value.inc() | ||
} | ||
} | ||
|
||
impl EncodeMetric for MyCounter { | ||
fn encode(&self, mut encoder: MetricEncoder) -> Result<(), Error> { | ||
encoder.encode_counter::<prometheus_client::encoding::NoLabelSet, _, u64>(&self.get(), None) | ||
} | ||
|
||
fn metric_type(&self) -> MetricType { | ||
todo!() | ||
} | ||
} | ||
|
||
#[derive(Clone, Hash, Default, Debug, PartialEq, Eq, EncodeLabelSet)] | ||
struct Labels { | ||
name: String, | ||
} | ||
|
||
fn main() { | ||
let mut registry = Registry::default(); | ||
|
||
let metric: Family<Labels, MyCounter> = Family::default(); | ||
registry.register("my_custom_metric", "test", metric.clone()); | ||
metric | ||
.get_or_create(&Labels { | ||
name: "apple".to_string(), | ||
}) | ||
.inc(); | ||
metric | ||
.get_or_create(&Labels { | ||
name: "banana".to_string(), | ||
}) | ||
.inc(); | ||
|
||
let mut encoded = String::new(); | ||
encode(&mut encoded, ®istry).unwrap(); | ||
|
||
println!("Scrape output:\n{}", encoded); | ||
thread::sleep(Duration::from_secs(1)); | ||
metric | ||
.get_or_create(&Labels { | ||
name: "banana".to_string(), | ||
}) | ||
.inc(); | ||
let now = Instant::now(); | ||
metric.retain(|_labels, counter| { | ||
let last = counter.last_access.lock().unwrap().unwrap(); | ||
now.saturating_duration_since(last) > Duration::from_secs(1) | ||
}); | ||
|
||
let mut encoded = String::new(); | ||
encode(&mut encoded, ®istry).unwrap(); | ||
|
||
println!("Scrape output:\n{}", encoded); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters