Skip to content

Commit

Permalink
Try out official prometheus_client crate
Browse files Browse the repository at this point in the history
Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed Jun 21, 2024
1 parent 3504368 commit 29ca988
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 46 deletions.
40 changes: 24 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ schemars = { version = "0.8.12", features = ["chrono"] }
serde = { version = "1.0.185", features = ["derive"] }
serde_json = "1.0.105"
serde_yaml = "0.9.25"
prometheus = "0.13.3"
chrono = { version = "0.4.26", features = ["serde"] }
tracing = "0.1.37"
tracing-subscriber = { version = "0.3.17", features = ["json", "env-filter"] }
Expand All @@ -44,6 +43,7 @@ opentelemetry-otlp = { version = "0.13.0", features = ["tokio"], optional = true
tonic = { version = "0.9", optional = true }
thiserror = "1.0.47"
anyhow = "1.0.75"
prometheus-client = "0.22.2"

[dev-dependencies]
assert-json-diff = "2.0.2"
Expand Down
2 changes: 1 addition & 1 deletion src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub struct State {
/// Diagnostics populated by the reconciler
diagnostics: Arc<RwLock<Diagnostics>>,
/// Metrics registry
registry: prometheus::Registry,
registry: Registry,
}

/// State wrapper around the controller outputs for the web server
Expand Down
64 changes: 36 additions & 28 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
use crate::{Document, Error};
use kube::ResourceExt;
use prometheus::{histogram_opts, opts, HistogramVec, IntCounter, IntCounterVec, Registry};
use prometheus_client::{
encoding::EncodeLabelSet,
metrics::counter::{Atomic, Counter},
metrics::family::Family,
metrics::histogram::Histogram,
registry::Registry,
};
use tokio::time::Instant;

#[derive(Clone)]
pub struct Metrics {
pub reconciliations: IntCounter,
pub failures: IntCounterVec,
pub reconcile_duration: HistogramVec,
pub reconciliations: Counter,
pub failures: Counter,
pub reconcile_duration: Histogram,
}

impl Default for Metrics {
fn default() -> Self {
let reconcile_duration = HistogramVec::new(
histogram_opts!(
"doc_controller_reconcile_duration_seconds",
"The duration of reconcile to complete in seconds"
)
.buckets(vec![0.01, 0.1, 0.25, 0.5, 1., 5., 15., 60.]),
&[],
)
.unwrap();
let failures = IntCounterVec::new(
opts!(
"doc_controller_reconciliation_errors_total",
"reconciliation errors",
),
&["instance", "error"],
)
.unwrap();
let reconciliations =
IntCounter::new("doc_controller_reconciliations_total", "reconciliations").unwrap();
let reconcile_duration = Histogram::new([0.01, 0.1, 0.25, 0.5, 1., 5., 15., 60.].into_iter());
let failures = Family::<ErrorLabels, Counter>::default();
let reconciliations = Family::<(), Counter>::default();
Metrics {
reconciliations,
failures,
Expand All @@ -39,12 +29,30 @@ impl Default for Metrics {
}
}

#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)]
struct ErrorLabels {
instance: String,
errror: String,
}

impl Metrics {
/// Register API metrics to start tracking them.
pub fn register(self, registry: &Registry) -> Result<Self, prometheus::Error> {
registry.register(Box::new(self.reconcile_duration.clone()))?;
registry.register(Box::new(self.failures.clone()))?;
registry.register(Box::new(self.reconciliations.clone()))?;
pub fn register(self, registry: &Registry) -> Self {
registry.register(
"doc_controller_reconcile_duration_seconds",
"The duration of reconcile to complete in seconds",
self.reconcile_duration.clone(),
);
registry.register(
"doc_controller_reconciliation_errors_total",
"reconciliation errors",
self.failures.clone(),
);
registry.register(
"doc_controller_reconciliations_total",
"reconciliations",
self.reconciliations.clone(),
);
Ok(self)
}

Expand All @@ -68,7 +76,7 @@ impl Metrics {
/// Relies on Drop to calculate duration and register the observation in the histogram
pub struct ReconcileMeasurer {
start: Instant,
metric: HistogramVec,
metric: Histogram,
}

impl Drop for ReconcileMeasurer {
Expand Down

0 comments on commit 29ca988

Please sign in to comment.