-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from Ptrskay3/backend-agnostic-layer
A backend-agnostic layer
- Loading branch information
Showing
5 changed files
with
150 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
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,13 @@ | ||
[package] | ||
name = "base-metric-layer-example" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
axum = "0.7.1" | ||
tokio = { version = "1.0", features = ["full"] } | ||
tracing = "0.1" | ||
tracing-subscriber = { version = "0.3", features = ["env-filter"] } | ||
axum-prometheus = { path = "../../", features = ["push-gateway"] } | ||
|
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,43 @@ | ||
//! This example uses the `BaseMetricLayer`, which only emits metrics using the `metrics` crate's macros, | ||
//! and the global exporter/recorder is fully up to user to initialize and configure. | ||
//! | ||
//! Run with | ||
//! | ||
//! ```not_rust | ||
//! cd examples && cargo run -p base-metric-layer-example | ||
//! ``` | ||
//! | ||
use axum::{routing::get, Router}; | ||
use axum_prometheus::{metrics_exporter_prometheus::PrometheusBuilder, BaseMetricLayer}; | ||
use std::{net::SocketAddr, time::Duration}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Initialize the recorder as you like. This example uses push gateway mode instead of a http listener. | ||
// To use this, don't forget to enable the "push-gateway" feature in `axum-prometheus`. | ||
PrometheusBuilder::new() | ||
.with_push_gateway( | ||
"http://127.0.0.1:9091/metrics/job/example", | ||
Duration::from_secs(10), | ||
None, | ||
None, | ||
) | ||
.expect("push gateway endpoint should be valid") | ||
.install() | ||
.expect("failed to install Prometheus recorder"); | ||
|
||
let app = Router::<()>::new() | ||
.route("/fast", get(|| async {})) | ||
.route( | ||
"/slow", | ||
get(|| async { | ||
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||
}), | ||
) | ||
// Only need to add this layer at the end. | ||
.layer(BaseMetricLayer::new()); | ||
let listener = tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 3000))) | ||
.await | ||
.unwrap(); | ||
axum::serve(listener, app).await.unwrap() | ||
} |
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