WS-G: apply review fixes F1-F8

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YShymaPrXmc3KYRUg2HMSs
This commit is contained in:
ariandClaude Sonnet 5.5 committed 2026-10-15 18:43:21 -04:00
1 parent 2aeb9ae154
commit 1eb7ffce19
4 files changed
+219 -99

No files matched your search

+83 -6
View File
@@ -130,7 +130,31 @@ pub fn spawn(state: AppState, provider: &SdkLoggerProvider) -> Vec<JoinHandle<()
handles
}
/// Names identifying this process in the journal (unit and syslog identifier).
/// Used to keep the app's own log lines out of the unfiltered follow, which
/// would otherwise feed back into itself. Limitation: matching is by name; the
/// journal cursor is not used.
pub fn own_names(service_name: &str) -> Vec<String> {
let mut v = vec![service_name.to_owned(), "skyg-linux-tracker".to_owned()];
if let Some(exe) = std::env::current_exe()
.ok()
.and_then(|p| p.file_name().map(|n| n.to_string_lossy().into_owned()))
{
v.push(exe);
}
v
}
pub fn is_own_entry(e: &JournalEntry, own: &[String]) -> bool {
e.identifier.as_ref().is_some_and(|i| own.contains(i))
|| e.unit.as_ref().is_some_and(|u| {
own.iter()
.any(|n| u == n || u.strip_suffix(".service") == Some(n.as_str()))
})
}
async fn journal_task(state: AppState, logger: SdkLogger, unit: Option<String>) {
let own = own_names(&state.config.otel.service_name);
let mut backoff = Backoff::new(BACKOFF_MIN, BACKOFF_MAX);
let mut last: Option<Timestamp> = None;
let max_lines = state.config.limits.max_lines;
@@ -155,6 +179,9 @@ async fn journal_task(state: AppState, logger: SdkLogger, unit: Option<String>)
e = rx.recv() => e,
};
let Some(e) = e else { break };
if is_own_entry(&e, &own) {
continue;
}
if last.is_some_and(|l| e.timestamp <= l) {
continue;
}
@@ -210,6 +237,8 @@ async fn docker_reconciler(state: AppState, logger: SdkLogger) {
let allow = state.config.otel.logs.docker_containers.clone();
let mut tasks: HashMap<String, (CancellationToken, JoinHandle<()>)> = HashMap::new();
let mut warned = false;
let mut failures = 0u32;
let mut first_pass = true;
loop {
let mut delay = RECONCILE_INTERVAL;
let ps = tokio::select! {
@@ -219,6 +248,9 @@ async fn docker_reconciler(state: AppState, logger: SdkLogger) {
match ps {
Ok(cs) => {
warned = false;
failures = 0;
let started: HashMap<String, Option<Timestamp>> =
cs.iter().map(|c| (c.name.clone(), c.started_at)).collect();
let running: Vec<String> = cs.into_iter().map(|c| c.name).collect();
let tracked: Vec<String> = tasks.keys().cloned().collect();
let (start, stop) = reconcile(&running, &tracked, &allow);
@@ -230,21 +262,35 @@ async fn docker_reconciler(state: AppState, logger: SdkLogger) {
}
for name in start {
let tok = state.shutdown.child_token();
// Containers present at startup: only new lines. Containers that
// appear later: from their StartedAt so startup output is kept.
let from = if first_pass {
None
} else {
started.get(&name).copied().flatten()
};
let h = tokio::spawn(container_task(
state.clone(),
logger.clone(),
name.clone(),
from,
tok.clone(),
));
tasks.insert(name, (tok, h));
}
first_pass = false;
}
Err(e) => {
if !warned {
tracing::warn!(error = %e, "otel logs: docker unavailable, retrying slowly");
warned = true;
failures += 1;
// Transient errors keep the normal cadence; only a persistent
// outage switches to the slow retry.
if failures >= 3 {
if !warned {
tracing::warn!(error = %e, "otel logs: docker unavailable, retrying slowly");
warned = true;
}
delay = DOCKER_SLOW_RETRY;
}
delay = DOCKER_SLOW_RETRY;
}
}
if !sleep_or_shutdown(&state.shutdown, delay).await {
@@ -261,13 +307,23 @@ async fn container_task(
state: AppState,
logger: SdkLogger,
name: String,
from: Option<Timestamp>,
token: CancellationToken,
) {
let mut backoff = Backoff::new(BACKOFF_MIN, BACKOFF_MAX);
let mut since: Option<Timestamp> = Some(Timestamp::now());
// `from == None`: container was already running at startup, so only new lines.
// docker `tail=0` returns nothing regardless of `since`, so a backlog needs
// a real tail.
let mut since: Option<Timestamp> = Some(from.unwrap_or_else(Timestamp::now));
let mut tail = if from.is_some() {
state.config.limits.max_tail
} else {
0
};
let mut last_emitted: Option<Timestamp> = None;
loop {
let opts = LogOptions {
tail: Some(0),
tail: Some(tail),
since,
};
let rx = tokio::select! {
@@ -285,6 +341,11 @@ async fn container_task(
let Some(l) = l else { break };
got = true;
if let Some(ts) = l.ts {
// Dedupe lines replayed after a reconnect.
if last_emitted.is_some_and(|le| ts <= le) {
continue;
}
last_emitted = Some(ts);
since = Some(ts);
}
emit(&logger, docker_record(&name, &l));
@@ -297,6 +358,10 @@ async fn container_task(
tracing::debug!(error = %e, container = %name, "otel logs: container follow failed");
}
}
// After the first connection, reconnects replay from the last seen line.
if last_emitted.is_some() {
tail = state.config.limits.max_tail;
}
if !sleep_or_shutdown(&token, backoff.next_delay()).await {
return;
}
@@ -320,6 +385,18 @@ mod tests {
}
}
#[test]
fn own_entries_are_excluded() {
let own = vec!["skyg-linux-tracker".to_string()];
let mut e = entry(None);
assert!(!is_own_entry(&e, &own));
e.identifier = Some("skyg-linux-tracker".into());
assert!(is_own_entry(&e, &own));
e.identifier = Some("slinuxm".into());
e.unit = Some("skyg-linux-tracker.service".into());
assert!(is_own_entry(&e, &own));
}
#[test]
fn severity_mapping() {
use Priority::*;