use std::{
fmt, io,
time::{Duration, Instant},
};
use ic_host_artifacts::batch::{BatchLabelError, validate_labels};
use ic_platform_pocket_ic::{CanisterLogRecord, CanisterStatusResult, ClientError, PocketIc};
use ic_platform_types::principal::ExternalPrincipal as Principal;
pub const DEFAULT_CANISTER_LOG_RECORD_LIMIT: usize = 32;
pub const DEFAULT_CANISTER_LOG_BYTE_LIMIT: usize = 16 * 1024;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CanisterLogRenderLimits {
pub record_limit: usize,
pub byte_limit: usize,
}
impl Default for CanisterLogRenderLimits {
fn default() -> Self {
Self {
record_limit: DEFAULT_CANISTER_LOG_RECORD_LIMIT,
byte_limit: DEFAULT_CANISTER_LOG_BYTE_LIMIT,
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct CanisterDiagnosticsRequest {
pub canister_id: Principal,
pub status_sender: Principal,
pub log_sender: Principal,
pub log_limits: CanisterLogRenderLimits,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LabeledCanisterDiagnosticsRequest {
pub label: String,
pub request: CanisterDiagnosticsRequest,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CanisterDiagnosticLogRecord {
index: u64,
timestamp_nanos: u64,
content: String,
original_content_bytes: usize,
omitted_content_bytes: usize,
}
impl CanisterDiagnosticLogRecord {
#[must_use]
pub const fn index(&self) -> u64 {
self.index
}
#[must_use]
pub const fn timestamp_nanos(&self) -> u64 {
self.timestamp_nanos
}
#[must_use]
pub fn content(&self) -> &str {
&self.content
}
#[must_use]
pub const fn original_content_bytes(&self) -> usize {
self.original_content_bytes
}
#[must_use]
pub const fn omitted_content_bytes(&self) -> usize {
self.omitted_content_bytes
}
#[must_use]
pub const fn was_truncated(&self) -> bool {
self.omitted_content_bytes != 0
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CanisterDiagnosticLogs {
records: Vec<CanisterDiagnosticLogRecord>,
total_records: usize,
total_content_bytes: usize,
omitted_content_bytes: usize,
}
impl CanisterDiagnosticLogs {
#[must_use]
pub fn records(&self) -> &[CanisterDiagnosticLogRecord] {
&self.records
}
#[must_use]
pub const fn total_records(&self) -> usize {
self.total_records
}
#[must_use]
pub const fn total_content_bytes(&self) -> usize {
self.total_content_bytes
}
#[must_use]
pub const fn omitted_records(&self) -> usize {
self.total_records - self.records.len()
}
#[must_use]
pub const fn omitted_content_bytes(&self) -> usize {
self.omitted_content_bytes
}
#[must_use]
pub const fn was_truncated(&self) -> bool {
self.omitted_records() != 0 || self.omitted_content_bytes != 0
}
}
impl fmt::Display for CanisterDiagnosticLogs {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.records.is_empty() {
if self.total_records == 0 {
formatter.write_str("<empty>")?;
} else {
formatter.write_str("<no retained records>")?;
}
} else {
for (position, record) in self.records.iter().enumerate() {
if position != 0 {
formatter.write_str(", ")?;
}
write!(
formatter,
"[{}@{}]={:?}",
record.index, record.timestamp_nanos, record.content
)?;
if record.was_truncated() {
write!(
formatter,
" (truncated {} bytes)",
record.omitted_content_bytes
)?;
}
}
}
if self.was_truncated() {
write!(
formatter,
"; truncated omitted_records={} omitted_content_bytes={}",
self.omitted_records(),
self.omitted_content_bytes
)?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct CanisterDiagnosticsReport {
pub request: CanisterDiagnosticsRequest,
pub status: Result<CanisterStatusResult, ClientError>,
pub logs: Result<CanisterDiagnosticLogs, ClientError>,
}
impl CanisterDiagnosticsReport {
#[must_use]
pub const fn is_success(&self) -> bool {
self.status.is_ok() && self.logs.is_ok()
}
}
#[derive(Debug)]
pub struct CanisterDiagnosticsBatchEntry {
pub label: String,
pub report: CanisterDiagnosticsReport,
pub entry_elapsed: Duration,
}
impl CanisterDiagnosticsBatchEntry {
#[must_use]
pub const fn is_success(&self) -> bool {
self.report.is_success()
}
}
#[derive(Debug, Default)]
pub struct CanisterDiagnosticsBatchReport {
pub entries: Vec<CanisterDiagnosticsBatchEntry>,
pub total: Duration,
}
impl CanisterDiagnosticsBatchReport {
pub fn failures(&self) -> impl Iterator<Item = &CanisterDiagnosticsBatchEntry> {
self.entries.iter().filter(|entry| !entry.is_success())
}
#[must_use]
pub fn is_success(&self) -> bool {
self.entries
.iter()
.all(CanisterDiagnosticsBatchEntry::is_success)
}
}
impl fmt::Display for CanisterDiagnosticsBatchReport {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"diagnostics={} total={:?}",
self.entries.len(),
self.total
)?;
for entry in &self.entries {
write!(
formatter,
"; label={:?} elapsed={:?} {}",
entry.label, entry.entry_elapsed, entry.report
)?;
}
Ok(())
}
}
impl fmt::Display for CanisterDiagnosticsReport {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"canister={} status_sender={} status=",
self.request.canister_id, self.request.status_sender
)?;
match &self.status {
Ok(status) => write!(
formatter,
"ok(state={:?} version={} controllers={} module_hash_bytes={} memory_bytes={} cycles={})",
status.status,
status.version,
status.settings.controllers.len(),
status.module_hash.as_ref().map_or(0, Vec::len),
status.memory_size,
status.cycles,
),
Err(failure) => write!(formatter, "<{failure}>"),
}?;
write!(formatter, " log_sender={} logs=", self.request.log_sender)?;
match &self.logs {
Err(failure) => write!(formatter, "<{failure}>")?,
Ok(logs) => write!(formatter, "{logs}")?,
}
Ok(())
}
}
pub trait PocketIcDiagnosticsExt {
fn collect_canister_diagnostics(
&self,
request: CanisterDiagnosticsRequest,
) -> CanisterDiagnosticsReport;
fn write_canister_diagnostics(
&self,
request: CanisterDiagnosticsRequest,
context: &str,
writer: &mut dyn io::Write,
) -> io::Result<()> {
let report = self.collect_canister_diagnostics(request);
writeln!(writer, "{context}: {report}")
}
fn collect_canister_diagnostics_batch(
&self,
requests: &[LabeledCanisterDiagnosticsRequest],
) -> Result<CanisterDiagnosticsBatchReport, BatchLabelError> {
validate_labels(requests.iter().map(|request| request.label.as_str()))?;
let started = Instant::now();
let entries = requests
.iter()
.map(|labeled| {
let entry_started = Instant::now();
let request = labeled.request;
let report = self.collect_canister_diagnostics(request);
CanisterDiagnosticsBatchEntry {
label: labeled.label.clone(),
report,
entry_elapsed: entry_started.elapsed(),
}
})
.collect();
Ok(CanisterDiagnosticsBatchReport {
entries,
total: started.elapsed(),
})
}
}
impl PocketIcDiagnosticsExt for PocketIc {
fn collect_canister_diagnostics(
&self,
request: CanisterDiagnosticsRequest,
) -> CanisterDiagnosticsReport {
let status = self.canister_status(request.canister_id, Some(request.status_sender));
let logs = self
.fetch_canister_logs(request.canister_id, request.log_sender)
.map(|records| render_log_records(records, request.log_limits));
CanisterDiagnosticsReport {
request,
status,
logs,
}
}
}
fn render_log_records(
records: Vec<CanisterLogRecord>,
limits: CanisterLogRenderLimits,
) -> CanisterDiagnosticLogs {
let total_records = records.len();
let mut total_content_bytes = 0usize;
let mut rendered = Vec::with_capacity(total_records.min(limits.record_limit));
let mut retained_bytes = 0usize;
let mut omitted_content_bytes = 0usize;
for record in records {
total_content_bytes = total_content_bytes.saturating_add(record.content.len());
let include = rendered.len() < limits.record_limit && retained_bytes < limits.byte_limit;
let retained = if include {
record
.content
.len()
.min(limits.byte_limit.saturating_sub(retained_bytes))
} else {
0
};
let omitted = record.content.len().saturating_sub(retained);
omitted_content_bytes = omitted_content_bytes.saturating_add(omitted);
if !include {
continue;
}
let content = String::from_utf8_lossy(&record.content[..retained]).into_owned();
retained_bytes = retained_bytes.saturating_add(retained);
rendered.push(CanisterDiagnosticLogRecord {
index: record.idx,
timestamp_nanos: record.timestamp_nanos,
content,
original_content_bytes: record.content.len(),
omitted_content_bytes: omitted,
});
}
CanisterDiagnosticLogs {
records: rendered,
total_records,
total_content_bytes,
omitted_content_bytes,
}
}
#[cfg(test)]
mod tests {
use std::cell::Cell;
use ic_host_artifacts::batch::BatchLabelError;
use ic_platform_pocket_ic::CanisterLogRecord;
use ic_platform_types::principal::ExternalPrincipal as Principal;
use super::{
CanisterDiagnosticsReport, CanisterDiagnosticsRequest, CanisterLogRenderLimits,
LabeledCanisterDiagnosticsRequest, PocketIcDiagnosticsExt, render_log_records,
};
struct Reporting {
calls: Cell<usize>,
request: Cell<Option<CanisterDiagnosticsRequest>>,
}
impl PocketIcDiagnosticsExt for Reporting {
fn collect_canister_diagnostics(
&self,
request: CanisterDiagnosticsRequest,
) -> CanisterDiagnosticsReport {
self.calls.set(self.calls.get() + 1);
self.request.set(Some(request));
CanisterDiagnosticsReport {
request,
status: Err(ic_platform_pocket_ic::ClientError::Protocol(
"synthetic status failure".into(),
)),
logs: Err(ic_platform_pocket_ic::ClientError::Protocol(
"synthetic log failure".into(),
)),
}
}
}
#[test]
fn explicit_reporting_preserves_requests_and_writer_failures() {
let collector = Reporting {
calls: Cell::new(0),
request: Cell::new(None),
};
let request = CanisterDiagnosticsRequest {
canister_id: Principal::from_slice(&[1]),
status_sender: Principal::from_slice(&[2]),
log_sender: Principal::from_slice(&[3]),
log_limits: CanisterLogRenderLimits::default(),
};
let mut output = Vec::new();
collector
.write_canister_diagnostics(request, "install", &mut output)
.unwrap();
let text = String::from_utf8(output).unwrap();
assert!(
text.contains("synthetic status failure") && text.contains("synthetic log failure")
);
let error = collector
.write_canister_diagnostics(request, "install", &mut std::io::Cursor::new([0_u8; 0]))
.unwrap_err();
assert_eq!(error.kind(), std::io::ErrorKind::WriteZero);
assert_eq!(collector.calls.get(), 2);
assert_eq!(collector.request.get(), Some(request));
}
#[test]
fn labeled_batch_retains_order_and_continues_after_entry_failure() {
let collector = Reporting {
calls: Cell::new(0),
request: Cell::new(None),
};
let first = CanisterDiagnosticsRequest {
canister_id: Principal::from_slice(&[1]),
status_sender: Principal::from_slice(&[2]),
log_sender: Principal::from_slice(&[3]),
log_limits: CanisterLogRenderLimits::default(),
};
let second = CanisterDiagnosticsRequest {
canister_id: Principal::from_slice(&[4]),
status_sender: Principal::from_slice(&[5]),
log_sender: Principal::from_slice(&[6]),
log_limits: CanisterLogRenderLimits::default(),
};
let report = collector
.collect_canister_diagnostics_batch(&[
LabeledCanisterDiagnosticsRequest {
label: "root".into(),
request: first,
},
LabeledCanisterDiagnosticsRequest {
label: "worker".into(),
request: second,
},
])
.expect("valid labeled diagnostics batch");
assert_eq!(collector.calls.get(), 2);
assert_eq!(report.entries.len(), 2);
assert_eq!(report.entries[0].label, "root");
assert_eq!(report.entries[0].report.request, first);
assert_eq!(report.entries[1].label, "worker");
assert_eq!(report.entries[1].report.request, second);
assert!(
report
.entries
.iter()
.all(|entry| entry.entry_elapsed <= report.total)
);
assert_eq!(report.failures().count(), 2);
assert!(!report.is_success());
let compact = report.to_string();
assert!(compact.contains("label=\"root\""));
assert!(compact.contains("label=\"worker\""));
assert!(compact.contains("synthetic status failure"));
}
#[test]
fn diagnostic_batch_rejects_invalid_labels_before_collection() {
let collector = Reporting {
calls: Cell::new(0),
request: Cell::new(None),
};
let request = CanisterDiagnosticsRequest {
canister_id: Principal::from_slice(&[1]),
status_sender: Principal::from_slice(&[2]),
log_sender: Principal::from_slice(&[3]),
log_limits: CanisterLogRenderLimits::default(),
};
let empty = collector
.collect_canister_diagnostics_batch(&[LabeledCanisterDiagnosticsRequest {
label: String::new(),
request,
}])
.expect_err("empty label must reject diagnostics batch");
assert_eq!(empty, BatchLabelError::Empty { index: 0 });
assert_eq!(collector.calls.get(), 0);
let duplicate = collector
.collect_canister_diagnostics_batch(&[
LabeledCanisterDiagnosticsRequest {
label: "same".into(),
request,
},
LabeledCanisterDiagnosticsRequest {
label: "same".into(),
request,
},
])
.expect_err("duplicate labels must reject diagnostics batch");
assert_eq!(
duplicate,
BatchLabelError::Duplicate {
label: "same".to_owned(),
first_index: 0,
duplicate_index: 1,
}
);
assert_eq!(collector.calls.get(), 0);
}
#[test]
fn log_rendering_is_bounded_lossy_utf8_and_reports_truncation() {
let logs = render_log_records(
vec![
CanisterLogRecord {
idx: 7,
timestamp_nanos: 11,
content: vec![b'f', 0x80, b'o'],
},
CanisterLogRecord {
idx: 8,
timestamp_nanos: 12,
content: b"bar".to_vec(),
},
],
CanisterLogRenderLimits {
record_limit: 1,
byte_limit: 2,
},
);
assert_eq!(logs.total_records(), 2);
assert_eq!(logs.total_content_bytes(), 6);
assert_eq!(logs.omitted_records(), 1);
assert_eq!(logs.omitted_content_bytes(), 4);
assert!(logs.was_truncated());
assert_eq!(logs.records().len(), 1);
assert_eq!(logs.records()[0].content(), "f�");
assert_eq!(logs.records()[0].original_content_bytes(), 3);
assert_eq!(logs.records()[0].omitted_content_bytes(), 1);
assert!(logs.records()[0].was_truncated());
let rendered = logs.to_string();
assert!(rendered.contains("f�"));
assert!(rendered.contains("truncated omitted_records=1 omitted_content_bytes=4"));
}
#[test]
fn zero_log_bounds_retain_only_aggregate_truncation() {
for (record_limit, byte_limit) in [(0, 0), (0, 16), (16, 0)] {
let logs = render_log_records(
vec![CanisterLogRecord {
idx: 1,
timestamp_nanos: 2,
content: b"hello".to_vec(),
}],
CanisterLogRenderLimits {
record_limit,
byte_limit,
},
);
assert_eq!(logs.records(), []);
assert_eq!(logs.total_records(), 1);
assert_eq!(logs.total_content_bytes(), 5);
assert_eq!(logs.omitted_records(), 1);
assert_eq!(logs.omitted_content_bytes(), 5);
assert!(logs.was_truncated());
assert_eq!(
logs.to_string(),
"<no retained records>; truncated omitted_records=1 omitted_content_bytes=5"
);
}
}
#[test]
fn logs_within_bounds_preserve_empty_records_and_raw_byte_totals() {
let limits = CanisterLogRenderLimits {
record_limit: 3,
byte_limit: 3,
};
let empty = render_log_records(vec![], limits);
assert_eq!(empty.total_records(), 0);
assert_eq!(empty.total_content_bytes(), 0);
assert_eq!(empty.omitted_records(), 0);
assert_eq!(empty.omitted_content_bytes(), 0);
assert!(!empty.was_truncated());
assert_eq!(empty.to_string(), "<empty>");
let logs = render_log_records(
vec![
CanisterLogRecord {
idx: 1,
timestamp_nanos: 2,
content: vec![],
},
CanisterLogRecord {
idx: 3,
timestamp_nanos: 4,
content: vec![0x80, b'a'],
},
CanisterLogRecord {
idx: 5,
timestamp_nanos: 6,
content: vec![],
},
],
limits,
);
assert_eq!(logs.total_records(), 3);
assert_eq!(logs.records().len(), 3);
assert_eq!(logs.total_content_bytes(), 2);
assert_eq!(logs.records()[0].content(), "");
assert_eq!(logs.records()[1].content(), "�a");
assert_eq!(logs.records()[2].content(), "");
assert_eq!(logs.omitted_records(), 0);
assert_eq!(logs.omitted_content_bytes(), 0);
assert!(!logs.was_truncated());
}
}