From 662b29405335de040a9f57b5e85f9eeb21e3d187 Mon Sep 17 00:00:00 2001 From: Nicolas Stinus Date: Fri, 4 Nov 2022 14:54:21 -0400 Subject: [PATCH] appender: allow worker thread name to be configured (#2365) ## Motivation The worker thread name in non blocking mode is always "tracing-appender". It can be convenient to quickly identify the appender threads for audit reasons or affinity pinning. ## Solution This patch adds a new setter to the builder and propagates the info to the thread initialization. Closes #2364 --- tracing-appender/src/non_blocking.rs | 25 ++++++++++++++++++++++--- tracing-appender/src/worker.rs | 4 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tracing-appender/src/non_blocking.rs b/tracing-appender/src/non_blocking.rs index f620608914..ce25565054 100644 --- a/tracing-appender/src/non_blocking.rs +++ b/tracing-appender/src/non_blocking.rs @@ -154,14 +154,18 @@ impl NonBlocking { writer: T, buffered_lines_limit: usize, is_lossy: bool, + thread_name: String, ) -> (NonBlocking, WorkerGuard) { let (sender, receiver) = bounded(buffered_lines_limit); let (shutdown_sender, shutdown_receiver) = bounded(0); let worker = Worker::new(receiver, writer, shutdown_receiver); - let worker_guard = - WorkerGuard::new(worker.worker_thread(), sender.clone(), shutdown_sender); + let worker_guard = WorkerGuard::new( + worker.worker_thread(thread_name), + sender.clone(), + shutdown_sender, + ); ( Self { @@ -187,6 +191,7 @@ impl NonBlocking { pub struct NonBlockingBuilder { buffered_lines_limit: usize, is_lossy: bool, + thread_name: String, } impl NonBlockingBuilder { @@ -207,9 +212,22 @@ impl NonBlockingBuilder { self } + /// Override the worker thread's name. + /// + /// The default worker thread name is "tracing-appender". + pub fn thread_name(mut self, name: &str) -> NonBlockingBuilder { + self.thread_name = name.to_string(); + self + } + /// Completes the builder, returning the configured `NonBlocking`. pub fn finish(self, writer: T) -> (NonBlocking, WorkerGuard) { - NonBlocking::create(writer, self.buffered_lines_limit, self.is_lossy) + NonBlocking::create( + writer, + self.buffered_lines_limit, + self.is_lossy, + self.thread_name, + ) } } @@ -218,6 +236,7 @@ impl Default for NonBlockingBuilder { NonBlockingBuilder { buffered_lines_limit: DEFAULT_BUFFERED_LINES_LIMIT, is_lossy: true, + thread_name: "tracing-appender".to_string(), } } } diff --git a/tracing-appender/src/worker.rs b/tracing-appender/src/worker.rs index 00012d518c..b2579fb2cd 100644 --- a/tracing-appender/src/worker.rs +++ b/tracing-appender/src/worker.rs @@ -67,9 +67,9 @@ impl Worker { } /// Creates a worker thread that processes a channel until it's disconnected - pub(crate) fn worker_thread(mut self) -> std::thread::JoinHandle<()> { + pub(crate) fn worker_thread(mut self, name: String) -> std::thread::JoinHandle<()> { thread::Builder::new() - .name("tracing-appender".to_string()) + .name(name) .spawn(move || { loop { match self.work() {