|
| 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 | + |
| 102 | + |
| 103 | +See [`tracing-flame`](https://crates.io/crates/tracing-flame) for more details. |
0 commit comments