mirror of
https://github.com/tokio-rs/tracing.git
synced 2026-03-19 06:12:30 +00:00
## Motivation Currently, `tracing-appender`'s `RollingFileAppender` does not implement the `MakeWriter` trait. This means it can only be used by either wrapping it in `NonBlocking`, or by wrapping it in a `Mutex`. However, this shouldn't be strictly necessary, as `&File` implements `io::Write`. It should thus only be necessary to introduce locking when we are in the process of _rotating_ the log file. ## Solution This branch adds a `MakeWriter` implementation for `RollingFileAppender`. This is done by moving the file itself inside of an `RwLock`, so that a read lock is acquired to write to the file. This allows multiple threads to write to the file without contention. When the file needs to be rolled, the rolling thread acquires the write lock to replace the file. Acquiring the write lock is guarded by an atomic CAS on the timestamp, so that only a single thread will try to roll the file. This prevents other threads from immediately rolling the file _again_ when the write lock is released. I...should probably write tests for that, though. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Tracing Examples
This directory contains a collection of examples that demonstrate the use of the
tracing ecosystem:
- tracing:
counters: Implements a very simple metrics system to demonstrate how subscribers can consume field values as typed data.sloggish: A demoSubscriberimplementation that mimics the output ofslog-term'sCompactformatter.
- tracing-attributes:
attrs-basic: A simple example of the#[instrument]attribute.attrs-literal-field-names: Demonstrates using literal field names rather than rust tokens..attrs-args: An example implementing a simple recursive calculation of Fibonacci numbers, to demonstrate how the#[instrument]attribute can record function arguments.
- tracing-subscriber:
fmt: Demonstrates the use of thefmtmodule intracing-subscriber, which provides a subscriber implementation that logs traces to the console.fmt-stderr: Demonstrates overriding the output stream used by thefmtsubscriber.fmt-custom-field: Demonstrates overriding how thefmtsubscriber formats fields on spans and events.fmt-custom-event: Demonstrates overriding how thefmtsubscriber formats events.fmt-multiple-writers.rs: demonstrates howfmt::Layercan write to multiple destinations (in this instance, stdout and a file) simultaneously.subscriber-filter: Demonstrates thetracing-subscriber::filtermodule, which provides a layer which adds configurable filtering to a subscriber implementation.tower-load: Demonstrates how dynamically reloadable filters can be used to debug a server under load in production.journald: Demonstrates how to usefmtandjournaldlayers to output to both the terminal and the system journal.toggle-layers: Demonstrates how Layers can be wrapped with anOptionallowing them to be dynamically toggled.
- tracing-futures:
spawny-thing: Demonstrates the use of the#[instrument]attribute macro asynchronous functions.tokio-spawny-thing.rs: Similar tospawny-thingy, but with the additional demonstration instrumenting concurrent tasks created withtokio::spawn.futures-proxy-server: Demonstrates the use oftracing-futuresby implementing a simple proxy server, based on this example fromtokio.async_fn: Demonstrates how asynchronous functions can be instrumented.echo: Demonstrates atracing-instrumented variant of Tokio'sechoexample.
- tracing-flame:
infero-flame: Demonstrates the use oftracing-flameto generate a flamegraph from spans.
- tracing-tower:
tower-client: Demonstrates the use oftracing-towerto instrument a simpletowerHTTP/1.1 client.tower-server: Demonstrates the use oftracing-towerto instrument a simpletowerHTTP/1.1 server.
- tracing-serde:
serde-yak-shave: Demonstrates the use oftracing-serdeby implementing a subscriber that emits trace output as JSON.
- tracing-log:
hyper-echo: Demonstrates howtracing-logcan be used to record unstructured logs from dependencies astracingevents, by instrumenting this example fromhyper, and usingtracing-logto record logs emitted byhyper.
- tracing-opentelemetry:
opentelemetry: Demonstrates howtracing-opentelemetrycan be used to export and visualizetracingspan data.opentelemetry-remote-context: Demonstrates howtracing-opentelemetrycan be used to extract and inject remote context when traces span multiple systems.