mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-10-02 15:24:47 +00:00

## Motivation The tracing_error crate should provide some basic functionality for instrumenting Errors with `SpanTraces` generically. The goal is to make it easy for users to instrument their errors without needing to add a `SpanTrace` to every error they construct. ## Solution Critically this trait provides 3 traits for instrumenting errors, `InstrumentResult`, `InstrumentError`, and `ExtractSpanTrace`. The first two traits expose a `in_current_span()` method that can work on a `Result<T, E: InstrumentError>` and an `Error`, respectively, that wraps the error type in a `TracedError` that stores the inner error and a SpanTrace. The third trait, `ExtractSpanTrace`, is used to retrieve the span_trace member of a `TracedError` from a `&dyn Error` trait object. This is done by downcasting the error to a `TracedError` and returning the inner `span_trace` if it is successful. By default this only works because the inner error is stored in a Box as a trait object, which prevents it from being part of the type signature for `TracedError`. However, this crate also exposes a second version of `TracedError` that does not box the inner error, thus avoiding the need for heap allocation, which it accomplishes by type erasing the `TracedError<E>` into a `TracedError<()>` while iterating through the error `source()` chain.
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 demoSubscriber
implementation that mimics the output ofslog-term
'sCompact
formatter.
- tracing-attributes:
attrs-basic
: A simple example of the#[instrument]
attribute.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 thefmt
module intracing-subscriber
, which provides a subscriber implementation that logs traces to the console.fmt-stderr
: Demonstrates overriding the output stream used by thefmt
subscriber.subscriber-filter
: Demonstrates thetracing-subscriber::filter
module, 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.
- tracing-futures:
futures-proxy-server
: Demonstrates the use oftracing-futures
by implementing a simple proxy server, based on this example fromtokio
.futures-spawn
: A simple demonstration of the relationship between the spans representing spawned futures.
- tracing-tower:
tower-h2-client
: Demonstrates the use oftracing-tower
to instrument a simpletower-h2
HTTP/2 client (based on this example fromtower-h2
).tower-h2-server
: Demonstrates the use oftracing-tower
to instrument a simpletower-h2
HTTP/2 server (based on this example fromtower-h2
).
- tracing-serde:
serde-yak-shave
: Demonstrates the use oftracing-serde
by implementing a subscriber that emits trace output as JSON.
- tracing-log:
hyper-echo
: Demonstrates howtracing-log
can be used to record unstructured logs from dependencies astracing
events, by instrumenting this example fromhyper
, and usingtracing-log
to record logs emitted byhyper
.
The nightly-examples directory contains examples of how tracing
can be used
with async/await. These are kept separate as async/await is currently only
available on nightly Rust toolchains.