наблюдаемость: ввести безопасный контракт метрик
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
@@ -5,6 +5,7 @@ use std::{
|
||||
|
||||
use axum::body::to_bytes;
|
||||
use crank_core::InvocationStatus;
|
||||
use crank_metrics::{McpMethod, McpOutcome, McpResponseMode};
|
||||
use crank_observability::{
|
||||
ObservabilityConfig, OperationalIncident, RedactionLimits, ServiceIdentity,
|
||||
operational_incident_total,
|
||||
@@ -16,16 +17,20 @@ use serde_json::{Value, json};
|
||||
use tracing_subscriber::fmt::MakeWriter;
|
||||
|
||||
use super::{
|
||||
ResponseMode, metrics::normalized_mcp_method, observe_invocation_history_outcome,
|
||||
tool_error_response,
|
||||
ResponseMode,
|
||||
metrics::{McpRequestMetrics, normalized_mcp_method},
|
||||
observe_invocation_history_outcome, tool_error_response,
|
||||
};
|
||||
use crate::jsonrpc::CURRENT_PROTOCOL_VERSION;
|
||||
use crate::jsonrpc::{CURRENT_PROTOCOL_VERSION, jsonrpc_error};
|
||||
use crate::tool_error::generic_tool_error_contract;
|
||||
use crate::transport::transport_response;
|
||||
|
||||
#[tokio::test]
|
||||
async fn tool_error_response_includes_structured_context() {
|
||||
let message = json!({"jsonrpc": "2.0", "id": "req-1", "method": "tools/call"});
|
||||
let mut request_metrics = McpRequestMetrics::new(&message);
|
||||
let response = tool_error_response(
|
||||
&json!({"jsonrpc": "2.0", "id": "req-1"}),
|
||||
&message,
|
||||
ResponseMode::Json,
|
||||
CURRENT_PROTOCOL_VERSION,
|
||||
generic_tool_error_contract(
|
||||
@@ -36,6 +41,12 @@ async fn tool_error_response_includes_structured_context() {
|
||||
Some("Проверьте параметры вызова инструмента."),
|
||||
),
|
||||
);
|
||||
assert_eq!(
|
||||
response.extensions().get::<McpOutcome>(),
|
||||
Some(&McpOutcome::ToolError)
|
||||
);
|
||||
request_metrics.complete(&response);
|
||||
assert_eq!(request_metrics.outcome(), McpOutcome::ToolError);
|
||||
|
||||
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
|
||||
let payload: Value = serde_json::from_slice(&body).unwrap();
|
||||
@@ -53,6 +64,67 @@ async fn tool_error_response_includes_structured_context() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jsonrpc_error_over_http_200_is_not_counted_as_success() {
|
||||
let message = json!({"jsonrpc": "2.0", "id": 1, "method": "unsupported"});
|
||||
let mut request_metrics = McpRequestMetrics::new(&message);
|
||||
let response = transport_response(
|
||||
axum::http::StatusCode::OK,
|
||||
jsonrpc_error(json!(1), -32601, "unsupported"),
|
||||
ResponseMode::Json,
|
||||
None,
|
||||
Some(CURRENT_PROTOCOL_VERSION),
|
||||
);
|
||||
|
||||
request_metrics.complete(&response);
|
||||
|
||||
assert_eq!(request_metrics.outcome(), McpOutcome::JsonRpcError);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metric_uses_actual_json_response_when_sse_request_falls_back() {
|
||||
let message = json!({"jsonrpc": "2.0", "id": 1, "method": "unsupported"});
|
||||
let mut request_metrics = McpRequestMetrics::new(&message);
|
||||
let response = transport_response(
|
||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
||||
jsonrpc_error(json!(1), -32603, "internal error"),
|
||||
ResponseMode::Json,
|
||||
None,
|
||||
Some(CURRENT_PROTOCOL_VERSION),
|
||||
);
|
||||
|
||||
request_metrics.complete(&response);
|
||||
|
||||
assert_eq!(request_metrics.response_mode(), McpResponseMode::Json);
|
||||
assert_eq!(request_metrics.outcome(), McpOutcome::ServerError);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transport_failure_takes_priority_over_jsonrpc_payload() {
|
||||
let message = json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call"});
|
||||
let mut request_metrics = McpRequestMetrics::new(&message);
|
||||
let response = transport_response(
|
||||
axum::http::StatusCode::TOO_MANY_REQUESTS,
|
||||
jsonrpc_error(json!(1), -32000, "rate limited"),
|
||||
ResponseMode::Json,
|
||||
None,
|
||||
Some(CURRENT_PROTOCOL_VERSION),
|
||||
);
|
||||
|
||||
request_metrics.complete(&response);
|
||||
|
||||
assert_eq!(request_metrics.outcome(), McpOutcome::ClientError);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unfinished_request_is_classified_as_aborted() {
|
||||
let message = json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call"});
|
||||
let request_metrics = McpRequestMetrics::new(&message);
|
||||
|
||||
assert_eq!(request_metrics.response_mode(), McpResponseMode::Unknown);
|
||||
assert_eq!(request_metrics.outcome(), McpOutcome::Aborted);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emits_bounded_history_loss_incident() {
|
||||
let writer = SharedLogWriter::default();
|
||||
@@ -96,19 +168,19 @@ fn emits_bounded_history_loss_incident() {
|
||||
fn mcp_metric_method_is_always_from_a_closed_set() {
|
||||
assert_eq!(
|
||||
normalized_mcp_method(&json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call"})),
|
||||
"tools_call"
|
||||
McpMethod::ToolsCall
|
||||
);
|
||||
assert_eq!(
|
||||
normalized_mcp_method(
|
||||
&json!({"jsonrpc": "2.0", "id": 2, "method": "customer-controlled-method"})
|
||||
),
|
||||
"unsupported"
|
||||
McpMethod::Unsupported
|
||||
);
|
||||
assert_eq!(
|
||||
normalized_mcp_method(
|
||||
&json!({"jsonrpc": "2.0", "method": "customer-controlled-notification"})
|
||||
),
|
||||
"notification"
|
||||
McpMethod::Notification
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user