32 lines
765 B
Rust
32 lines
765 B
Rust
|
use rocket::{get, routes};
|
||
|
|
||
|
pub fn rocket() -> rocket::Rocket<rocket::Build> {
|
||
|
rocket::build().mount("/", routes![metrics,])
|
||
|
}
|
||
|
|
||
|
#[get("/metrics")]
|
||
|
fn metrics() -> Result<String, ServerError> {
|
||
|
Ok(
|
||
|
prometheus::TextEncoder::new()
|
||
|
.encode_to_string(&prometheus::default_registry().gather())?,
|
||
|
)
|
||
|
}
|
||
|
|
||
|
enum ServerError {
|
||
|
Prometheus,
|
||
|
}
|
||
|
|
||
|
impl From<prometheus::Error> for ServerError {
|
||
|
fn from(_: prometheus::Error) -> Self {
|
||
|
Self::Prometheus
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<'a> rocket::response::Responder<'a, 'a> for ServerError {
|
||
|
fn respond_to(self, _: &'a rocket::Request<'_>) -> rocket::response::Result<'a> {
|
||
|
Err(match self {
|
||
|
ServerError::Prometheus => rocket::http::Status::InternalServerError,
|
||
|
})
|
||
|
}
|
||
|
}
|