Skip to content

Commit dc6aafe

Browse files
authored
Setup tracing and document tracing usage (#12730)
1 parent 5107a50 commit dc6aafe

File tree

21 files changed

+513
-160
lines changed

21 files changed

+513
-160
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ flamegraph.svg
2121
# `CARGO_TARGET_DIR=target-llvm-lines RUSTFLAGS="-Csymbol-mangling-version=v0" cargo llvm-lines -p ruff --lib`
2222
/target*
2323

24+
# samply profiles
25+
profile.json
26+
27+
# tracing-flame traces
28+
tracing.folded
29+
tracing-flamechart.svg
30+
tracing-flamegraph.svg
31+
2432
###
2533
# Rust.gitignore
2634
###

Cargo.lock

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ thiserror = { version = "1.0.58" }
132132
tikv-jemallocator = { version = "0.6.0" }
133133
toml = { version = "0.8.11" }
134134
tracing = { version = "0.1.40" }
135+
tracing-flame = { version = "0.2.0" }
135136
tracing-indicatif = { version = "0.3.6" }
136-
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
137+
tracing-subscriber = { version = "0.3.18", default-features = false, features = ["env-filter", "fmt"] }
137138
tracing-tree = { version = "0.4.0" }
138139
typed-arena = { version = "2.0.2" }
139140
unic-ucd-category = { version = "0.9" }

crates/red_knot/Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ red_knot_server = { workspace = true }
1919
ruff_db = { workspace = true, features = ["os", "cache"] }
2020

2121
anyhow = { workspace = true }
22+
chrono = { workspace = true }
2223
clap = { workspace = true, features = ["wrap_help"] }
24+
colored = { workspace = true }
2325
countme = { workspace = true, features = ["enable"] }
2426
crossbeam = { workspace = true }
2527
ctrlc = { version = "3.4.4" }
2628
rayon = { workspace = true }
2729
salsa = { workspace = true }
28-
tracing = { workspace = true }
29-
tracing-subscriber = { workspace = true }
30+
tracing = { workspace = true, features = ["release_max_level_debug"] }
31+
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }
32+
tracing-flame = { workspace = true }
3033
tracing-tree = { workspace = true }
3134

3235
[dev-dependencies]
3336
filetime = { workspace = true }
3437
tempfile = { workspace = true }
3538

36-
3739
[lints]
3840
workspace = true
40.3 KB
Loading

crates/red_knot/docs/tracing.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Tracing
2+
3+
Traces are a useful tool to narrow down the location of a bug or, at least, to understand why the compiler is doing a particular thing.
4+
Note, tracing messages with severity `debug` or greater are user-facing. They should be phrased accordingly.
5+
Tracing spans are only shown when using `-vvv`.
6+
7+
## Verbosity levels
8+
9+
The CLI supports different verbosity levels.
10+
11+
- default: Only show errors and warnings.
12+
- `-v` activates `info!`: Show generally useful information such as paths of configuration files, detected platform, etc., but it's not a lot of messages, it's something you'll activate in CI by default. cargo build e.g. shows you which packages are fresh.
13+
- `-vv` activates `debug!` and timestamps: This should be enough information to get to the bottom of bug reports. When you're processing many packages or files, you'll get pages and pages of output, but each line is link to a specific action or state change.
14+
- `-vvv` activates `trace!` (only in debug builds) and shows tracing-spans: At this level, you're logging everything. Most of this is wasted, it's really slow, we dump e.g. the entire resolution graph. Only useful to developers, and you almost certainly want to use `RED_KNOT_LOG` to filter it down to the area your investigating.
15+
16+
## `RED_KNOT_LOG`
17+
18+
By default, the CLI shows messages from the `ruff` and `red_knot` crates. Tracing messages from other crates are not shown.
19+
The `RED_KNOT_LOG` environment variable allows you to customize which messages are shown by specifying one
20+
or more [filter directives](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives).
21+
22+
### Examples
23+
24+
#### Show all debug messages
25+
26+
Shows debug messages from all crates.
27+
28+
```bash
29+
RED_KNOT_LOG=debug
30+
```
31+
32+
#### Show salsa query execution messages
33+
34+
Show the salsa `execute: my_query` messages in addition to all red knot messages.
35+
36+
```bash
37+
RED_KNOT_LOG=ruff=trace,red_knot=trace,salsa=info
38+
```
39+
40+
#### Show typing traces
41+
42+
Only show traces for the `red_knot_python_semantic::types` module.
43+
44+
```bash
45+
RED_KNOT_LOG="red_knot_python_semantic::types"
46+
```
47+
48+
Note: Ensure that you use `-vvv` to see tracing spans.
49+
50+
#### Show messages for a single file
51+
52+
Shows all messages that are inside of a span for a specific file.
53+
54+
```bash
55+
RED_KNOT_LOG=red_knot[{file=/home/micha/astral/test/x.py}]=trace
56+
```
57+
58+
**Note**: Tracing still shows all spans because tracing can't know at the time of entering the span
59+
whether one if its children has the file `x.py`.
60+
61+
**Note**: Salsa currently logs the entire memoized values. In our case, the source text and parsed AST.
62+
This very quickly leads to extremely long outputs.
63+
64+
## Tracing and Salsa
65+
66+
Be mindful about using `tracing` in Salsa queries, especially when using `warn` or `error` because it isn't guaranteed
67+
that the query will execute after restoring from a persistent cache. In which case the user won't see the message.
68+
69+
For example, don't use `tracing` to show the user a message when generating a lint violation failed
70+
because the message would only be shown when linting the file the first time, but not on subsequent analysis
71+
runs or when restoring from a persistent cache. This can be confusing for users because they
72+
don't understand why a specific lint violation isn't raised. Instead, change your
73+
query to return the failure as part of the query's result or use a Salsa accumulator.
74+
75+
## Release builds
76+
77+
`trace!` events are removed in release builds.
78+
79+
## Profiling
80+
81+
Red Knot generates a folded stack trace to the current directory named `tracing.folded` when setting the environment variable `RED_KNOT_LOG_PROFILE` to `1` or `true`.
82+
83+
```bash
84+
RED_KNOT_LOG_PROFILE=1 red_knot -- --current-directory=../test -vvv
85+
```
86+
87+
You can convert the textual representation into a visual one using `inferno`.
88+
89+
```shell
90+
cargo install inferno
91+
```
92+
93+
```shell
94+
# flamegraph
95+
cat tracing.folded | inferno-flamegraph > tracing-flamegraph.svg
96+
97+
# flamechart
98+
cat tracing.folded | inferno-flamegraph --flamechart > tracing-flamechart.svg
99+
```
100+
101+
![Example flamegraph](./tracing-flamegraph.png)
102+
103+
See [`tracing-flame`](https://crates.io/crates/tracing-flame) for more details.

0 commit comments

Comments
 (0)