A cluster rehearsal moved a Log tracer to another directory: the write was reported x:settingsReload applied:true, but the tracer kept writing to the old file until a restart. Telemetry::update only refreshed each running tracer's events, level and lossiness; a tracer's own settings (path, prefix, rotation, format, endpoint, headers, ...) stayed as built. Each tracer now carries a hash of the registry object it was built from, less the fields that change in place. The reload compares it with the running tracer's: unchanged ones are updated in place as before, changed ones are started over, new ones started and removed ones stopped. Only tracers this server started are removed; upstream removed every subscriber not in the settings, which also cut off live-tracing streams on each reload. Starting over is a swap in the collector, so no event is lost or written twice: a subscriber registered under a running one's id replaces it between two collection passes. The old one's batch is sent first (what its full channel can't take moves to the new one), and dropping it closes its channel, so its task writes what is queued and ends. Per tracer kind: - Log: a tracer started over on the same files (rotation or format changed) waits for the old one to finish, so lines don't interleave. - Webhook: the task held a sender of its own channel for retries, so it never ended; retries now use a weak sender, and pending events are posted when the channel closes. - OpenTelemetry: pending logs and spans are exported when the channel closes instead of dropped, and a span that was open across the swap is exported by the new tracer with the events it saw. - Console and journal: nothing kept between batches. - Trace history: built from the tracing store, which takes a restart, so it is never started over. No kind needs a restart, so x:settingsReload doesn't gain one. system::tracer_reload::tracer_reload_tests (new): a Log tracer created over JMAP writes to its directory; its path is changed over JMAP while 2000 numbered events are emitted; after the reload, events land in the new file and not the old one, each numbered event is in exactly one of the two files, and a destroyed tracer writes nothing. On main the new file never appears.
162 lines
5.7 KiB
Rust
162 lines
5.7 KiB
Rust
/*
|
|
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <[email protected]>
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
|
|
*
|
|
* Modified by Coffey Labs in 2026 for INBUXA.
|
|
*/
|
|
|
|
use std::{path::PathBuf, time::SystemTime};
|
|
|
|
use crate::config::telemetry::{LogTracer, RotationStrategy};
|
|
|
|
use mail_parser::DateTime;
|
|
use tokio::{
|
|
fs::{File, OpenOptions},
|
|
io::BufWriter,
|
|
};
|
|
use trc::{TelemetryEvent, ipc::subscriber::SubscriberBuilder, serializers::text::FmtWriter};
|
|
|
|
// inbuxa: when a Log tracer is started over on the same files (its rotation
|
|
// or format changed), the new one waits for the old one to write what it
|
|
// has queued, so their lines don't interleave. Keyed by path and prefix;
|
|
// each entry is the last tracer's "done" signal, sent when it ends.
|
|
type LogFileOwners = ahash::AHashMap<(String, String), tokio::sync::oneshot::Receiver<()>>;
|
|
static LOG_FILE_OWNERS: parking_lot::Mutex<Option<LogFileOwners>> = parking_lot::Mutex::new(None);
|
|
|
|
pub(crate) fn spawn_log_tracer(builder: SubscriberBuilder, settings: LogTracer) {
|
|
let (done_tx, done_rx) = tokio::sync::oneshot::channel::<()>();
|
|
let previous = LOG_FILE_OWNERS
|
|
.lock()
|
|
.get_or_insert_with(Default::default)
|
|
.insert((settings.path.clone(), settings.prefix.clone()), done_rx);
|
|
let (_, mut rx) = builder.register();
|
|
tokio::spawn(async move {
|
|
// Dropped when this tracer ends, however it ends
|
|
let _done = done_tx;
|
|
if let Some(previous) = previous {
|
|
let _ = previous.await;
|
|
}
|
|
|
|
if let Some(writer) = settings.build_writer().await {
|
|
let mut buf = FmtWriter::new(writer)
|
|
.with_ansi(settings.ansi)
|
|
.with_multiline(settings.multiline);
|
|
let mut roatation_timestamp = settings.next_rotation();
|
|
|
|
while let Some(events) = rx.recv().await {
|
|
for event in events {
|
|
// Check if we need to rotate the log file
|
|
if roatation_timestamp != 0 && event.inner.timestamp > roatation_timestamp {
|
|
if let Err(err) = buf.flush().await {
|
|
trc::event!(
|
|
Telemetry(TelemetryEvent::LogError),
|
|
Reason = err.to_string(),
|
|
Details = "Failed to flush log buffer"
|
|
);
|
|
}
|
|
|
|
if let Some(writer) = settings.build_writer().await {
|
|
buf.update_writer(writer);
|
|
roatation_timestamp = settings.next_rotation();
|
|
} else {
|
|
return;
|
|
};
|
|
}
|
|
|
|
if let Err(err) = buf.write(&event).await {
|
|
trc::event!(
|
|
Telemetry(TelemetryEvent::LogError),
|
|
Reason = err.to_string(),
|
|
Details = "Failed to write event to log"
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
|
|
if let Err(err) = buf.flush().await {
|
|
trc::event!(
|
|
Telemetry(TelemetryEvent::LogError),
|
|
Reason = err.to_string(),
|
|
Details = "Failed to flush log buffer"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
impl LogTracer {
|
|
pub async fn build_writer(&self) -> Option<BufWriter<File>> {
|
|
let now = DateTime::from_timestamp(
|
|
SystemTime::now()
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
.map_or(0, |d| d.as_secs()) as i64,
|
|
);
|
|
let file_name = match self.rotate {
|
|
RotationStrategy::Daily => {
|
|
format!(
|
|
"{}.{:04}-{:02}-{:02}",
|
|
self.prefix, now.year, now.month, now.day
|
|
)
|
|
}
|
|
RotationStrategy::Hourly => {
|
|
format!(
|
|
"{}.{:04}-{:02}-{:02}T{:02}",
|
|
self.prefix, now.year, now.month, now.day, now.hour
|
|
)
|
|
}
|
|
RotationStrategy::Minutely => {
|
|
format!(
|
|
"{}.{:04}-{:02}-{:02}T{:02}:{:02}",
|
|
self.prefix, now.year, now.month, now.day, now.hour, now.minute
|
|
)
|
|
}
|
|
RotationStrategy::Never => self.prefix.clone(),
|
|
};
|
|
let path = PathBuf::from(&self.path).join(file_name);
|
|
|
|
match OpenOptions::new()
|
|
.create(true)
|
|
.append(true)
|
|
.open(&path)
|
|
.await
|
|
{
|
|
Ok(writer) => Some(BufWriter::new(writer)),
|
|
Err(err) => {
|
|
trc::event!(
|
|
Telemetry(TelemetryEvent::LogError),
|
|
Details = "Failed to create log file",
|
|
Path = path.to_string_lossy().into_owned(),
|
|
Reason = err.to_string(),
|
|
);
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn next_rotation(&self) -> u64 {
|
|
let mut now = DateTime::from_timestamp(
|
|
SystemTime::now()
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
.map_or(0, |d| d.as_secs()) as i64,
|
|
);
|
|
|
|
now.second = 0;
|
|
|
|
match self.rotate {
|
|
RotationStrategy::Daily => {
|
|
now.hour = 0;
|
|
now.minute = 0;
|
|
now.to_timestamp() as u64 + 86400
|
|
}
|
|
RotationStrategy::Hourly => {
|
|
now.minute = 0;
|
|
now.to_timestamp() as u64 + 3600
|
|
}
|
|
RotationStrategy::Minutely => now.to_timestamp() as u64 + 60,
|
|
RotationStrategy::Never => 0,
|
|
}
|
|
}
|
|
}
|