generated from ewpratten/rust-template
-
-
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.
Enable prometheus metrics in binaries
- Loading branch information
Showing
5 changed files
with
79 additions
and
10 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,44 @@ | ||
use hyper::{ | ||
service::{make_service_fn, service_fn}, | ||
Body, Method, Request, Response, Server, | ||
}; | ||
use prometheus::{Encoder, TextEncoder}; | ||
use std::{convert::Infallible, net::SocketAddr}; | ||
|
||
/// Handle an HTTP request | ||
#[allow(clippy::unused_async)] | ||
async fn handle_request(request: Request<Body>) -> Result<Response<Body>, Infallible> { | ||
// If the request is targeting the metrics endpoint | ||
if request.method() == Method::GET && request.uri().path() == "/metrics" { | ||
// Gather metrics | ||
let metric_families = prometheus::gather(); | ||
let body = { | ||
let mut buffer = Vec::new(); | ||
let encoder = TextEncoder::new(); | ||
encoder.encode(&metric_families, &mut buffer).unwrap(); | ||
String::from_utf8(buffer).unwrap() | ||
}; | ||
|
||
// Return the response | ||
return Ok(Response::new(Body::from(body))); | ||
} | ||
|
||
// Otherwise, just return a 404 | ||
Ok(Response::builder() | ||
.status(404) | ||
.body(Body::from("Not found")) | ||
.unwrap()) | ||
} | ||
|
||
/// Bring up an HTTP server that listens for metrics requests | ||
pub async fn serve_metrics(bind_addr: SocketAddr) { | ||
// Set up the server | ||
let make_service = | ||
make_service_fn(|_| async { Ok::<_, Infallible>(service_fn(handle_request)) }); | ||
let server = Server::bind(&bind_addr).serve(make_service); | ||
|
||
// Run the server | ||
if let Err(e) = server.await { | ||
eprintln!("Metrics server error: {e}"); | ||
} | ||
} |
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
41708f1
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.
Binary sizes for
aarch64-unknown-linux-musl
Channel:
stable
41708f1
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.
Binary sizes for
x86_64-unknown-linux-musl
Channel:
stable