наблюдаемость: завершить базовый контур Community
CI / Rust Checks (push) Failing after 4m28s
CI / UI Checks (push) Has been skipped
CI / Frontend E2E (push) Has been skipped
CI / Community Image Smoke (push) Has been skipped
CI / Deploy (push) Has been skipped

Добавить структурированные журналы, метрики, трассировку и безопасный канал критических ошибок. Усилить границы рантайма, тесты, проверку зависимостей и сценарии развёртывания.
This commit is contained in:
bsodfather committed 2026-07-31 01:01:14 +03:00
1 parent 99bd05c145
commit 0e8f1ca03a
160 files changed
+13506 -1499

No files matched your search

@@ -0,0 +1,133 @@
use std::time::Instant;
use axum::{
extract::{MatchedPath, Request},
middleware::Next,
response::Response,
};
use metrics::{Gauge, Unit};
use crate::{MetricKind, MetricUnit, metric_schema};
pub async fn record_http_request(request: Request, next: Next) -> Response {
let route = request
.extensions()
.get::<MatchedPath>()
.map_or("unmatched", MatchedPath::as_str)
.to_owned();
let method = normalized_http_method(request.method().as_str());
let started_at = Instant::now();
let _inflight = GaugeGuard::increment("crank_http_inflight");
let response = next.run(request).await;
let status_class = status_class(response.status().as_u16());
metrics::counter!(
"crank_http_requests_total",
"route" => route.clone(),
"method" => method,
"status_class" => status_class
)
.increment(1);
metrics::histogram!(
"crank_http_request_duration_seconds",
"route" => route,
"method" => method
)
.record(started_at.elapsed().as_secs_f64());
response
}
pub fn record_db_pool_connections(total: u32, idle: usize) {
let idle = idle.min(total as usize) as f64;
metrics::gauge!("crank_db_pool_connections", "state" => "idle").set(idle);
metrics::gauge!("crank_db_pool_connections", "state" => "used").set(f64::from(total) - idle);
}
pub(crate) fn register_metric_schema() {
for definition in metric_schema() {
let unit = match definition.unit {
MetricUnit::Count => Unit::Count,
MetricUnit::Seconds => Unit::Seconds,
};
match definition.kind {
MetricKind::Counter => {
metrics::describe_counter!(definition.name, unit, definition.description);
}
MetricKind::Gauge => {
metrics::describe_gauge!(definition.name, unit, definition.description);
}
MetricKind::Histogram => {
metrics::describe_histogram!(definition.name, unit, definition.description);
}
}
}
metrics::gauge!("crank_http_inflight").set(0.0);
metrics::gauge!("crank_mcp_active_sessions").set(0.0);
metrics::gauge!("crank_runtime_inflight").set(0.0);
metrics::gauge!("crank_db_pool_connections", "state" => "idle").set(0.0);
metrics::gauge!("crank_db_pool_connections", "state" => "used").set(0.0);
metrics::gauge!("crank_catalog_tools").set(0.0);
metrics::gauge!("crank_catalog_estimated_context_tokens").set(0.0);
metrics::gauge!("crank_catalog_warnings").set(0.0);
}
fn normalized_http_method(method: &str) -> &'static str {
match method {
"GET" => "GET",
"POST" => "POST",
"PUT" => "PUT",
"PATCH" => "PATCH",
"DELETE" => "DELETE",
"OPTIONS" => "OPTIONS",
"HEAD" => "HEAD",
"CONNECT" => "CONNECT",
"TRACE" => "TRACE",
_ => "OTHER",
}
}
fn status_class(status: u16) -> &'static str {
match status {
100..=199 => "1xx",
200..=299 => "2xx",
300..=399 => "3xx",
400..=499 => "4xx",
500..=599 => "5xx",
_ => "other",
}
}
struct GaugeGuard {
gauge: Gauge,
}
impl GaugeGuard {
fn increment(name: &'static str) -> Self {
let gauge = metrics::gauge!(name);
gauge.increment(1.0);
Self { gauge }
}
}
impl Drop for GaugeGuard {
fn drop(&mut self) {
self.gauge.decrement(1.0);
}
}
#[cfg(test)]
mod tests {
use super::{normalized_http_method, status_class};
#[test]
fn normalizes_unbounded_http_values() {
assert_eq!(normalized_http_method("GET"), "GET");
assert_eq!(normalized_http_method("CUSTOM-user-controlled"), "OTHER");
assert_eq!(status_class(204), "2xx");
assert_eq!(status_class(429), "4xx");
assert_eq!(status_class(999), "other");
}
}