Skip to content

Commit

Permalink
feat: support prometheus-client metrics exporter (#805)
Browse files Browse the repository at this point in the history
* feat: support prometheus-client metrics exporter

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

* test: text encode in ut

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

---------

Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx authored Nov 28, 2024
1 parent 7e0b714 commit 9321f60
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ jobs:
cargo clippy --all-targets --features tokio-console -- -D warnings
cargo clippy --all-targets --features deadlock -- -D warnings
cargo clippy --all-targets --features tracing -- -D warnings
cargo clippy --all-targets --features prometheus,opentelemetry_0_26,opentelemetry_0_27 -- -D warnings
cargo clippy --all-targets --features prometheus,prometheus-client_0_22,opentelemetry_0_26,opentelemetry_0_27 -- -D warnings
cargo clippy --all-targets -- -D warnings
- if: steps.cache.outputs.cache-hit != 'true'
uses: taiki-e/install-action@cargo-llvm-cov
Expand All @@ -182,7 +182,7 @@ jobs:
RUST_BACKTRACE: 1
CI: true
run: |
cargo llvm-cov --no-report nextest --features "strict_assertions,sanity,prometheus,opentelemetry_0_26,opentelemetry_0_27"
cargo llvm-cov --no-report nextest --features "strict_assertions,sanity,prometheus,prometheus-client_0_22,opentelemetry_0_26,opentelemetry_0_27"
- name: Run examples with coverage
env:
RUST_BACKTRACE: 1
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ tracing = "0.1"
prometheus = "0.13"
opentelemetry_0_27 = { package = "opentelemetry", version = "0.27" }
opentelemetry_0_26 = { package = "opentelemetry", version = "0.26" }
prometheus-client_0_22 = { package = "prometheus-client", version = "0.22" }

# foyer components
foyer-common = { version = "0.13.0-dev", path = "foyer-common" }
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ check:
./scripts/minimize-dashboards.sh
cargo sort -w
cargo fmt --all
cargo clippy --all-targets --features prometheus,opentelemetry_0_26,opentelemetry_0_27
cargo clippy --all-targets --features prometheus,prometheus-client_0_22,opentelemetry_0_26,opentelemetry_0_27

check-all:
shellcheck ./scripts/*
Expand All @@ -21,11 +21,11 @@ check-all:
cargo clippy --all-targets --features tokio-console
cargo clippy --all-targets --features sanity
cargo clippy --all-targets --features tracing
cargo clippy --all-targets --features prometheus,opentelemetry_0_26,opentelemetry_0_27
cargo clippy --all-targets --features prometheus,prometheus-client_0_22,opentelemetry_0_26,opentelemetry_0_27
cargo clippy --all-targets

test:
RUST_BACKTRACE=1 cargo nextest run --all --features "strict_assertions,sanity,prometheus,opentelemetry_0_26,opentelemetry_0_27"
RUST_BACKTRACE=1 cargo nextest run --all --features "strict_assertions,sanity,prometheus,prometheus-client_0_22,opentelemetry_0_26,opentelemetry_0_27"
RUST_BACKTRACE=1 cargo test --doc

test-ignored:
Expand Down
3 changes: 3 additions & 0 deletions foyer-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ opentelemetry_0_27 = { workspace = true, optional = true }
parking_lot = { workspace = true }
pin-project = "1"
prometheus = { workspace = true, optional = true }
prometheus-client_0_22 = { workspace = true, optional = true }
serde = { workspace = true }
tokio = { workspace = true }

Expand All @@ -36,6 +37,8 @@ rand = "0.8.5"
strict_assertions = []
tracing = ["fastrace/enable"]
prometheus = ["dep:prometheus"]
prometheus-client = ["prometheus-client_0_22"]
prometheus-client_0_22 = ["dep:prometheus-client_0_22"]
opentelemetry = ["opentelemetry_0_27"]
opentelemetry_0_27 = ["dep:opentelemetry_0_27"]
opentelemetry_0_26 = ["dep:opentelemetry_0_26"]
Expand Down
6 changes: 3 additions & 3 deletions foyer-common/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ 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: &[&str]) -> impl CounterOps;
fn counter(&self, labels: &[&'static str]) -> impl CounterOps;
}

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

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

/// Metrics registry.
Expand Down
14 changes: 14 additions & 0 deletions foyer-common/src/metrics/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,20 @@ mod tests {
case(&PrometheusMetricsRegistry::new(prometheus::Registry::new()));
}

#[cfg(feature = "prometheus-client_0_22")]
#[test]
fn test_metrics_prometheus_client_0_22() {
use std::sync::Arc;

use parking_lot::Mutex;

use crate::metrics::registry::prometheus_client_0_22::PrometheusClientMetricsRegistry;

case(&PrometheusClientMetricsRegistry::new(Arc::new(Mutex::new(
prometheus_client_0_22::registry::Registry::default(),
))));
}

#[cfg(feature = "opentelemetry_0_27")]
#[test]
fn test_metrics_opentelemetry_0_27() {
Expand Down
8 changes: 8 additions & 0 deletions foyer-common/src/metrics/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ pub mod noop;
#[cfg(feature = "prometheus")]
pub mod prometheus;

/// Prometheus metrics components.
#[cfg(feature = "prometheus-client")]
pub use prometheus_client_0_22 as prometheus_client;

/// Prometheus metrics components.
#[cfg(feature = "prometheus-client_0_22")]
pub mod prometheus_client_0_22;

#[cfg(feature = "opentelemetry")]
pub use opentelemetry_0_27 as opentelemetry;

Expand Down
6 changes: 3 additions & 3 deletions foyer-common/src/metrics/registry/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl CounterOps for NoopMetricsRegistry {
}

impl CounterVecOps for NoopMetricsRegistry {
fn counter(&self, _: &[&str]) -> impl CounterOps {
fn counter(&self, _: &[&'static str]) -> impl CounterOps {
NoopMetricsRegistry
}
}
Expand All @@ -37,7 +37,7 @@ impl GaugeOps for NoopMetricsRegistry {
}

impl GaugeVecOps for NoopMetricsRegistry {
fn gauge(&self, _: &[&str]) -> impl GaugeOps {
fn gauge(&self, _: &[&'static str]) -> impl GaugeOps {
NoopMetricsRegistry
}
}
Expand All @@ -47,7 +47,7 @@ impl HistogramOps for NoopMetricsRegistry {
}

impl HistogramVecOps for NoopMetricsRegistry {
fn histogram(&self, _: &[&str]) -> impl HistogramOps {
fn histogram(&self, _: &[&'static str]) -> impl HistogramOps {
NoopMetricsRegistry
}
}
Expand Down
6 changes: 3 additions & 3 deletions foyer-common/src/metrics/registry/opentelemetry_0_26.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct MetricVec {
}

impl CounterVecOps for MetricVec {
fn counter(&self, labels: &[&str]) -> impl CounterOps {
fn counter(&self, labels: &[&'static str]) -> impl CounterOps {
let counter = self.meter.u64_counter(self.name).with_description(self.desc).init();
let labels = self
.label_names
Expand All @@ -97,7 +97,7 @@ impl CounterVecOps for MetricVec {
}

impl GaugeVecOps for MetricVec {
fn gauge(&self, labels: &[&str]) -> impl GaugeOps {
fn gauge(&self, labels: &[&'static str]) -> impl GaugeOps {
let gauge = self.meter.u64_gauge(self.name).with_description(self.desc).init();
let labels = self
.label_names
Expand All @@ -111,7 +111,7 @@ impl GaugeVecOps for MetricVec {
}

impl HistogramVecOps for MetricVec {
fn histogram(&self, labels: &[&str]) -> impl HistogramOps {
fn histogram(&self, labels: &[&'static str]) -> impl HistogramOps {
let histogram = self.meter.f64_histogram(self.name).with_description(self.desc).init();
let labels = self
.label_names
Expand Down
6 changes: 3 additions & 3 deletions foyer-common/src/metrics/registry/opentelemetry_0_27.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct MetricVec {
}

impl CounterVecOps for MetricVec {
fn counter(&self, labels: &[&str]) -> impl CounterOps {
fn counter(&self, labels: &[&'static str]) -> impl CounterOps {
let counter = self.meter.u64_counter(self.name).with_description(self.desc).build();
let labels = self
.label_names
Expand All @@ -97,7 +97,7 @@ impl CounterVecOps for MetricVec {
}

impl GaugeVecOps for MetricVec {
fn gauge(&self, labels: &[&str]) -> impl GaugeOps {
fn gauge(&self, labels: &[&'static str]) -> impl GaugeOps {
let gauge = self.meter.u64_gauge(self.name).with_description(self.desc).build();
let labels = self
.label_names
Expand All @@ -111,7 +111,7 @@ impl GaugeVecOps for MetricVec {
}

impl HistogramVecOps for MetricVec {
fn histogram(&self, labels: &[&str]) -> impl HistogramOps {
fn histogram(&self, labels: &[&'static str]) -> impl HistogramOps {
let histogram = self.meter.f64_histogram(self.name).with_description(self.desc).build();
let labels = self
.label_names
Expand Down
8 changes: 4 additions & 4 deletions foyer-common/src/metrics/registry/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl CounterOps for IntCounter {
}

impl CounterVecOps for IntCounterVec {
fn counter(&self, labels: &[&str]) -> impl CounterOps {
fn counter(&self, labels: &[&'static str]) -> impl CounterOps {
self.with_label_values(labels)
}
}
Expand All @@ -139,7 +139,7 @@ impl GaugeOps for IntGauge {
}

impl GaugeVecOps for IntGaugeVec {
fn gauge(&self, labels: &[&str]) -> impl GaugeOps {
fn gauge(&self, labels: &[&'static str]) -> impl GaugeOps {
self.with_label_values(labels)
}
}
Expand All @@ -151,12 +151,12 @@ impl HistogramOps for Histogram {
}

impl HistogramVecOps for HistogramVec {
fn histogram(&self, labels: &[&str]) -> impl HistogramOps {
fn histogram(&self, labels: &[&'static str]) -> impl HistogramOps {
self.with_label_values(labels)
}
}

/// Prometheus metrics registry.
/// Prometheus metric registry with lib `prometheus`.
///
/// The [`PrometheusMetricsRegistry`] can be cloned and used by multiple foyer instances, without worrying about
/// duplicately registering.
Expand Down
Loading

0 comments on commit 9321f60

Please sign in to comment.