use crate::routing::{RequestId, RouteOutcome, SessionId};
use rootle_provider::ErrorKind;
use rootle_trace::EventKind;
use serde_json::{Map, Value, json};
const PROVIDER: &str = "stdio";
pub(crate) fn error_kind_label(kind: ErrorKind) -> &'static str {
match kind {
ErrorKind::Auth => "auth",
ErrorKind::RateLimited => "rate_limited",
ErrorKind::NotFound => "not_found",
ErrorKind::Network => "network",
ErrorKind::Timeout => "timeout",
ErrorKind::Provider => "provider",
ErrorKind::Other => "other",
}
}
pub(crate) fn lifecycle<F>(stage: &str, fields: F)
where
F: FnOnce(&mut Map<String, Value>),
{
rootle_trace::record_with(EventKind::ProviderLifecycle, || {
let mut map = Map::new();
map.insert("provider".into(), json!(PROVIDER));
map.insert("stage".into(), json!(stage));
fields(&mut map);
Value::Object(map)
});
}
pub(crate) fn tx_request(
session: SessionId,
id: RequestId,
method: &str,
bytes: u64,
written: bool,
) {
rootle_trace::record_with(EventKind::RpcMessage, || {
json!({
"provider": PROVIDER,
"session": session.value(),
"frame": "request",
"dir": "tx",
"id": id.wire(),
"method": method,
"bytes": bytes,
"written": written,
})
});
}
pub(crate) fn tx_cancel(session: SessionId, id: u64, bytes: u64, written: bool) {
rootle_trace::record_with(EventKind::RpcMessage, || {
json!({
"provider": PROVIDER,
"session": session.value(),
"frame": "cancel",
"dir": "tx",
"id": id,
"method": "$/cancelRequest",
"bytes": bytes,
"written": written,
})
});
}
pub(crate) fn rx_response(
session: SessionId,
id: RequestId,
bytes: u64,
outcome: RouteOutcome,
error_kind: Option<&'static str>,
) {
rootle_trace::record_with(EventKind::RpcMessage, || {
json!({
"provider": PROVIDER,
"session": session.value(),
"frame": "response",
"dir": "rx",
"id": id.wire(),
"bytes": bytes,
"routed": outcome.label(),
"error_kind": error_kind,
})
});
}
pub(crate) fn rx_partial(session: SessionId, id: RequestId, bytes: u64, outcome: RouteOutcome) {
rootle_trace::record_with(EventKind::RpcMessage, || {
json!({
"provider": PROVIDER,
"session": session.value(),
"frame": "partial",
"dir": "rx",
"id": id.wire(),
"bytes": bytes,
"routed": outcome.label(),
})
});
}
pub(crate) fn rx_notification(session: SessionId, method: Option<&str>, bytes: u64) {
rootle_trace::record_with(EventKind::RpcMessage, || {
json!({
"provider": PROVIDER,
"session": session.value(),
"frame": "notification",
"dir": "rx",
"method": method,
"bytes": bytes,
})
});
}
pub(crate) fn rx_malformed(session: SessionId, bytes: u64) {
rootle_trace::record_with(EventKind::RpcMessage, || {
json!({
"provider": PROVIDER,
"session": session.value(),
"frame": "malformed",
"dir": "rx",
"bytes": bytes,
})
});
}
pub(crate) fn request_expired(session: SessionId, id: RequestId, timeout_ms: u64) {
rootle_trace::record_with(EventKind::RpcMessage, || {
json!({
"provider": PROVIDER,
"session": session.value(),
"frame": "request",
"id": id.wire(),
"outcome": "timeout",
"timeout_ms": timeout_ms,
})
});
}