Skip to content

Commit 662b294

Browse files
authored
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
1 parent 6292b7f commit 662b294

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

tracing-appender/src/non_blocking.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,18 @@ impl NonBlocking {
154154
writer: T,
155155
buffered_lines_limit: usize,
156156
is_lossy: bool,
157+
thread_name: String,
157158
) -> (NonBlocking, WorkerGuard) {
158159
let (sender, receiver) = bounded(buffered_lines_limit);
159160

160161
let (shutdown_sender, shutdown_receiver) = bounded(0);
161162

162163
let worker = Worker::new(receiver, writer, shutdown_receiver);
163-
let worker_guard =
164-
WorkerGuard::new(worker.worker_thread(), sender.clone(), shutdown_sender);
164+
let worker_guard = WorkerGuard::new(
165+
worker.worker_thread(thread_name),
166+
sender.clone(),
167+
shutdown_sender,
168+
);
165169

166170
(
167171
Self {
@@ -187,6 +191,7 @@ impl NonBlocking {
187191
pub struct NonBlockingBuilder {
188192
buffered_lines_limit: usize,
189193
is_lossy: bool,
194+
thread_name: String,
190195
}
191196

192197
impl NonBlockingBuilder {
@@ -207,9 +212,22 @@ impl NonBlockingBuilder {
207212
self
208213
}
209214

215+
/// Override the worker thread's name.
216+
///
217+
/// The default worker thread name is "tracing-appender".
218+
pub fn thread_name(mut self, name: &str) -> NonBlockingBuilder {
219+
self.thread_name = name.to_string();
220+
self
221+
}
222+
210223
/// Completes the builder, returning the configured `NonBlocking`.
211224
pub fn finish<T: Write + Send + Sync + 'static>(self, writer: T) -> (NonBlocking, WorkerGuard) {
212-
NonBlocking::create(writer, self.buffered_lines_limit, self.is_lossy)
225+
NonBlocking::create(
226+
writer,
227+
self.buffered_lines_limit,
228+
self.is_lossy,
229+
self.thread_name,
230+
)
213231
}
214232
}
215233

@@ -218,6 +236,7 @@ impl Default for NonBlockingBuilder {
218236
NonBlockingBuilder {
219237
buffered_lines_limit: DEFAULT_BUFFERED_LINES_LIMIT,
220238
is_lossy: true,
239+
thread_name: "tracing-appender".to_string(),
221240
}
222241
}
223242
}

tracing-appender/src/worker.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ impl<T: Write + Send + Sync + 'static> Worker<T> {
6767
}
6868

6969
/// Creates a worker thread that processes a channel until it's disconnected
70-
pub(crate) fn worker_thread(mut self) -> std::thread::JoinHandle<()> {
70+
pub(crate) fn worker_thread(mut self, name: String) -> std::thread::JoinHandle<()> {
7171
thread::Builder::new()
72-
.name("tracing-appender".to_string())
72+
.name(name)
7373
.spawn(move || {
7474
loop {
7575
match self.work() {

0 commit comments

Comments
 (0)