core: type observability timestamps
This commit is contained in:
@@ -4174,7 +4174,7 @@ impl AdminService {
|
|||||||
error_kind: request.error_kind,
|
error_kind: request.error_kind,
|
||||||
request_preview: request.request_preview,
|
request_preview: request.request_preview,
|
||||||
response_preview: request.response_preview,
|
response_preview: request.response_preview,
|
||||||
created_at: now_string()?,
|
created_at: OffsetDateTime::now_utc(),
|
||||||
};
|
};
|
||||||
|
|
||||||
self.registry
|
self.registry
|
||||||
|
|||||||
@@ -1821,9 +1821,7 @@ async fn persist_invocation(
|
|||||||
tool: &PublishedAgentTool,
|
tool: &PublishedAgentTool,
|
||||||
record: InvocationRecord<'_>,
|
record: InvocationRecord<'_>,
|
||||||
) -> Result<(), crank_registry::RegistryError> {
|
) -> Result<(), crank_registry::RegistryError> {
|
||||||
let created_at = OffsetDateTime::now_utc()
|
let created_at = OffsetDateTime::now_utc();
|
||||||
.format(&Rfc3339)
|
|
||||||
.unwrap_or_else(|_| "1970-01-01T00:00:00Z".to_owned());
|
|
||||||
let duration_ms = u64::try_from(record.duration.as_millis()).unwrap_or(u64::MAX);
|
let duration_ms = u64::try_from(record.duration.as_millis()).unwrap_or(u64::MAX);
|
||||||
let log = InvocationLog {
|
let log = InvocationLog {
|
||||||
id: InvocationLogId::new(format!("log_{}", uuid::Uuid::now_v7().simple())),
|
id: InvocationLogId::new(format!("log_{}", uuid::Uuid::now_v7().simple())),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
use time::OffsetDateTime;
|
||||||
|
|
||||||
use crate::{AgentId, OperationId, WorkspaceId};
|
use crate::{AgentId, OperationId, WorkspaceId};
|
||||||
|
|
||||||
@@ -63,7 +64,8 @@ pub struct InvocationLog {
|
|||||||
pub error_kind: Option<String>,
|
pub error_kind: Option<String>,
|
||||||
pub request_preview: Value,
|
pub request_preview: Value,
|
||||||
pub response_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)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
@@ -79,3 +81,52 @@ pub struct UsageRollup {
|
|||||||
pub p95_ms: u64,
|
pub p95_ms: u64,
|
||||||
pub p99_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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -130,7 +130,8 @@ pub struct UsageRollupRecord {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct UsageTimelinePoint {
|
pub struct UsageTimelinePoint {
|
||||||
pub bucket_start: String,
|
#[serde(with = "time::serde::rfc3339")]
|
||||||
|
pub bucket_start: OffsetDateTime,
|
||||||
pub calls_ok: u64,
|
pub calls_ok: u64,
|
||||||
pub calls_error: u64,
|
pub calls_error: u64,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2496,7 +2496,7 @@ mod tests {
|
|||||||
error_kind: None,
|
error_kind: None,
|
||||||
request_preview: json!({"input":"value"}),
|
request_preview: json!({"input":"value"}),
|
||||||
response_preview: json!({"ok":true}),
|
response_preview: json!({"ok":true}),
|
||||||
created_at: created_at.to_owned(),
|
created_at: timestamp(created_at),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ impl PostgresRegistry {
|
|||||||
.bind(&request.log.error_kind)
|
.bind(&request.log.error_kind)
|
||||||
.bind(Json(request.log.request_preview.clone()))
|
.bind(Json(request.log.request_preview.clone()))
|
||||||
.bind(Json(request.log.response_preview.clone()))
|
.bind(Json(request.log.response_preview.clone()))
|
||||||
.bind(&request.log.created_at)
|
.bind(request.log.created_at)
|
||||||
.execute(&self.pool)
|
.execute(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ impl PostgresRegistry {
|
|||||||
l.error_kind,
|
l.error_kind,
|
||||||
l.request_preview_json,
|
l.request_preview_json,
|
||||||
l.response_preview_json,
|
l.response_preview_json,
|
||||||
to_char(l.created_at at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"') as created_at,
|
l.created_at as created_at,
|
||||||
o.name as operation_name,
|
o.name as operation_name,
|
||||||
o.display_name as operation_display_name,
|
o.display_name as operation_display_name,
|
||||||
a.slug as agent_slug,
|
a.slug as agent_slug,
|
||||||
@@ -147,7 +147,7 @@ impl PostgresRegistry {
|
|||||||
l.error_kind,
|
l.error_kind,
|
||||||
l.request_preview_json,
|
l.request_preview_json,
|
||||||
l.response_preview_json,
|
l.response_preview_json,
|
||||||
to_char(l.created_at at time zone 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"') as created_at,
|
l.created_at as created_at,
|
||||||
o.name as operation_name,
|
o.name as operation_name,
|
||||||
o.display_name as operation_display_name,
|
o.display_name as operation_display_name,
|
||||||
a.slug as agent_slug,
|
a.slug as agent_slug,
|
||||||
@@ -229,7 +229,7 @@ impl PostgresRegistry {
|
|||||||
let bucket = usage_bucket_sql(query.bucket);
|
let bucket = usage_bucket_sql(query.bucket);
|
||||||
let sql = format!(
|
let sql = format!(
|
||||||
"select
|
"select
|
||||||
to_char(date_trunc('{bucket}', created_at at time zone 'UTC'), 'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') as bucket_start,
|
(date_trunc('{bucket}', created_at at time zone 'UTC') at time zone 'UTC') as bucket_start,
|
||||||
count(*) filter (where status = 'ok')::bigint as calls_ok,
|
count(*) filter (where status = 'ok')::bigint as calls_ok,
|
||||||
count(*) filter (where status = 'error')::bigint as calls_error
|
count(*) filter (where status = 'error')::bigint as calls_error
|
||||||
from invocation_logs
|
from invocation_logs
|
||||||
|
|||||||
Reference in New Issue
Block a user