Skip to content

Commit

Permalink
refactor: make metrics related components dyn (#833)
Browse files Browse the repository at this point in the history
* refactor: make metrics related components dyn

Signed-off-by: MrCroxx <[email protected]>

* chore: pass clippy check

Signed-off-by: MrCroxx <[email protected]>

---------

Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx authored Jan 9, 2025
1 parent d5c08fd commit 204f84b
Show file tree
Hide file tree
Showing 13 changed files with 340 additions and 347 deletions.
2 changes: 1 addition & 1 deletion examples/export_metrics_prometheus_hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async fn main() {

// Build a cache with `PrometheusMetricsRegistry`.
let _: Cache<String, String> = CacheBuilder::new(100)
.with_metrics_registry(PrometheusMetricsRegistry::new(registry))
.with_metrics_registry(Box::new(PrometheusMetricsRegistry::new(registry)))
.build();

// > curl http://127.0.0.1:7890
Expand Down
2 changes: 1 addition & 1 deletion foyer-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ async fn benchmark(args: Args) {
let addr: SocketAddr = "0.0.0.0:19970".parse().unwrap();
PrometheusExporter::new(registry.clone(), addr).run();
builder
.with_metrics_registry(PrometheusMetricsRegistry::new(registry))
.with_metrics_registry(Box::new(PrometheusMetricsRegistry::new(registry)))
.memory(args.mem.as_u64() as _)
.with_shards(args.shards)
} else {
Expand Down
44 changes: 32 additions & 12 deletions foyer-common/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,55 +39,75 @@ pub trait HistogramOps: Send + Sync + 'static + Debug {
/// A vector of counters.
pub trait CounterVecOps: Send + Sync + 'static + Debug {
/// Get a counter within the vector of counters.
fn counter(&self, labels: &[Cow<'static, str>]) -> impl CounterOps;
fn counter(&self, labels: &[Cow<'static, str>]) -> BoxedCounter;
}

/// A vector of gauges.
pub trait GaugeVecOps: Send + Sync + 'static + Debug {
/// Get a gauge within the vector of gauges.
fn gauge(&self, labels: &[Cow<'static, str>]) -> impl GaugeOps;
fn gauge(&self, labels: &[Cow<'static, str>]) -> BoxedGauge;
}

/// A vector of histograms.
pub trait HistogramVecOps: Send + Sync + 'static + Debug {
/// Get a histogram within the vector of histograms.
fn histogram(&self, labels: &[Cow<'static, str>]) -> impl HistogramOps;
fn histogram(&self, labels: &[Cow<'static, str>]) -> BoxedHistogram;
}

/// Metrics registry.
pub trait RegistryOps: Send + Sync + 'static + Debug {
/// Register a vector of counters to the registry.
fn register_counter_vec(
&self,
name: impl Into<Cow<'static, str>>,
desc: impl Into<Cow<'static, str>>,
name: Cow<'static, str>,
desc: Cow<'static, str>,
label_names: &'static [&'static str],
) -> impl CounterVecOps;
) -> BoxedCounterVec;

/// Register a vector of gauges to the registry.
fn register_gauge_vec(
&self,
name: impl Into<Cow<'static, str>>,
desc: impl Into<Cow<'static, str>>,
name: Cow<'static, str>,
desc: Cow<'static, str>,
label_names: &'static [&'static str],
) -> impl GaugeVecOps;
) -> BoxedGaugeVec;

/// Register a vector of histograms to the registry.
fn register_histogram_vec(
&self,
name: impl Into<Cow<'static, str>>,
desc: impl Into<Cow<'static, str>>,
name: Cow<'static, str>,
desc: Cow<'static, str>,
label_names: &'static [&'static str],
) -> impl HistogramVecOps;
) -> BoxedHistogramVec;
}

trait Boxer {
fn boxed(self) -> Box<Self>
where
Self: Sized,
{
Box::new(self)
}
}
impl<T> Boxer for T {}

/// Boxed generic counter.
pub type BoxedCounter = Box<dyn CounterOps>;
/// Boxed generic gauge.
pub type BoxedGauge = Box<dyn GaugeOps>;
/// Boxed generic histogram.
pub type BoxedHistogram = Box<dyn HistogramOps>;

/// Boxed generic counter vec.
pub type BoxedCounterVec = Box<dyn CounterVecOps>;
/// Boxed generic gauge vec.
pub type BoxedGaugeVec = Box<dyn GaugeVecOps>;
/// Boxed generic histogram vec.
pub type BoxedHistogramVec = Box<dyn HistogramVecOps>;

/// Boxed generic registry.
pub type BoxedRegistry = Box<dyn RegistryOps>;

/// Shared metrics model.
pub mod model;
/// Provisioned metrics registries.
Expand Down
Loading

0 comments on commit 204f84b

Please sign in to comment.