hive-c0re logs each container build with build_id, agent, kind, start and finish lines
Every output line of a nixos-container operation already reached the journal with its command line. They now also carry the build-log row id, the agent and the kind, and a start and a finish line (status, runtime) bracket them, so the swarm log store can group a build and a board can list builds.
This commit is contained in:
parent
9a773349a8
commit
febeb0d395
1 changed files with 209 additions and 21 deletions
|
|
@ -766,32 +766,118 @@ fn is_actionable_nix_line(line: &str) -> bool {
|
|||
|| trimmed.starts_with("error:")
|
||||
}
|
||||
|
||||
/// What every log line of one container operation carries, so the swarm log
|
||||
/// store can group a build's lines and the builds board can list the builds:
|
||||
/// `build_id` (the build-log row's id, empty when the store is not installed),
|
||||
/// `agent` and `kind`. A `start` line opens the build and a `finish` line
|
||||
/// closes it with its `status` and `runtime_secs`; the output lines between
|
||||
/// them have no `event`.
|
||||
#[derive(Clone)]
|
||||
struct BuildTag {
|
||||
build_id: String,
|
||||
agent: String,
|
||||
kind: String,
|
||||
cmdline: String,
|
||||
}
|
||||
|
||||
impl BuildTag {
|
||||
fn new(log_id: Option<i64>, agent: &str, kind: &str, cmdline: &str) -> Self {
|
||||
Self {
|
||||
build_id: log_id.map(|id| id.to_string()).unwrap_or_default(),
|
||||
agent: agent.to_owned(),
|
||||
kind: kind.to_owned(),
|
||||
cmdline: cmdline.to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
fn started(&self) {
|
||||
tracing::info!(
|
||||
target: "nixos-container",
|
||||
event = "start",
|
||||
build_id = %self.build_id,
|
||||
agent = %self.agent,
|
||||
kind = %self.kind,
|
||||
cmdline = %self.cmdline,
|
||||
"{} started",
|
||||
self.cmdline
|
||||
);
|
||||
}
|
||||
|
||||
/// One output line; a nix warning or error is logged at warn, the rest at
|
||||
/// info.
|
||||
fn line(&self, stream: hive_priv_sock::PrivStream, line: &str) {
|
||||
let actionable =
|
||||
matches!(stream, hive_priv_sock::PrivStream::Stderr) && is_actionable_nix_line(line);
|
||||
if actionable {
|
||||
tracing::warn!(
|
||||
target: "nixos-container",
|
||||
build_id = %self.build_id,
|
||||
agent = %self.agent,
|
||||
kind = %self.kind,
|
||||
cmdline = %self.cmdline,
|
||||
"{line}"
|
||||
);
|
||||
} else {
|
||||
tracing::info!(
|
||||
target: "nixos-container",
|
||||
build_id = %self.build_id,
|
||||
agent = %self.agent,
|
||||
kind = %self.kind,
|
||||
cmdline = %self.cmdline,
|
||||
"{line}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn finished(&self, succeeded: bool, runtime: std::time::Duration) {
|
||||
let status = if succeeded { "ok" } else { "fail" };
|
||||
if succeeded {
|
||||
tracing::info!(
|
||||
target: "nixos-container",
|
||||
event = "finish",
|
||||
build_id = %self.build_id,
|
||||
agent = %self.agent,
|
||||
kind = %self.kind,
|
||||
status,
|
||||
runtime_secs = runtime.as_secs(),
|
||||
cmdline = %self.cmdline,
|
||||
"{} finished: {status}",
|
||||
self.cmdline
|
||||
);
|
||||
} else {
|
||||
tracing::warn!(
|
||||
target: "nixos-container",
|
||||
event = "finish",
|
||||
build_id = %self.build_id,
|
||||
agent = %self.agent,
|
||||
kind = %self.kind,
|
||||
status,
|
||||
runtime_secs = runtime.as_secs(),
|
||||
cmdline = %self.cmdline,
|
||||
"{} finished: {status}",
|
||||
self.cmdline
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build the per-line callback for `create_container_streaming` /
|
||||
/// `update_container_streaming`. Both ops share identical dispatch logic
|
||||
/// (stdout → info + `append_stdout`, stderr → warn for a nix warning/error
|
||||
/// and info otherwise + `append_stderr`); this helper avoids duplicating
|
||||
/// that match body across the two call sites.
|
||||
/// (log the line through its `BuildTag`, then `append_stdout` or
|
||||
/// `append_stderr`); this helper avoids duplicating that match body across the
|
||||
/// two call sites.
|
||||
fn make_log_callback(
|
||||
logs: Option<std::sync::Arc<crate::build_logs::BuildLogs>>,
|
||||
log_id: Option<i64>,
|
||||
cmdline: String,
|
||||
tag: BuildTag,
|
||||
) -> impl FnMut(hive_priv_sock::PrivStream, &str) {
|
||||
use hive_priv_sock::PrivStream;
|
||||
move |stream, line| match stream {
|
||||
PrivStream::Stdout => {
|
||||
tracing::info!(target: "nixos-container", cmdline = %cmdline, "{line}");
|
||||
if let (Some(h), Some(id)) = (&logs, log_id) {
|
||||
h.append_stdout(id, line);
|
||||
}
|
||||
}
|
||||
PrivStream::Stderr => {
|
||||
if is_actionable_nix_line(line) {
|
||||
tracing::warn!(target: "nixos-container", cmdline = %cmdline, "{line}");
|
||||
} else {
|
||||
tracing::info!(target: "nixos-container", cmdline = %cmdline, "{line}");
|
||||
}
|
||||
if let (Some(h), Some(id)) = (&logs, log_id) {
|
||||
h.append_stderr(id, line);
|
||||
move |stream, line| {
|
||||
tag.line(stream, line);
|
||||
if let (Some(h), Some(id)) = (&logs, log_id) {
|
||||
match stream {
|
||||
PrivStream::Stdout => h.append_stdout(id, line),
|
||||
PrivStream::Stderr => h.append_stderr(id, line),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -862,13 +948,16 @@ async fn priv_run_inner(
|
|||
|
||||
// For long-running ops use the streaming protocol so build_logs
|
||||
// receives lines in real time rather than as a batch at completion.
|
||||
let tag = BuildTag::new(log_id, name, kind, &cmdline);
|
||||
tag.started();
|
||||
let began = std::time::Instant::now();
|
||||
let result: Result<()> = match kind {
|
||||
"create" => match toplevel {
|
||||
Some(toplevel) => {
|
||||
crate::priv_client::create_container_streaming(
|
||||
name,
|
||||
toplevel,
|
||||
make_log_callback(logs.clone(), log_id, cmdline.clone()),
|
||||
make_log_callback(logs.clone(), log_id, tag.clone()),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
|
@ -879,7 +968,7 @@ async fn priv_run_inner(
|
|||
crate::priv_client::update_container_streaming(
|
||||
name,
|
||||
toplevel,
|
||||
make_log_callback(logs.clone(), log_id, cmdline.clone()),
|
||||
make_log_callback(logs.clone(), log_id, tag.clone()),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
|
@ -903,6 +992,7 @@ async fn priv_run_inner(
|
|||
};
|
||||
|
||||
let succeeded = result.is_ok();
|
||||
tag.finished(succeeded, began.elapsed());
|
||||
if let (Some(h), Some(id)) = (&logs, log_id) {
|
||||
h.finish(
|
||||
id,
|
||||
|
|
|
|||
Loading…
Reference in a new issue