-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setup tracing and document tracing usage #12730
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Tracing | ||
|
||
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. | ||
Note, tracing messages with severity `debug`..`error` are user-facing. They should be phrased accordingly. | ||
MichaReiser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Tracing spans are only shown when using `-vvv`. | ||
|
||
## Verbosity levels | ||
|
||
The CLI supports different verbosity levels. | ||
|
||
- default: Only show errors and warnings. | ||
- `-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. | ||
- `-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. | ||
- `-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. | ||
|
||
## `RED_KNOT_LOG` | ||
|
||
By default, the CLI shows messages from the `ruff` and `red_knot` crates. Tracing messages from other crates are not shown. | ||
The `RED_KNOT_LOG` environment variable allows you to customize which messages are shown by specifying one | ||
or more [filter directives](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives). | ||
|
||
### Examples | ||
|
||
#### Show all debug messages | ||
|
||
Shows debug messages from all crates. | ||
|
||
```bash | ||
RED_KNOT_LOG=debug | ||
``` | ||
|
||
#### Show salsa query execution messages | ||
|
||
Show the salsa `execute: my_query` messages in addition to all red knot messages. | ||
|
||
```bash | ||
RED_KNOT_LOG=ruff=trace,red_knot=trace,salsa=info | ||
``` | ||
|
||
#### Show typing traces | ||
|
||
Only show traces for the `red_knot_python_semantic::types` module. | ||
|
||
```bash | ||
RED_KNOT_LOG="red_knot_python_semantic::types" | ||
``` | ||
|
||
Note: Ensure that you use `-vvv` to see tracing spans. | ||
|
||
#### Show messages for a single file | ||
|
||
Shows all messages that are inside of a span for a specific file. | ||
|
||
```bash | ||
RED_KNOT_LOG=red_knot[{file=/home/micha/astral/test/x.py}]=trace | ||
``` | ||
|
||
**Note**: Tracing still shows all spans because tracing can't know at the time of entering the span | ||
whether one if its children has the file `x.py`. | ||
|
||
**Note**: Salsa currently logs the entire memoized values. In our case, the source text and parsed AST. | ||
This very quickly leads to extremely long outputs. | ||
|
||
## Tracing and Salsa | ||
|
||
Be mindful about using `tracing` in Salsa queries, especially when using `warn` or `error` because it isn't guaranteed | ||
that the query will execute after restoring from a persistent cache. In which case the user won't see the message. | ||
|
||
For example, don't use `tracing` to show the user a message when generating a lint violation failed | ||
because the message would only be shown when linting the file the first time, but not on subsequent analysis | ||
runs or when restoring from a persistent cache. This can be confusing for users because they | ||
don't understand why a specific lint violation isn't raised. Instead, change your | ||
query to return the failure as part of the query's result or use a Salsa accumulator. | ||
|
||
## Release builds | ||
|
||
`trace!` events are removed in release builds. | ||
|
||
## Profiling | ||
|
||
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`. | ||
|
||
```bash | ||
RED_KNOT_LOG_PROFILE=1 red_knot -- --current-directory=../test -vvv | ||
``` | ||
|
||
You can convert the textual representation into a visual one using `inferno`. | ||
|
||
```shell | ||
cargo install inferno | ||
``` | ||
|
||
```shell | ||
# flamegraph | ||
cat tracing.folded | inferno-flamegraph > tracing-flamegraph.svg | ||
|
||
# flamechart | ||
cat tracing.folded | inferno-flamegraph --flamechart > tracing-flamechart.svg | ||
``` | ||
|
||
![Example flamegraph](./tracing-flamegraph.png) | ||
|
||
See [`tracing-flame`](https://crates.io/crates/tracing-flame) for more details. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume that the
crates/red_knot/docs/...
directory is for developer-facing docs, not user-facing docs?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, that's certainly true I don't know about long-term. We could have
docs/users
anddocs/dev
if we end up with needing both. But most user-facing documentation should probably live on the website.