-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prometheus metrics via
metrics-rs
- Loading branch information
Showing
6 changed files
with
104 additions
and
0 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
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,14 @@ | ||
use actix_web::HttpRequest; | ||
use lazy_static::lazy_static; | ||
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle}; | ||
use paperclip::actix::api_v2_operation; | ||
Check warning on line 4 in src/server/metrics/handler.rs GitHub Actions / Nightly tests
Check failure on line 4 in src/server/metrics/handler.rs GitHub Actions / Serwus Clippy Outputunused import: `paperclip::actix::api_v2_operation`
|
||
|
||
lazy_static! { | ||
pub static ref PROM_HANDLER: PrometheusHandle = PrometheusBuilder::new() | ||
.install_recorder() | ||
.expect("failed to install recorder"); | ||
} | ||
|
||
pub async fn metrics(_: HttpRequest) -> String { | ||
PROM_HANDLER.render() | ||
} |
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,74 @@ | ||
use std::{ | ||
future::{ready, Ready}, | ||
time::Instant, | ||
}; | ||
|
||
use actix_web::{ | ||
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform}, | ||
Error, | ||
}; | ||
use futures_util::future::LocalBoxFuture; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Metrics; | ||
|
||
impl<S, B> Transform<S, ServiceRequest> for Metrics | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, | ||
S::Future: 'static, | ||
B: 'static, | ||
{ | ||
type Response = ServiceResponse<B>; | ||
type Error = Error; | ||
type InitError = (); | ||
type Transform = MetricsMiddleware<S>; | ||
type Future = Ready<Result<Self::Transform, Self::InitError>>; | ||
|
||
fn new_transform(&self, service: S) -> Self::Future { | ||
ready(Ok(MetricsMiddleware { service })) | ||
} | ||
} | ||
|
||
pub struct MetricsMiddleware<S> { | ||
service: S, | ||
} | ||
|
||
impl<S, B> Service<ServiceRequest> for MetricsMiddleware<S> | ||
where | ||
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static, | ||
S::Future: 'static, | ||
B: 'static, | ||
{ | ||
type Response = ServiceResponse<B>; | ||
type Error = Error; | ||
type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>; | ||
|
||
forward_ready!(service); | ||
|
||
fn call(&self, req: ServiceRequest) -> Self::Future { | ||
let start = Instant::now(); | ||
let path = req.path().to_owned(); | ||
let method = req.method().to_string(); | ||
|
||
let fut = self.service.call(req); | ||
|
||
Box::pin(async move { | ||
let response = fut.await; | ||
|
||
let latency = start.elapsed().as_secs_f64(); | ||
let status = response | ||
.as_ref() | ||
.map(|r| r.status()) | ||
.unwrap_or_else(|e| e.as_response_error().status_code()) | ||
.as_u16() | ||
.to_string(); | ||
|
||
let labels = [("method", method), ("path", path), ("status", status)]; | ||
|
||
metrics::counter!("http_requests_total", &labels).increment(1); | ||
metrics::histogram!("http_requests_duration_seconds", &labels).record(latency); | ||
|
||
response | ||
}) | ||
} | ||
} |
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,2 @@ | ||
pub mod handler; | ||
pub mod middleware; |
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