fix: harden typed metrics review findings

This commit is contained in:
bsodfather committed 2026-08-14 13:50:04 +03:00
1 parent 996a5461de
commit b7face0e94
30 files changed
+722 -382

No files matched your search

+44 -97
View File
@@ -1,5 +1,11 @@
use std::collections::BTreeSet;
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 = 1;
pub const MAX_METRIC_FAMILIES: usize = 128;
pub const MAX_PRODUCT_LABELS_PER_FAMILY: usize = 4;
@@ -59,6 +65,13 @@ impl MetricService {
_ => None,
}
}
pub const fn process(self) -> MetricProcess {
match self {
Self::AdminApi => MetricProcess::AdminApi,
Self::McpServer => MetricProcess::McpServer,
}
}
}
impl MetricProcess {
@@ -208,103 +221,23 @@ pub const HTTP_ROUTE_DOMAIN: &[&str] = &[
"/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}",
"/v1/{workspace_slug}/{agent_slug}/approvals/{approval_id}/deny",
];
const METHODS: &[&str] = &[
"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD", "CONNECT", "TRACE", "OTHER",
];
const STATUS: &[&str] = &["1xx", "2xx", "3xx", "4xx", "5xx", "other"];
const MCP_METHODS: &[&str] = &[
"initialize",
"initialized",
"ping",
"tools_list",
"tools_call",
"notification",
"unsupported",
"response",
"invalid",
];
const RESPONSE_MODES: &[&str] = &["json", "sse", "unknown"];
const MCP_OUTCOMES: &[&str] = &[
"success",
"client_error",
"server_error",
"jsonrpc_error",
"tool_error",
"aborted",
"other",
];
const SOURCES: &[&str] = &["internal", "admin_test_run", "agent_tool_call"];
const TOOL_OUTCOMES: &[&str] = &["success", "error", "aborted"];
const TOOL_ERRORS: &[&str] = &[
"none",
"schema",
"mapping",
"rest_adapter",
"protocol_adapter",
"unsupported_protocol",
"unsupported_execution_mode",
"concurrency_limit",
"invalid_prepared_request",
"confirmation_required",
"invalid_confirmation_token",
"confirmation_store",
"idempotency_store",
"idempotency_in_progress",
"idempotency_conflict",
"idempotency_outcome_unknown",
"missing_auth_profile",
"missing_secret",
"missing_secret_version",
"invalid_auth_secret",
"secret_crypto",
"aborted",
];
const OPERATION_KINDS: &[&str] = &["rest"];
const UPSTREAM_OUTCOMES: &[&str] = &[
"success",
"client_error",
"server_error",
"unexpected_status",
"timeout",
"transport_error",
"response_too_large",
"rejected",
"window_expired",
"invalid_response",
"invalid_request",
"configuration",
"aborted",
];
const LIMIT_STAGES: &[&str] = &["concurrency", "rate_limit", "mcp_stream"];
const CACHE_OUTCOMES: &[&str] = &[
"hit",
"miss",
"read_error",
"decode_error",
"evict_error",
"stored",
"write_error",
];
const IDEMPOTENCY_OUTCOMES: &[&str] = &[
"execute",
"replay",
"completed",
"conflict",
"in_progress",
"outcome_unknown",
"store_unavailable",
"error",
];
const CONFIRMATION_OUTCOMES: &[&str] = &[
"approved",
"required",
"invalid_token",
"store_unavailable",
"error",
];
const DB_STATES: &[&str] = &["idle", "used"];
const SIGNAL_TYPES: &[&str] = &["trace", "invocation_history"];
const EXPORTERS: &[&str] = &["otlp", "postgres"];
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 {
@@ -635,6 +568,14 @@ pub const fn metric_schema() -> &'static [MetricDefinition] {
METRIC_SCHEMA
}
pub fn metric_schema_for_service(
service: MetricService,
) -> impl Iterator<Item = &'static MetricDefinition> {
METRIC_SCHEMA
.iter()
.filter(move |definition| definition.processes.contains(&service.process()))
}
pub fn schema_budget() -> Result<SchemaBudget, SchemaError> {
validate_schema(METRIC_SCHEMA)
}
@@ -745,8 +686,14 @@ fn valid_metric_name(name: &str) -> bool {
fn valid_label_name(name: &str) -> bool {
!name.is_empty()
&& name.len() <= 64
&& !name.starts_with("__")
&& name
.bytes()
.next()
.is_some_and(|byte| byte.is_ascii_lowercase() || byte == b'_')
&& name
.bytes()
.skip(1)
.all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'_')
}