Skip to content

Commit

Permalink
feat(layer/prometheus): Support custom metric bucket for Histogram (#…
Browse files Browse the repository at this point in the history
…3275)

* feat(layer/prometheus): Support custom metric bucket for Histogram

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

* Update code

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

* Update code

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

* update

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

* Update core/src/layers/prometheus.rs

Co-authored-by: Xuanwo <[email protected]>

* Update core/src/layers/prometheus.rs

Co-authored-by: Xuanwo <[email protected]>

* remove

* update doc

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

* update code

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

---------

Signed-off-by: Manjusaka <[email protected]>
Co-authored-by: Xuanwo <[email protected]>
  • Loading branch information
Zheaoli and Xuanwo authored Oct 15, 2023
1 parent 8539ccd commit 8d9c7f4
Showing 1 changed file with 52 additions and 9 deletions.
61 changes: 52 additions & 9 deletions core/src/layers/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ use crate::raw::*;
use crate::*;
/// Add [prometheus](https://docs.rs/prometheus) for every operations.
///
/// # Prometheus Metrics
///
/// In this section, we will introduce three metrics that are currently being exported by our project. These metrics are essential for understanding the behavior and performance of our applications.
///
///
/// | Metric Name | Type | Description | Labels |
/// |-------------------------|----------|---------------------------------------------------|---------------------|
/// | requests_total | Counter | Total times of 'create' operation being called | scheme, operation |
/// | requests_duration_seconds | Histogram | Histogram of the time spent on specific operation | scheme, operation |
/// | bytes_total | Histogram | Total size | scheme, operation |
///
/// For a more detailed explanation of these metrics and how they are used, please refer to the [Prometheus documentation](https://prometheus.io/docs/introduction/overview/).
///
/// # Histogram Configuration
///
/// The metric buckets for these histograms are automatically generated based on the `exponential_buckets(0.01, 2.0, 16)` configuration.
///
/// # Examples
///
/// ```
Expand Down Expand Up @@ -88,12 +105,34 @@ use crate::*;
#[derive(Default, Debug, Clone)]
pub struct PrometheusLayer {
registry: Registry,
requests_duration_seconds_buckets: Vec<f64>,
bytes_total_buckets: Vec<f64>,
}

impl PrometheusLayer {
/// create PrometheusLayer by incoming registry.
pub fn with_registry(registry: Registry) -> Self {
Self { registry }
Self {
registry,
requests_duration_seconds_buckets: exponential_buckets(0.01, 2.0, 16).unwrap(),
bytes_total_buckets: exponential_buckets(0.01, 2.0, 16).unwrap(),
}
}

/// set buckets for requests_duration_seconds
pub fn requests_duration_seconds_buckets(mut self, buckets: Vec<f64>) -> Self {
if !buckets.is_empty() {
self.requests_duration_seconds_buckets = buckets;
}
self
}

/// set buckets for bytes_total
pub fn bytes_total_buckets(mut self, buckets: Vec<f64>) -> Self {
if !buckets.is_empty() {
self.bytes_total_buckets = buckets;
}
self
}
}

Expand All @@ -106,7 +145,11 @@ impl<A: Accessor> Layer<A> for PrometheusLayer {

PrometheusAccessor {
inner,
stats: Arc::new(PrometheusMetrics::new(self.registry.clone())),
stats: Arc::new(PrometheusMetrics::new(
self.registry.clone(),
self.requests_duration_seconds_buckets.clone(),
self.bytes_total_buckets.clone(),
)),
scheme: scheme.to_string(),
}
}
Expand All @@ -124,7 +167,11 @@ pub struct PrometheusMetrics {

impl PrometheusMetrics {
/// new with prometheus register.
pub fn new(registry: Registry) -> Self {
pub fn new(
registry: Registry,
requests_duration_seconds_buckets: Vec<f64>,
bytes_total_buckets: Vec<f64>,
) -> Self {
let requests_total = register_int_counter_vec_with_registry!(
"requests_total",
"Total times of create be called",
Expand All @@ -135,18 +182,14 @@ impl PrometheusMetrics {
let opts = histogram_opts!(
"requests_duration_seconds",
"Histogram of the time spent on specific operation",
exponential_buckets(0.01, 2.0, 16).unwrap()
requests_duration_seconds_buckets
);

let requests_duration_seconds =
register_histogram_vec_with_registry!(opts, &["scheme", "operation"], registry)
.unwrap();

let opts = histogram_opts!(
"bytes_total",
"Total size of ",
exponential_buckets(0.01, 2.0, 16).unwrap()
);
let opts = histogram_opts!("bytes_total", "Total size of ", bytes_total_buckets);
let bytes_total =
register_histogram_vec_with_registry!(opts, &["scheme", "operation"], registry)
.unwrap();
Expand Down

0 comments on commit 8d9c7f4

Please sign in to comment.