наблюдаемость: ввести безопасный контракт метрик
CI / Rust Checks (pull_request) Successful in 6m15s
CI / UI Checks (pull_request) Successful in 5s
CI / Community Image Smoke (pull_request) Successful in 4m25s
CI / Frontend E2E (pull_request) Successful in 5m17s
CI / Deploy (pull_request) Has been skipped
CI / Rust Checks (push) Successful in 6m9s
CI / UI Checks (push) Successful in 5s
CI / Community Image Smoke (push) Successful in 1m3s
CI / Frontend E2E (push) Successful in 3m47s
CI / Deploy (push) Failing after 3s

This commit is contained in:
2026-07-31 05:04:01 +03:00
parent ec2453c00f
commit 9b1a739e39
50 changed files with 3066 additions and 433 deletions
+18 -5
View File
@@ -155,7 +155,7 @@ impl MetricsSurface {
Router::new()
.route("/metrics", get(render_metrics))
.route("/health", get(metrics_health))
.layer(middleware::from_fn_with_state(
.route_layer(middleware::from_fn_with_state(
self.state.clone(),
authorize_metrics,
))
@@ -179,6 +179,12 @@ pub struct MetricsServer {
}
impl MetricsServer {
pub fn local_addr(&self) -> Result<SocketAddr, MetricsServeError> {
self.listener
.local_addr()
.map_err(|_| MetricsServeError::LocalAddress)
}
pub async fn serve(self) -> Result<(), MetricsServeError> {
axum::serve(self.listener, self.router)
.await
@@ -198,6 +204,8 @@ pub enum MetricsServeError {
Bind,
#[error("metrics listener stopped unexpectedly")]
Serve,
#[error("failed to read metrics listener address")]
LocalAddress,
}
pub(crate) fn install_prometheus_recorder(
@@ -257,10 +265,15 @@ async fn authorize_metrics(
}
fn bearer_token(headers: &HeaderMap) -> Option<&[u8]> {
headers
.get(header::AUTHORIZATION)?
.as_bytes()
.strip_prefix(b"Bearer ")
let value = headers.get(header::AUTHORIZATION)?.as_bytes();
let separator = value.iter().position(|byte| *byte == b' ')?;
let (scheme, token_with_spaces) = value.split_at(separator);
let token_start = token_with_spaces.iter().position(|byte| *byte != b' ')?;
let token = &token_with_spaces[token_start..];
scheme
.eq_ignore_ascii_case(b"bearer")
.then_some(token)
.filter(|token| !token.is_empty())
}