• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1![Tracing — Structured, application-level diagnostics][splash]
2
3[splash]: https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/splash.svg
4
5# tracing-flame
6
7A [tracing] [`Layer`][`FlameLayer`] for generating a folded stack trace for generating flamegraphs
8and flamecharts with [`inferno`]
9
10[![Crates.io][crates-badge]][crates-url]
11[![Documentation][docs-badge]][docs-url]
12[![Documentation (master)][docs-master-badge]][docs-master-url]
13[![MIT licensed][mit-badge]][mit-url]
14[![Build Status][actions-badge]][actions-url]
15[![Discord chat][discord-badge]][discord-url]
16![maintenance status][maint-badge]
17
18[Documentation][docs-url] | [Chat][discord-url]
19
20# Overview
21
22[`tracing`] is a framework for instrumenting Rust programs to collect
23scoped, structured, and async-aware diagnostics. `tracing-flame` provides helpers
24for consuming `tracing` instrumentation that can later be visualized as a
25flamegraph/flamechart. Flamegraphs/flamecharts are useful for identifying performance
26bottlenecks in an application. For more details, see Brendan Gregg's [post]
27on flamegraphs.
28
29*Compiler support: [requires `rustc` 1.49+][msrv]*
30
31[msrv]: #supported-rust-versions
32[post]: http://www.brendangregg.com/flamegraphs.html
33
34## Usage
35
36This crate is meant to be used in a two step process:
37
381. Capture textual representation of the spans that are entered and exited
39   with [`FlameLayer`].
402. Feed the textual representation into `inferno-flamegraph` to generate the
41   flamegraph or flamechart.
42
43*Note*: when using a buffered writer as the writer for a `FlameLayer`, it is necessary to
44ensure that the buffer has been flushed before the data is passed into
45[`inferno-flamegraph`]. For more details on how to flush the internal writer
46of the `FlameLayer`, see the docs for [`FlushGuard`].
47
48## Layer Setup
49
50```rust
51use tracing_flame::FlameLayer;
52use tracing_subscriber::{prelude::*, fmt};
53
54fn setup_global_subscriber() -> impl Drop {
55    let fmt_layer = fmt::Layer::default();
56
57    let (flame_layer, _guard) = FlameLayer::with_file("./tracing.folded").unwrap();
58
59    tracing_subscriber::registry()
60        .with(fmt_layer)
61        .with(flame_layer)
62        .init().
63    _guard
64}
65
66// your code here ..
67```
68
69As an alternative, you can provide _any_ type that implements `std::io::Write` to
70`FlameLayer::new`.
71
72## Generating the Image
73
74To convert the textual representation of a flamegraph to a visual one, first install `inferno`:
75
76```console
77cargo install inferno
78```
79
80Then, pass the file created by `FlameLayer` into `inferno-flamegraph`:
81
82```console
83# flamegraph
84cat tracing.folded | inferno-flamegraph > tracing-flamegraph.svg
85
86# flamechart
87cat tracing.folded | inferno-flamegraph --flamechart > tracing-flamechart.svg
88```
89
90## Differences between `flamegraph`s and `flamechart`s
91
92By default, `inferno-flamegraph` creates flamegraphs. Flamegraphs operate by
93that collapsing identical stack frames and sorting them on the frame's names.
94
95This behavior is great for multithreaded programs and long-running programs
96where the same frames occur _many_ times, for short durations, because it reduces
97noise in the graph and gives the reader a better idea of the
98overall time spent in each part of the application.
99
100However, it is sometimes desirable to preserve the _exact_ ordering of events
101as they were emitted by `tracing-flame`, so that it is clear when each
102span is entered relative to others and get an accurate visual trace of
103the execution of your program. This representation is best created with a
104_flamechart_, which _does not_ sort or collapse identical stack frames.
105
106## Supported Rust Versions
107
108Tracing is built against the latest stable release. The minimum supported
109version is 1.49. The current Tracing version is not guaranteed to build on Rust
110versions earlier than the minimum supported version.
111
112Tracing follows the same compiler support policies as the rest of the Tokio
113project. The current stable Rust compiler and the three most recent minor
114versions before it will always be supported. For example, if the current stable
115compiler version is 1.45, the minimum supported version will not be increased
116past 1.42, three minor versions prior. Increasing the minimum supported compiler
117version is not considered a semver breaking change as long as doing so complies
118with this policy.
119
120## License
121
122This project is licensed under the [MIT license](LICENSE).
123
124### Contribution
125
126Unless you explicitly state otherwise, any contribution intentionally submitted
127for inclusion in Tracing by you, shall be licensed as MIT, without any additional
128terms or conditions.
129
130[`inferno`]: https://docs.rs/inferno
131[`FlameLayer`]: https://docs.rs/tracing-flame/*/tracing_flame/struct.FlameLayer.html
132[`FlushGuard`]: https://docs.rs/tracing-flame/*/tracing_flame/struct.FlushGuard.html
133[`inferno-flamegraph`]: https://docs.rs/inferno/0.9.5/inferno/index.html#producing-a-flame-graph
134[`tracing`]: https://github.com/tokio-rs/tracing/tree/master/tracing
135[crates-badge]: https://img.shields.io/crates/v/tracing-flame.svg
136[crates-url]: https://crates.io/crates/tracing-flame
137[docs-badge]: https://docs.rs/tracing-flame/badge.svg
138[docs-url]: https://docs.rs/tracing-flame/0.2.6
139[docs-master-badge]: https://img.shields.io/badge/docs-master-blue
140[docs-master-url]: https://tracing-rs.netlify.com/tracing_flame
141[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
142[mit-url]: LICENSE
143[actions-badge]: https://github.com/tokio-rs/tracing/workflows/CI/badge.svg
144[actions-url]:https://github.com/tokio-rs/tracing/actions?query=workflow%3ACI
145[discord-badge]: https://img.shields.io/discord/500028886025895936?logo=discord&label=discord&logoColor=white
146[discord-url]: https://discord.gg/EeF3cQw
147[maint-badge]: https://img.shields.io/badge/maintenance-experimental-blue.svg
148