This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 8d9c7f41c feat(layer/prometheus): Support custom metric bucket for 
Histogram (#3275)
8d9c7f41c is described below

commit 8d9c7f41c03bb6d3641cc442833605793f83ea98
Author: Nadeshiko Manju <[email protected]>
AuthorDate: Sun Oct 15 23:55:16 2023 +0800

    feat(layer/prometheus): Support custom metric bucket for Histogram (#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]>
---
 core/src/layers/prometheus.rs | 61 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 52 insertions(+), 9 deletions(-)

diff --git a/core/src/layers/prometheus.rs b/core/src/layers/prometheus.rs
index 2558bb50b..76379f5b4 100644
--- a/core/src/layers/prometheus.rs
+++ b/core/src/layers/prometheus.rs
@@ -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
 ///
 /// ```
@@ -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
     }
 }
 
@@ -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(),
         }
     }
@@ -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",
@@ -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();

Reply via email to