наблюдаемость: ввести безопасный контракт метрик
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
CI / Rust Checks (pull_request) Successful in 6m15s
CI / UI Checks (pull_request) Successful in 5s
CI / Frontend E2E (pull_request) Successful in 5m17s
CI / Community Image Smoke (pull_request) Successful in 4m25s
CI / Deploy (pull_request) 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
CI / Rust Checks (pull_request) Successful in 6m15s
CI / UI Checks (pull_request) Successful in 5s
CI / Frontend E2E (pull_request) Successful in 5m17s
CI / Community Image Smoke (pull_request) Successful in 4m25s
CI / Deploy (pull_request) Skipped
This commit is contained in:
1 parent
ec2453c00f
commit
9b1a739e39
50 files changed
+3066
-433
No files matched your search
@@ -0,0 +1,243 @@
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use metrics::Gauge;
|
||||
|
||||
use crate::{
|
||||
CacheOutcome, ConfirmationOutcome, DbPoolState, Exporter, HttpMethod, HttpRoute,
|
||||
HttpStatusClass, IdempotencyOutcome, InvocationSource, LimitStage, McpMethod, McpOutcome,
|
||||
McpResponseMode, SignalType, ToolErrorKind, ToolOutcome, UpstreamOperationKind,
|
||||
UpstreamOutcome,
|
||||
};
|
||||
|
||||
pub fn record_http_request(
|
||||
route: HttpRoute,
|
||||
method: HttpMethod,
|
||||
status: HttpStatusClass,
|
||||
duration: Duration,
|
||||
) {
|
||||
metrics::counter!(
|
||||
"crank_http_requests_total",
|
||||
"route" => route.as_str(),
|
||||
"method" => method.as_str(),
|
||||
"status_class" => status.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
metrics::histogram!(
|
||||
"crank_http_request_duration_seconds",
|
||||
"route" => route.as_str(),
|
||||
"method" => method.as_str()
|
||||
)
|
||||
.record(duration.as_secs_f64());
|
||||
}
|
||||
|
||||
pub fn record_mcp_request(method: McpMethod, response_mode: McpResponseMode, outcome: McpOutcome) {
|
||||
metrics::counter!(
|
||||
"crank_mcp_requests_total",
|
||||
"method" => method.as_str(),
|
||||
"response_mode" => response_mode.as_str(),
|
||||
"outcome" => outcome.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
}
|
||||
|
||||
pub fn set_mcp_active_sessions(count: u64) {
|
||||
metrics::gauge!("crank_mcp_active_sessions").set(count as f64);
|
||||
}
|
||||
|
||||
pub fn record_tool_invocation(
|
||||
source: InvocationSource,
|
||||
outcome: ToolOutcome,
|
||||
error_kind: ToolErrorKind,
|
||||
duration: Duration,
|
||||
) {
|
||||
metrics::counter!(
|
||||
"crank_tool_invocations_total",
|
||||
"source" => source.as_str(),
|
||||
"outcome" => outcome.as_str(),
|
||||
"error_kind" => error_kind.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
metrics::histogram!(
|
||||
"crank_tool_invocation_duration_seconds",
|
||||
"source" => source.as_str(),
|
||||
"outcome" => outcome.as_str()
|
||||
)
|
||||
.record(duration.as_secs_f64());
|
||||
}
|
||||
|
||||
pub fn record_upstream_request(
|
||||
operation_kind: UpstreamOperationKind,
|
||||
outcome: UpstreamOutcome,
|
||||
duration: Duration,
|
||||
) {
|
||||
metrics::counter!(
|
||||
"crank_upstream_requests_total",
|
||||
"operation_kind" => operation_kind.as_str(),
|
||||
"outcome" => outcome.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
metrics::histogram!(
|
||||
"crank_upstream_request_duration_seconds",
|
||||
"operation_kind" => operation_kind.as_str(),
|
||||
"outcome" => outcome.as_str()
|
||||
)
|
||||
.record(duration.as_secs_f64());
|
||||
}
|
||||
|
||||
pub struct ToolInvocationMetrics {
|
||||
source: InvocationSource,
|
||||
started_at: Option<Instant>,
|
||||
}
|
||||
|
||||
impl ToolInvocationMetrics {
|
||||
pub fn start(source: InvocationSource) -> Self {
|
||||
Self {
|
||||
source,
|
||||
started_at: Some(Instant::now()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn complete(mut self, outcome: ToolOutcome, error_kind: ToolErrorKind) {
|
||||
self.finish(outcome, error_kind);
|
||||
}
|
||||
|
||||
fn finish(&mut self, outcome: ToolOutcome, error_kind: ToolErrorKind) {
|
||||
if let Some(started_at) = self.started_at.take() {
|
||||
record_tool_invocation(self.source, outcome, error_kind, started_at.elapsed());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ToolInvocationMetrics {
|
||||
fn drop(&mut self) {
|
||||
self.finish(ToolOutcome::Aborted, ToolErrorKind::Aborted);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UpstreamRequestMetrics {
|
||||
operation_kind: UpstreamOperationKind,
|
||||
started_at: Option<Instant>,
|
||||
}
|
||||
|
||||
impl UpstreamRequestMetrics {
|
||||
pub fn start(operation_kind: UpstreamOperationKind) -> Self {
|
||||
Self {
|
||||
operation_kind,
|
||||
started_at: Some(Instant::now()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn complete(mut self, outcome: UpstreamOutcome) {
|
||||
self.finish(outcome);
|
||||
}
|
||||
|
||||
fn finish(&mut self, outcome: UpstreamOutcome) {
|
||||
if let Some(started_at) = self.started_at.take() {
|
||||
record_upstream_request(self.operation_kind, outcome, started_at.elapsed());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for UpstreamRequestMetrics {
|
||||
fn drop(&mut self) {
|
||||
self.finish(UpstreamOutcome::Aborted);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_limit_rejection(stage: LimitStage) {
|
||||
metrics::counter!(
|
||||
"crank_runtime_limit_rejections_total",
|
||||
"stage" => stage.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
}
|
||||
|
||||
pub fn record_cache_outcome(outcome: CacheOutcome) {
|
||||
metrics::counter!(
|
||||
"crank_runtime_cache_total",
|
||||
"outcome" => outcome.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
}
|
||||
|
||||
pub fn record_idempotency_outcome(outcome: IdempotencyOutcome) {
|
||||
metrics::counter!(
|
||||
"crank_idempotency_total",
|
||||
"outcome" => outcome.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
}
|
||||
|
||||
pub fn record_confirmation_outcome(outcome: ConfirmationOutcome) {
|
||||
metrics::counter!(
|
||||
"crank_confirmation_total",
|
||||
"outcome" => outcome.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
}
|
||||
|
||||
pub fn set_db_pool_connections(state: DbPoolState, count: u32) {
|
||||
metrics::gauge!(
|
||||
"crank_db_pool_connections",
|
||||
"state" => state.as_str()
|
||||
)
|
||||
.set(f64::from(count));
|
||||
}
|
||||
|
||||
pub fn set_catalog(tool_count: usize, estimated_context_tokens: usize, warnings: usize) {
|
||||
metrics::gauge!("crank_catalog_tools").set(tool_count as f64);
|
||||
metrics::gauge!("crank_catalog_estimated_context_tokens").set(estimated_context_tokens as f64);
|
||||
metrics::gauge!("crank_catalog_warnings").set(warnings as f64);
|
||||
}
|
||||
|
||||
pub fn record_invocation_history_lost() {
|
||||
metrics::counter!("crank_invocation_history_lost_total").increment(1);
|
||||
}
|
||||
|
||||
pub fn record_export_failure(signal: SignalType, exporter: Exporter) {
|
||||
metrics::counter!(
|
||||
"crank_telemetry_export_failures_total",
|
||||
"signal_type" => signal.as_str(),
|
||||
"exporter" => exporter.as_str()
|
||||
)
|
||||
.increment(1);
|
||||
}
|
||||
|
||||
pub fn initialize_gauges() {
|
||||
metrics::gauge!("crank_http_inflight").set(0.0);
|
||||
metrics::gauge!("crank_mcp_active_sessions").set(0.0);
|
||||
metrics::gauge!("crank_mcp_active_streams").set(0.0);
|
||||
metrics::gauge!("crank_runtime_inflight").set(0.0);
|
||||
set_db_pool_connections(DbPoolState::Idle, 0);
|
||||
set_db_pool_connections(DbPoolState::Used, 0);
|
||||
set_catalog(0, 0, 0);
|
||||
}
|
||||
|
||||
pub struct InFlightGuard {
|
||||
gauge: Gauge,
|
||||
}
|
||||
|
||||
impl InFlightGuard {
|
||||
pub fn http() -> Self {
|
||||
Self::increment(metrics::gauge!("crank_http_inflight"))
|
||||
}
|
||||
|
||||
pub fn runtime() -> Self {
|
||||
Self::increment(metrics::gauge!("crank_runtime_inflight"))
|
||||
}
|
||||
|
||||
pub fn mcp_stream() -> Self {
|
||||
Self::increment(metrics::gauge!("crank_mcp_active_streams"))
|
||||
}
|
||||
|
||||
fn increment(gauge: Gauge) -> Self {
|
||||
gauge.increment(1.0);
|
||||
Self { gauge }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for InFlightGuard {
|
||||
fn drop(&mut self) {
|
||||
self.gauge.decrement(1.0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user