Files
faunosteo/crates/worker/src/log.rs
T

162 lines
4.5 KiB
Rust

//! JSON-lines logging of the parent (STORAGE.md §3.7): one object per line on stdout, an
//! allowlist of fields, values authored by the worker only. Text a child wrote is escaped
//! by the JSON encoder and truncated; no key, no token, no user content ever reaches a line.
use std::io::Write;
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};
use serde_json::{Map, Value};
static WORKER_ID: OnceLock<String> = OnceLock::new();
/// Tags every later line with the worker id (first call wins).
pub fn set_worker_id(id: &str) {
let _ = WORKER_ID.set(id.to_owned());
}
/// Longest child stderr excerpt kept in a line.
pub const CHILD_STDERR_MAX: usize = 200;
/// One line under construction.
#[must_use = "a log line is written by emit()"]
pub struct LogLine {
fields: Map<String, Value>,
}
pub fn info(event: &str) -> LogLine {
LogLine::new("info", event)
}
pub fn warn(event: &str) -> LogLine {
LogLine::new("warn", event)
}
pub fn error(event: &str) -> LogLine {
LogLine::new("error", event)
}
impl LogLine {
fn new(level: &str, event: &str) -> Self {
let mut fields = Map::new();
fields.insert("ts".into(), Value::from(unix_seconds()));
fields.insert("level".into(), Value::from(level));
fields.insert("event".into(), Value::from(event));
if let Some(id) = WORKER_ID.get() {
fields.insert("worker_id".into(), Value::from(id.as_str()));
}
Self { fields }
}
fn with(mut self, key: &str, value: impl Into<Value>) -> Self {
self.fields.insert(key.to_owned(), value.into());
self
}
pub fn asset(self, id: &str) -> Self {
self.with("asset_id", id)
}
/// A sheet verification round (V1-2.3).
pub fn review(self, id: &str) -> Self {
self.with("review_id", id)
}
pub fn stage(self, stage: &str) -> Self {
self.with("stage", stage)
}
pub fn code(self, code: &str) -> Self {
self.with("code", code)
}
pub fn signal(self, signal: i32) -> Self {
self.with("signal", signal)
}
pub fn http_status(self, status: u16) -> Self {
self.with("http_status", status)
}
pub fn duration_ms(self, ms: u128) -> Self {
self.with("duration_ms", u64::try_from(ms).unwrap_or(u64::MAX))
}
pub fn count(self, count: usize) -> Self {
self.with("count", u64::try_from(count).unwrap_or(u64::MAX))
}
/// Worker-authored text only (never a child's or a server's free text).
pub fn detail(self, detail: &str) -> Self {
self.with("detail", detail)
}
/// Child-controlled text: truncated on a character boundary, escaped by the encoder.
pub fn child_stderr(self, text: &str) -> Self {
self.with("child_stderr", truncate(text, CHILD_STDERR_MAX))
}
/// Renders the line (tests read it, `emit` writes it).
pub fn render(&self) -> String {
Value::Object(self.fields.clone()).to_string()
}
pub fn emit(self) {
let stdout = std::io::stdout();
let mut lock = stdout.lock();
let _ = writeln!(lock, "{}", self.render());
let _ = lock.flush();
}
}
fn truncate(text: &str, max: usize) -> &str {
if text.len() <= max {
return text;
}
let mut end = max;
while !text.is_char_boundary(end) {
end -= 1;
}
&text[..end]
}
fn unix_seconds() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn lines_are_json_objects_with_the_allowlisted_fields() {
let line = info("job_done")
.asset("a1")
.stage("commit")
.duration_ms(12)
.render();
let value: Value = serde_json::from_str(&line).unwrap();
assert_eq!(value["event"], "job_done");
assert_eq!(value["asset_id"], "a1");
assert_eq!(value["stage"], "commit");
assert_eq!(value["duration_ms"], 12);
assert!(value["ts"].as_u64().is_some());
}
#[test]
fn child_text_is_escaped_and_truncated() {
let hostile = format!("line\n\u{1b}[31mred\"quote{}", "é".repeat(300));
let line = warn("child").child_stderr(&hostile).render();
assert!(!line.contains('\n'));
assert!(!line.contains('\u{1b}'));
let value: Value = serde_json::from_str(&line).unwrap();
let kept = value["child_stderr"].as_str().unwrap();
assert!(kept.len() <= CHILD_STDERR_MAX);
assert!(kept.starts_with("line\n"));
}
}