forked from prometheus/client_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/prometheus/client_rust in…
…to dyn-disp-metric
- Loading branch information
Showing
7 changed files
with
280 additions
and
5 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
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,75 @@ | ||
use hyper::{ | ||
service::{make_service_fn, service_fn}, | ||
Body, Request, Response, Server, | ||
}; | ||
use prometheus_client::{encoding::text::encode, metrics::counter::Counter, registry::Registry}; | ||
use std::{ | ||
future::Future, | ||
io, | ||
net::{IpAddr, Ipv4Addr, SocketAddr}, | ||
pin::Pin, | ||
sync::Arc, | ||
}; | ||
use tokio::signal::unix::{signal, SignalKind}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let request_counter: Counter<u64> = Default::default(); | ||
|
||
let mut registry = <Registry>::with_prefix("tokio_hyper_example"); | ||
|
||
registry.register( | ||
"requests", | ||
"How many requests the application has received", | ||
request_counter.clone(), | ||
); | ||
|
||
// Spawn a server to serve the OpenMetrics endpoint. | ||
let metrics_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8001); | ||
start_metrics_server(metrics_addr, registry).await | ||
} | ||
|
||
/// Start a HTTP server to report metrics. | ||
pub async fn start_metrics_server(metrics_addr: SocketAddr, registry: Registry) { | ||
let mut shutdown_stream = signal(SignalKind::terminate()).unwrap(); | ||
|
||
eprintln!("Starting metrics server on {metrics_addr}"); | ||
|
||
let registry = Arc::new(registry); | ||
Server::bind(&metrics_addr) | ||
.serve(make_service_fn(move |_conn| { | ||
let registry = registry.clone(); | ||
async move { | ||
let handler = make_handler(registry); | ||
Ok::<_, io::Error>(service_fn(handler)) | ||
} | ||
})) | ||
.with_graceful_shutdown(async move { | ||
shutdown_stream.recv().await; | ||
}) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
/// This function returns a HTTP handler (i.e. another function) | ||
pub fn make_handler( | ||
registry: Arc<Registry>, | ||
) -> impl Fn(Request<Body>) -> Pin<Box<dyn Future<Output = io::Result<Response<Body>>> + Send>> { | ||
// This closure accepts a request and responds with the OpenMetrics encoding of our metrics. | ||
move |_req: Request<Body>| { | ||
let reg = registry.clone(); | ||
Box::pin(async move { | ||
let mut buf = Vec::new(); | ||
encode(&mut buf, ®.clone()).map(|_| { | ||
let body = Body::from(buf); | ||
Response::builder() | ||
.header( | ||
hyper::header::CONTENT_TYPE, | ||
"application/openmetrics-text; version=1.0.0; charset=utf-8", | ||
) | ||
.body(body) | ||
.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
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