Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
add some diagnostics to file logger
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jan 22, 2024
1 parent 423ff2c commit e76eb97
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions targets/file/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![feature(stmt_expr_attributes, proc_macro_hygiene)]

use std::{
fs,
io::{self, Write},
Expand Down Expand Up @@ -74,7 +76,11 @@ impl FileSetBuilder {
let mut active_file = None;

let _ = receiver.blocking_exec(|mut batch: Buffer| {
let mut file = match active_file.take() {
use emit_batcher::Channel as _;

emit::debug!(rt: emit::runtime::internal(), "writing file batch of {batch_size: batch.remaining()} events");

let (mut file, path) = match active_file.take() {
Some(file) => file,
None => {
let now = std::time::UNIX_EPOCH.elapsed().unwrap();
Expand Down Expand Up @@ -115,13 +121,22 @@ impl FileSetBuilder {
let mut path = path.clone();
path.push(file_name);

try_open_reuse(&path).ok()
try_open_reuse(&path)
.map_err(|err| {
emit::warn!(rt: emit::runtime::internal(), "failed to open {#[emit::as_debug] path}: {err}");

err
})
.ok()
.map(|file| (file, path))
} else {
None
};

if let Some(file) = reuse_file {
file
if let Some((file, path)) = reuse_file {
emit::debug!(rt: emit::runtime::internal(), "reusing {#[emit::as_debug] path}");

(file, path)
}
// If there's no file to reuse then create a new one
else {
Expand All @@ -130,17 +145,27 @@ impl FileSetBuilder {

path.push(file_name(&file_prefix, &file_ext, &file_ts, &file_id));

match try_open_create(path) {
Ok(file) => file,
Err(e) => return Err(emit_batcher::BatchError::retry(e, batch)),
match try_open_create(&path) {
Ok(file) => {
emit::debug!(rt: emit::runtime::internal(), "created {#[emit::as_debug] path}");

(file, path)
},
Err(err) => {
emit::warn!(rt: emit::runtime::internal(), "failed to create {#[emit::as_debug] path}: {err}");

return Err(emit_batcher::BatchError::retry(err, batch))
},
}
}
}
};

while batch.index < batch.bufs.len() {
if let Err(e) = file.write_all(batch.bufs[batch.index].as_bytes()) {
return Err(emit_batcher::BatchError::retry(e, batch));
if let Err(err) = file.write_all(batch.bufs[batch.index].as_bytes()) {
emit::warn!(rt: emit::runtime::internal(), "failed to write event to {#[emit::as_debug] path}: {err}");

return Err(emit_batcher::BatchError::retry(err, batch));
}

// Drop the buffer at this point to free some memory
Expand All @@ -153,7 +178,7 @@ impl FileSetBuilder {
file.sync_all()
.map_err(|e| emit_batcher::BatchError::no_retry(e))?;

active_file = Some(file);
active_file = Some((file, path));

Ok(())
});
Expand Down

0 comments on commit e76eb97

Please sign in to comment.