//! Закрытые дескрипторы метрик: kind/unit/process/service, домены labels и //! `METRIC_SCHEMA` (union-таблица метадат, не бюджет процесса). use super::routes::{BOTH, HTTP_ROUTE_DOMAIN, MCP}; use crate::labels::{ CacheOutcome, ConfirmationOutcome, DbPoolState, Exporter, HttpMethod, HttpStatusClass, IdempotencyOutcome, InvocationSource, LimitStage, McpMethod, McpOutcome, McpResponseMode, SignalType, ToolErrorKind, ToolOutcome, UpstreamOperationKind, UpstreamOutcome, }; pub const METRIC_SCHEMA_VERSION: u32 = 2; pub const MAX_METRIC_FAMILIES: usize = 128; pub const MAX_PRODUCT_LABELS_PER_FAMILY: usize = 4; pub const MAX_LOGICAL_SERIES_PER_PROCESS: usize = 6_285; pub const MAX_RENDERED_SERIES_PER_PROCESS: usize = 19_515; pub const MAX_EXEMPLAR_SLOTS_PER_PROCESS: usize = 12_348; pub const MAX_EXPOSITION_BYTES: usize = 8 * 1024 * 1024; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum MetricKind { Counter, Gauge, Histogram, } impl MetricKind { pub const fn as_str(self) -> &'static str { match self { Self::Counter => "counter", Self::Gauge => "gauge", Self::Histogram => "histogram", } } } #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum MetricUnit { Count, Seconds, } impl MetricUnit { pub const fn as_str(self) -> &'static str { match self { Self::Count => "count", Self::Seconds => "seconds", } } } #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] pub enum MetricProcess { AdminApi, McpServer, } #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum MetricService { AdminApi, McpServer, } impl MetricService { pub fn parse(value: &str) -> Option { match value { "admin-api" => Some(Self::AdminApi), "mcp-server" => Some(Self::McpServer), _ => None, } } pub const fn process(self) -> MetricProcess { match self { Self::AdminApi => MetricProcess::AdminApi, Self::McpServer => MetricProcess::McpServer, } } } impl MetricProcess { pub const fn as_str(self) -> &'static str { match self { Self::AdminApi => "admin_api", Self::McpServer => "mcp_server", } } pub const fn service(self) -> MetricService { match self { Self::AdminApi => MetricService::AdminApi, Self::McpServer => MetricService::McpServer, } } } #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum LabelClass { ProductClosed, ProcessConstant, } impl LabelClass { pub const fn as_str(self) -> &'static str { match self { Self::ProductClosed => "product_closed", Self::ProcessConstant => "process_constant", } } } #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct LabelDomain { pub name: &'static str, pub class: LabelClass, pub values: &'static [&'static str], } #[derive(Clone, Copy, Debug, PartialEq)] pub struct MetricDefinition { pub name: &'static str, pub kind: MetricKind, pub unit: MetricUnit, pub labels: &'static [&'static str], pub label_domains: &'static [LabelDomain], pub buckets: &'static [f64], pub processes: &'static [MetricProcess], pub max_logical_series: usize, pub max_rendered_series: usize, pub description: &'static str, } impl MetricDefinition { /// `E(m, p)` — exemplar slots contributed by this family inside one process /// projection. Histograms hold `L x (finite_buckets + 1)` latest-per-bucket /// observations; counters/gauges hold none. The multiply is checked: an /// overflowing definition (one that already failed the checked seam) fails /// loudly instead of returning a saturated or truncated count. pub const fn max_exemplar_slots(&self) -> usize { if !matches!(self.kind, MetricKind::Histogram) { return 0; } match self.max_logical_series.checked_mul(self.buckets.len() + 1) { Some(slots) => slots, None => panic!("exemplar slot arithmetic overflowed a validated definition"), } } } pub const DURATION_BUCKETS_SECONDS: &[f64] = &[ 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 30.0, 60.0, ]; pub const PROCESS_CONSTANT_LABELS: &[LabelDomain] = &[ LabelDomain { name: "service", class: LabelClass::ProcessConstant, values: &["admin-api", "mcp-server"], }, LabelDomain { name: "version", class: LabelClass::ProcessConstant, values: &["validated_release_identity"], }, LabelDomain { name: "environment", class: LabelClass::ProcessConstant, values: &["validated_startup_value"], }, ]; const METHODS: &[&str] = HttpMethod::VALUES; const STATUS: &[&str] = HttpStatusClass::VALUES; const MCP_METHODS: &[&str] = McpMethod::VALUES; const RESPONSE_MODES: &[&str] = McpResponseMode::VALUES; const MCP_OUTCOMES: &[&str] = McpOutcome::VALUES; const SOURCES: &[&str] = InvocationSource::VALUES; const TOOL_OUTCOMES: &[&str] = ToolOutcome::VALUES; const TOOL_ERRORS: &[&str] = ToolErrorKind::VALUES; const OPERATION_KINDS: &[&str] = UpstreamOperationKind::VALUES; const UPSTREAM_OUTCOMES: &[&str] = UpstreamOutcome::VALUES; const LIMIT_STAGES: &[&str] = LimitStage::VALUES; const CACHE_OUTCOMES: &[&str] = CacheOutcome::VALUES; const IDEMPOTENCY_OUTCOMES: &[&str] = IdempotencyOutcome::VALUES; const CONFIRMATION_OUTCOMES: &[&str] = ConfirmationOutcome::VALUES; const DB_STATES: &[&str] = DbPoolState::VALUES; const SIGNAL_TYPES: &[&str] = SignalType::VALUES; const EXPORTERS: &[&str] = Exporter::VALUES; const fn domain(name: &'static str, values: &'static [&'static str]) -> LabelDomain { LabelDomain { name, class: LabelClass::ProductClosed, values, } } #[allow( clippy::too_many_arguments, reason = "the canonical descriptor keeps every metric contract dimension explicit" )] const fn metric( name: &'static str, kind: MetricKind, unit: MetricUnit, labels: &'static [&'static str], label_domains: &'static [LabelDomain], buckets: &'static [f64], processes: &'static [MetricProcess], logical: usize, description: &'static str, ) -> MetricDefinition { let rendered = if matches!(kind, MetricKind::Histogram) { logical * (buckets.len() + 3) } else { logical }; MetricDefinition { name, kind, unit, labels, label_domains, buckets, processes, max_logical_series: logical, max_rendered_series: rendered, description, } } /// Union descriptors: the route domain is the whole union and `max_*` are the /// union Cartesian metadata used for structural diagnostics, **not** a process /// budget. Real per-process data lives in the service projections. pub(crate) const METRIC_SCHEMA: &[MetricDefinition] = &[ metric( "crank_http_requests_total", MetricKind::Counter, MetricUnit::Count, &["route", "method", "status_class"], &[ domain("route", HTTP_ROUTE_DOMAIN), domain("method", METHODS), domain("status_class", STATUS), ], &[], BOTH, HTTP_ROUTE_DOMAIN.len() * 10 * 6, "Total HTTP requests.", ), metric( "crank_http_request_duration_seconds", MetricKind::Histogram, MetricUnit::Seconds, &["route", "method"], &[ domain("route", HTTP_ROUTE_DOMAIN), domain("method", METHODS), ], DURATION_BUCKETS_SECONDS, BOTH, HTTP_ROUTE_DOMAIN.len() * 10, "HTTP request duration in seconds.", ), metric( "crank_http_inflight", MetricKind::Gauge, MetricUnit::Count, &[], &[], &[], BOTH, 1, "HTTP requests currently being processed.", ), metric( "crank_mcp_requests_total", MetricKind::Counter, MetricUnit::Count, &["method", "response_mode", "outcome"], &[ domain("method", MCP_METHODS), domain("response_mode", RESPONSE_MODES), domain("outcome", MCP_OUTCOMES), ], &[], MCP, 9 * 3 * 7, "Total MCP JSON-RPC requests.", ), metric( "crank_mcp_request_duration_seconds", MetricKind::Histogram, MetricUnit::Seconds, &["method", "outcome"], &[ domain("method", MCP_METHODS), domain("outcome", MCP_OUTCOMES), ], DURATION_BUCKETS_SECONDS, MCP, 9 * 7, "MCP JSON-RPC request duration in seconds.", ), metric( "crank_mcp_active_sessions", MetricKind::Gauge, MetricUnit::Count, &[], &[], &[], MCP, 1, "Active persisted MCP transport sessions.", ), metric( "crank_mcp_session_metrics_fresh", MetricKind::Gauge, MetricUnit::Count, &[], &[], &[], MCP, 1, "Whether the active-session gauge was refreshed successfully.", ), metric( "crank_mcp_active_streams", MetricKind::Gauge, MetricUnit::Count, &[], &[], &[], MCP, 1, "MCP event streams currently being served.", ), metric( "crank_tool_invocations_total", MetricKind::Counter, MetricUnit::Count, &["source", "outcome", "error_kind"], &[ domain("source", SOURCES), domain("outcome", TOOL_OUTCOMES), domain("error_kind", TOOL_ERRORS), ], &[], BOTH, 3 * 3 * 22, "Total tool invocations.", ), metric( "crank_tool_invocation_duration_seconds", MetricKind::Histogram, MetricUnit::Seconds, &["source", "outcome"], &[domain("source", SOURCES), domain("outcome", TOOL_OUTCOMES)], DURATION_BUCKETS_SECONDS, BOTH, 3 * 3, "Tool invocation duration in seconds.", ), metric( "crank_upstream_requests_total", MetricKind::Counter, MetricUnit::Count, &["operation_kind", "outcome"], &[ domain("operation_kind", OPERATION_KINDS), domain("outcome", UPSTREAM_OUTCOMES), ], &[], BOTH, 13, "Total upstream requests.", ), metric( "crank_upstream_request_duration_seconds", MetricKind::Histogram, MetricUnit::Seconds, &["operation_kind", "outcome"], &[ domain("operation_kind", OPERATION_KINDS), domain("outcome", UPSTREAM_OUTCOMES), ], DURATION_BUCKETS_SECONDS, BOTH, 13, "Upstream request duration in seconds.", ), metric( "crank_runtime_inflight", MetricKind::Gauge, MetricUnit::Count, &[], &[], &[], BOTH, 1, "Runtime executions currently in progress.", ), metric( "crank_runtime_limit_rejections_total", MetricKind::Counter, MetricUnit::Count, &["stage"], &[domain("stage", LIMIT_STAGES)], &[], BOTH, 3, "Runtime executions rejected by a bounded limit.", ), metric( "crank_runtime_cache_total", MetricKind::Counter, MetricUnit::Count, &["outcome"], &[domain("outcome", CACHE_OUTCOMES)], &[], BOTH, 7, "Runtime response cache outcomes.", ), metric( "crank_idempotency_total", MetricKind::Counter, MetricUnit::Count, &["outcome"], &[domain("outcome", IDEMPOTENCY_OUTCOMES)], &[], BOTH, 8, "Runtime idempotency outcomes.", ), metric( "crank_confirmation_total", MetricKind::Counter, MetricUnit::Count, &["outcome"], &[domain("outcome", CONFIRMATION_OUTCOMES)], &[], BOTH, 5, "Runtime confirmation outcomes.", ), metric( "crank_db_pool_connections", MetricKind::Gauge, MetricUnit::Count, &["state"], &[domain("state", DB_STATES)], &[], BOTH, 2, "PostgreSQL pool connections by state.", ), metric( "crank_catalog_tools", MetricKind::Gauge, MetricUnit::Count, &[], &[], &[], MCP, 1, "Tools in the current published catalog.", ), metric( "crank_catalog_estimated_context_tokens", MetricKind::Gauge, MetricUnit::Count, &[], &[], &[], MCP, 1, "Estimated context tokens in the current published catalog.", ), metric( "crank_catalog_warnings", MetricKind::Gauge, MetricUnit::Count, &[], &[], &[], MCP, 1, "Warnings in the current published catalog.", ), metric( "crank_invocation_history_lost_total", MetricKind::Counter, MetricUnit::Count, &[], &[], &[], BOTH, 1, "Invocation history records lost after an action completed.", ), metric( "crank_telemetry_export_failures_total", MetricKind::Counter, MetricUnit::Count, &["signal_type", "exporter"], &[ domain("signal_type", SIGNAL_TYPES), domain("exporter", EXPORTERS), ], &[], BOTH, 4, "Telemetry export failures.", ), ]; /// Union descriptors (`metadata`, not a budget). pub const fn metric_schema() -> &'static [MetricDefinition] { METRIC_SCHEMA }