-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: terassyi <[email protected]>
- Loading branch information
Showing
7 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pub mod error; | |
pub mod reconciler; | ||
pub mod server; | ||
pub mod webhook; | ||
mod metrics; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use kube::Resource; | ||
use prometheus::Registry; | ||
use prometheus::{histogram_opts, opts, HistogramVec, IntCounter, IntCounterVec}; | ||
use sartd_trace::error::TraceableError; | ||
use tokio::time::Instant; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Metrics { | ||
pub reconciliations: IntCounterVec, | ||
pub failures: IntCounterVec, | ||
pub reconcile_duration: HistogramVec, | ||
} | ||
|
||
impl Default for Metrics { | ||
fn default() -> Self { | ||
let reconcile_duration = HistogramVec::new( | ||
histogram_opts!( | ||
"sart_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!( | ||
"sart_controller_reconciliation_errors_total", | ||
"reconciliation errors", | ||
), | ||
&["resource", "instance", "error"], | ||
) | ||
.unwrap(); | ||
let reconciliations = IntCounterVec::new( | ||
opts!( | ||
"sart_controller_reconciliation_total", | ||
"Total count of reconciliations", | ||
), | ||
&["resource", "instance"], | ||
) | ||
.unwrap(); | ||
Metrics { | ||
reconciliations, | ||
failures, | ||
reconcile_duration, | ||
} | ||
} | ||
} | ||
|
||
impl Metrics { | ||
pub fn register(self, registry: &Registry) -> Result<Self, prometheus::Error> { | ||
Ok(self) | ||
} | ||
|
||
pub fn reconcile_failure<T: Resource<DynamicType = ()>, E: TraceableError>( | ||
&self, | ||
resource: &T, | ||
e: &E, | ||
) { | ||
self.failures | ||
.with_label_values(&[ | ||
&resource.object_ref(&()).kind.unwrap(), | ||
&resource.object_ref(&()).name.unwrap(), | ||
e.metric_label().as_ref(), | ||
]) | ||
.inc() | ||
} | ||
|
||
pub fn reconciliation<T: Resource<DynamicType = ()>>(&self, resource: &T) { | ||
self.reconciliations | ||
.with_label_values(&[ | ||
&resource.object_ref(&()).kind.unwrap(), | ||
&resource.object_ref(&()).name.unwrap(), | ||
]) | ||
.inc() | ||
} | ||
} | ||
|
||
/// Smart function duration measurer | ||
/// | ||
/// Relies on Drop to calculate duration and register the observation in the histogram | ||
pub struct ReconcileMeasurer { | ||
start: Instant, | ||
metric: HistogramVec, | ||
} | ||
|
||
impl Drop for ReconcileMeasurer { | ||
fn drop(&mut self) { | ||
#[allow(clippy::cast_precision_loss)] | ||
let duration = self.start.elapsed().as_millis() as f64 / 1000.0; | ||
self.metric.with_label_values(&[]).observe(duration); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ pub mod controller; | |
pub mod crd; | ||
pub mod error; | ||
pub mod fixture; | ||
pub mod metrics; | ||
pub mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use kube::Resource; | ||
use prometheus::Registry; | ||
use prometheus::{histogram_opts, opts, HistogramVec, IntCounter, IntCounterVec}; | ||
use sartd_trace::error::TraceableError; | ||
use tokio::time::Instant; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Metrics { | ||
pub reconciliations: IntCounterVec, | ||
pub failures: IntCounterVec, | ||
pub reconcile_duration: HistogramVec, | ||
} | ||
|
||
impl Default for Metrics { | ||
fn default() -> Self { | ||
let reconcile_duration = HistogramVec::new( | ||
histogram_opts!( | ||
"sart_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!( | ||
"sart_controller_reconciliation_errors_total", | ||
"reconciliation errors", | ||
), | ||
&["resource", "instance", "error"], | ||
) | ||
.unwrap(); | ||
let reconciliations = IntCounterVec::new( | ||
opts!( | ||
"sart_controller_reconciliation_total", | ||
"Total count of reconciliations", | ||
), | ||
&["resource", "instance"], | ||
) | ||
.unwrap(); | ||
Metrics { | ||
reconciliations, | ||
failures, | ||
reconcile_duration, | ||
} | ||
} | ||
} | ||
|
||
impl Metrics { | ||
pub fn register(self, registry: &Registry) -> Result<Self, prometheus::Error> { | ||
Ok(self) | ||
} | ||
|
||
pub fn reconcile_failure<T: Resource<DynamicType = ()>, E: TraceableError>( | ||
&self, | ||
resource: &T, | ||
e: &E, | ||
) { | ||
self.failures | ||
.with_label_values(&[ | ||
&resource.object_ref(&()).kind.unwrap(), | ||
&resource.object_ref(&()).name.unwrap(), | ||
e.metric_label().as_ref(), | ||
]) | ||
.inc() | ||
} | ||
|
||
pub fn reconciliation<T: Resource<DynamicType = ()>>(&self, resource: &T) { | ||
self.reconciliations.with_label_values(&[ | ||
&resource.object_ref(&()).kind.unwrap(), | ||
&resource.object_ref(&()).name.unwrap(), | ||
]).inc() | ||
} | ||
} | ||
|
||
/// Smart function duration measurer | ||
/// | ||
/// Relies on Drop to calculate duration and register the observation in the histogram | ||
pub struct ReconcileMeasurer { | ||
start: Instant, | ||
metric: HistogramVec, | ||
} | ||
|
||
impl Drop for ReconcileMeasurer { | ||
fn drop(&mut self) { | ||
#[allow(clippy::cast_precision_loss)] | ||
let duration = self.start.elapsed().as_millis() as f64 / 1000.0; | ||
self.metric.with_label_values(&[]).observe(duration); | ||
} | ||
} |