about summary refs log tree commit diff
path: root/src/metrics.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2021-12-05 23:00:01 +0300
committerVika <vika@fireburn.ru>2021-12-05 23:00:01 +0300
commit4aa7f01da39ab55b4f6346e7565d8bb29566de39 (patch)
tree125c147e1a39dfebc4be1a3ac3a6dd8fadb3855e /src/metrics.rs
parentc0b552318cb08872c7deae82a10122b0e16b7ae8 (diff)
Code cleanup and small bugfixing in templates
Diffstat (limited to 'src/metrics.rs')
-rw-r--r--src/metrics.rs41
1 files changed, 23 insertions, 18 deletions
diff --git a/src/metrics.rs b/src/metrics.rs
index 0537b9d..9f512dd 100644
--- a/src/metrics.rs
+++ b/src/metrics.rs
@@ -1,8 +1,11 @@
-use tide::{Request, Response, Result, Next};
-use prometheus::{self, IntCounterVec, HistogramVec, TextEncoder, Encoder, register_int_counter_vec, register_histogram_vec};
-use lazy_static::lazy_static;
 use async_trait::async_trait;
-use std::time::{Instant, Duration};
+use lazy_static::lazy_static;
+use prometheus::{
+    self, register_histogram_vec, register_int_counter_vec, Encoder, HistogramVec, IntCounterVec,
+    TextEncoder,
+};
+use std::time::{Duration, Instant};
+use tide::{Next, Request, Response, Result};
 
 // Copied from https://docs.rs/prometheus/0.12.0/src/prometheus/histogram.rs.html#885-889
 #[inline]
@@ -13,29 +16,27 @@ fn duration_to_seconds(d: Duration) -> f64 {
 
 lazy_static! {
     static ref HTTP_CONNS_COUNTER: IntCounterVec = register_int_counter_vec!(
-        "http_requests_total", "Number of processed HTTP requests",
+        "http_requests_total",
+        "Number of processed HTTP requests",
         &["code", "method", "url"]
-    ).unwrap();
-
+    )
+    .unwrap();
     static ref HTTP_REQUEST_DURATION_HISTOGRAM: HistogramVec = register_histogram_vec!(
-        "http_request_duration_seconds", "Duration of HTTP requests",
+        "http_request_duration_seconds",
+        "Duration of HTTP requests",
         &["code", "method", "url"]
-    ).unwrap();
+    )
+    .unwrap();
 }
 
 pub struct InstrumentationMiddleware {}
 
-
 #[async_trait]
 impl<S> tide::Middleware<S> for InstrumentationMiddleware
 where
     S: Send + Sync + Clone + 'static,
 {
-    async fn handle(
-        &self,
-        req: Request<S>,
-        next: Next<'_, S>,
-    ) -> Result {
+    async fn handle(&self, req: Request<S>, next: Next<'_, S>) -> Result {
         let url = req.url().to_string();
         let method = req.method().to_string();
         // Execute the request
@@ -45,8 +46,12 @@ where
         // Get the code from the response
         let code = res.status().to_string();
 
-        HTTP_CONNS_COUNTER.with_label_values(&[&code, &method, &url]).inc();
-        HTTP_REQUEST_DURATION_HISTOGRAM.with_label_values(&[&code, &method, &url]).observe(elapsed);
+        HTTP_CONNS_COUNTER
+            .with_label_values(&[&code, &method, &url])
+            .inc();
+        HTTP_REQUEST_DURATION_HISTOGRAM
+            .with_label_values(&[&code, &method, &url])
+            .observe(elapsed);
 
         Ok(res)
     }
@@ -54,7 +59,7 @@ where
 
 pub async fn gather<S>(_: Request<S>) -> Result
 where
-    S: Send + Sync + Clone
+    S: Send + Sync + Clone,
 {
     let mut buffer: Vec<u8> = vec![];
     let encoder = TextEncoder::new();