|
| 1 | +use prometheus_client::encoding::{text::encode, EncodeMetric, MetricEncoder}; |
| 2 | +use prometheus_client::metrics::counter::Atomic; |
| 3 | +use prometheus_client::metrics::family::Family; |
| 4 | +use prometheus_client::metrics::{MetricType, TypedMetric}; |
| 5 | +use prometheus_client::registry::Registry; |
| 6 | +use prometheus_client_derive_encode::EncodeLabelSet; |
| 7 | +use std::fmt::Error; |
| 8 | +use std::sync::atomic::AtomicU64; |
| 9 | +use std::sync::{Arc, Mutex}; |
| 10 | +use std::thread; |
| 11 | +use std::time::Duration; |
| 12 | +use tokio::time::Instant; |
| 13 | + |
| 14 | +#[derive(Default, Debug)] |
| 15 | +struct MyCounter { |
| 16 | + value: Arc<AtomicU64>, |
| 17 | + last_access: Arc<Mutex<Option<Instant>>>, |
| 18 | +} |
| 19 | + |
| 20 | +impl TypedMetric for MyCounter {} |
| 21 | + |
| 22 | +impl MyCounter { |
| 23 | + pub fn get(&self) -> u64 { |
| 24 | + self.value.get() |
| 25 | + } |
| 26 | + pub fn inc(&self) -> u64 { |
| 27 | + let mut last = self.last_access.lock().unwrap(); |
| 28 | + *last = Some(Instant::now()); |
| 29 | + self.value.inc() |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl EncodeMetric for MyCounter { |
| 34 | + fn encode(&self, mut encoder: MetricEncoder) -> Result<(), Error> { |
| 35 | + encoder.encode_counter::<(), _, u64>(&self.get(), None) |
| 36 | + } |
| 37 | + |
| 38 | + fn metric_type(&self) -> MetricType { |
| 39 | + todo!() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Clone, Hash, Default, Debug, PartialEq, Eq, EncodeLabelSet)] |
| 44 | +struct Labels { |
| 45 | + name: String, |
| 46 | +} |
| 47 | + |
| 48 | +fn main() { |
| 49 | + let mut registry = Registry::default(); |
| 50 | + |
| 51 | + let metric: Family<Labels, MyCounter> = Family::default(); |
| 52 | + registry.register("my_custom_metric", "test", metric.clone()); |
| 53 | + metric |
| 54 | + .get_or_create(&Labels { |
| 55 | + name: "apple".to_string(), |
| 56 | + }) |
| 57 | + .inc(); |
| 58 | + metric |
| 59 | + .get_or_create(&Labels { |
| 60 | + name: "banana".to_string(), |
| 61 | + }) |
| 62 | + .inc(); |
| 63 | + |
| 64 | + let mut encoded = String::new(); |
| 65 | + encode(&mut encoded, ®istry).unwrap(); |
| 66 | + |
| 67 | + println!("Scrape output:\n{}", encoded); |
| 68 | + thread::sleep(Duration::from_secs(1)); |
| 69 | + metric |
| 70 | + .get_or_create(&Labels { |
| 71 | + name: "banana".to_string(), |
| 72 | + }) |
| 73 | + .inc(); |
| 74 | + let now = Instant::now(); |
| 75 | + metric.retain(|a, b| { |
| 76 | + let last = b.last_access.lock().unwrap().unwrap(); |
| 77 | + now.saturating_duration_since(last) > Duration::from_secs(1) |
| 78 | + }); |
| 79 | + |
| 80 | + let mut encoded = String::new(); |
| 81 | + encode(&mut encoded, ®istry).unwrap(); |
| 82 | + |
| 83 | + println!("Scrape output:\n{}", encoded); |
| 84 | +} |
0 commit comments