mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-14 04:37:00 +00:00
feat: Add Prometheus metrics endpoint
This commit is contained in:
1 parent
ea96858c4a
commit
f14c401f39
25 files changed
+652
-46
No files matched your search
@@ -1,9 +1,11 @@
|
||||
use axum::{Json, extract::State, response::IntoResponse};
|
||||
use conduwuit::{
|
||||
Result,
|
||||
Err, Result,
|
||||
matrix::versions::{unstable_features, versions},
|
||||
utils::string::constant_time_eq,
|
||||
};
|
||||
use futures::StreamExt;
|
||||
use http::{HeaderMap, header};
|
||||
use ruma::{api::client::discovery::get_supported_versions, assign};
|
||||
|
||||
use crate::Ruma;
|
||||
@@ -75,3 +77,37 @@ pub(crate) async fn continuwuity_local_user_count(
|
||||
"count": user_count
|
||||
})))
|
||||
}
|
||||
|
||||
/// # `GET /_continuwuity/metrics`
|
||||
///
|
||||
/// Prometheus metrics in the OpenMetrics text format. Only routed when
|
||||
/// `allow_prometheus_metrics` is enabled.
|
||||
pub(crate) async fn continuwuity_metrics(
|
||||
State(services): State<crate::State>,
|
||||
headers: HeaderMap,
|
||||
) -> Result<impl IntoResponse> {
|
||||
if let Some(token) = &services.config.prometheus_metrics_token {
|
||||
let authorized = headers
|
||||
.get(header::AUTHORIZATION)
|
||||
.and_then(|value| value.to_str().ok())
|
||||
.and_then(|value| value.split_once(' '))
|
||||
.is_some_and(|(scheme, provided)| {
|
||||
scheme.eq_ignore_ascii_case("bearer") && constant_time_eq(provided, token)
|
||||
});
|
||||
|
||||
if !authorized {
|
||||
return Err!(Request(Forbidden("Invalid metrics token")));
|
||||
}
|
||||
}
|
||||
|
||||
services.update_metrics()?;
|
||||
let body = services.server.metrics.prometheus.encode()?;
|
||||
|
||||
Ok((
|
||||
[(
|
||||
header::CONTENT_TYPE,
|
||||
"application/openmetrics-text; version=1.0.0; charset=utf-8",
|
||||
)],
|
||||
body,
|
||||
))
|
||||
}
|
||||
Reference in new issue
Block a user