1use async_trait::async_trait;
2use std::sync::Arc;
3
4use crate::error::IndexerResult;
5
6#[async_trait]
7pub trait Metrics: Send + Sync {
12 async fn initialize(&self) -> IndexerResult<()>;
13
14 async fn flush(&self) -> IndexerResult<()>;
15
16 async fn shutdown(&self) -> IndexerResult<()>;
17
18 async fn update_gauge(&self, name: &str, value: f64) -> IndexerResult<()>;
20
21 async fn increment_counter(&self, name: &str, value: u64) -> IndexerResult<()>;
23
24 async fn record_histogram(&self, name: &str, value: f64) -> IndexerResult<()>;
26}
27
28#[derive(Default)]
29pub struct MetricsCollection {
30 pub metrics: Vec<Arc<dyn Metrics>>,
31}
32
33impl MetricsCollection {
34 pub fn new(metrics: Vec<Arc<dyn Metrics>>) -> Self {
35 Self { metrics }
36 }
37
38 pub async fn initialize_metrics(&self) -> IndexerResult<()> {
39 for metric in &self.metrics {
40 metric.initialize().await?;
41 }
42 Ok(())
43 }
44
45 pub async fn shutdown_metrics(&self) -> IndexerResult<()> {
46 for metric in &self.metrics {
47 metric.shutdown().await?;
48 }
49 Ok(())
50 }
51
52 pub async fn flush_metrics(&self) -> IndexerResult<()> {
53 for metric in &self.metrics {
54 metric.flush().await?;
55 }
56 Ok(())
57 }
58
59 pub async fn update_gauge(&self, name: &str, value: f64) -> IndexerResult<()> {
60 for metric in &self.metrics {
61 metric.update_gauge(name, value).await?;
62 }
63 Ok(())
64 }
65
66 pub async fn increment_counter(&self, name: &str, value: u64) -> IndexerResult<()> {
67 for metric in &self.metrics {
68 metric.increment_counter(name, value).await?;
69 }
70 Ok(())
71 }
72
73 pub async fn record_histogram(&self, name: &str, value: f64) -> IndexerResult<()> {
74 for metric in &self.metrics {
75 metric.record_histogram(name, value).await?;
76 }
77 Ok(())
78 }
79}