-
-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add metrics server and http metrics (#1394)
* feat: add metrics server and http metrics * setup metrics * update default config * fix tests
- Loading branch information
Showing
9 changed files
with
247 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,52 @@ | ||
use std::time::Instant; | ||
|
||
use axum::{extract::MatchedPath, http::Request, middleware::Next, response::IntoResponse}; | ||
use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle}; | ||
|
||
pub fn setup_metrics_recorder() -> PrometheusHandle { | ||
const EXPONENTIAL_SECONDS: &[f64] = &[ | ||
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, | ||
]; | ||
|
||
PrometheusBuilder::new() | ||
.set_buckets_for_metric( | ||
Matcher::Full("http_requests_duration_seconds".to_string()), | ||
EXPONENTIAL_SECONDS, | ||
) | ||
.unwrap() | ||
.install_recorder() | ||
.unwrap() | ||
} | ||
|
||
/// Middleware to record some common HTTP metrics | ||
/// Generic over B to allow for arbitrary body types (eg Vec<u8>, Streams, a deserialized thing, etc) | ||
/// Someday tower-http might provide a metrics middleware: https://github.com/tower-rs/tower-http/issues/57 | ||
pub async fn track_metrics<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse { | ||
let start = Instant::now(); | ||
|
||
let path = if let Some(matched_path) = req.extensions().get::<MatchedPath>() { | ||
matched_path.as_str().to_owned() | ||
} else { | ||
req.uri().path().to_owned() | ||
}; | ||
|
||
let method = req.method().clone(); | ||
|
||
// Run the rest of the request handling first, so we can measure it and get response | ||
// codes. | ||
let response = next.run(req).await; | ||
|
||
let latency = start.elapsed().as_secs_f64(); | ||
let status = response.status().as_u16().to_string(); | ||
|
||
let labels = [ | ||
("method", method.to_string()), | ||
("path", path), | ||
("status", status), | ||
]; | ||
|
||
metrics::increment_counter!("http_requests_total", &labels); | ||
metrics::histogram!("http_requests_duration_seconds", latency, &labels); | ||
|
||
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
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
15d214e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
atuin-docs – ./
atuin-docs.vercel.app
atuin-docs-git-main-atuin.vercel.app
atuin-docs-atuin.vercel.app