core: type observability timestamps

This commit is contained in:
a.tolmachev
2026-04-19 07:53:15 +00:00
parent 153f9cb108
commit 899440fd8a
6 changed files with 61 additions and 11 deletions
+52 -1
View File
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::OffsetDateTime;
use crate::{AgentId, OperationId, WorkspaceId};
@@ -63,7 +64,8 @@ pub struct InvocationLog {
pub error_kind: Option<String>,
pub request_preview: Value,
pub response_preview: Value,
pub created_at: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -79,3 +81,52 @@ pub struct UsageRollup {
pub p95_ms: u64,
pub p99_ms: u64,
}
#[cfg(test)]
mod tests {
use serde_json::json;
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use super::{InvocationLevel, InvocationLog, InvocationSource, InvocationStatus, UsagePeriod};
use crate::{AgentId, OperationId, WorkspaceId, ids::InvocationLogId};
fn timestamp(value: &str) -> OffsetDateTime {
OffsetDateTime::parse(value, &Rfc3339).unwrap()
}
#[test]
fn invocation_log_serializes_timestamps_as_rfc3339() {
let log = InvocationLog {
id: InvocationLogId::new("log_01"),
workspace_id: WorkspaceId::new("ws_01"),
agent_id: Some(AgentId::new("agent_01")),
operation_id: OperationId::new("op_01"),
source: InvocationSource::AdminTestRun,
level: InvocationLevel::Info,
status: InvocationStatus::Ok,
tool_name: "create_lead".to_owned(),
message: "ok".to_owned(),
request_id: Some("req_01".to_owned()),
status_code: Some(200),
duration_ms: 123,
error_kind: None,
request_preview: json!({"input": "value"}),
response_preview: json!({"ok": true}),
created_at: timestamp("2026-04-19T12:34:56Z"),
};
let value = serde_json::to_value(&log).unwrap();
assert_eq!(value["created_at"], "2026-04-19T12:34:56Z");
assert_eq!(value["source"], "admin_test_run");
assert_eq!(value["level"], "info");
assert_eq!(value["status"], "ok");
}
#[test]
fn usage_period_serializes_compact_labels() {
let value = serde_json::to_value(UsagePeriod::Last7Days).unwrap();
assert_eq!(value, "7d");
}
}