11# Tracing
22
3- The feature "otel" can be used when building rustup to turn on Opentelemetry
4- tracing with an OLTP GRPC exporter .
3+ Similar to other tools in the Rust ecosystem like Rustc and Cargo,
4+ Rustup also provides observability/logging features via the ` tracing ` crate .
55
6- This can be very useful for diagnosing performance or correctness issues in more
7- complicated scenarios.
6+ The verbosity of logs is controlled via the ` RUSTUP_LOG ` environment
7+ variable using ` tracing_subscriber ` 's [ directive syntax] .
8+
9+ [ directive syntax ] : https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives
10+
11+ ## Console-based tracing
12+
13+ A ` tracing_subscriber ` that prints log lines directly to ` stderr ` is directly
14+ available in the prebuilt version of Rustup since v1.28.0.
15+
16+ For historical reasons, if ` RUSTUP_LOG ` is not set, this subscriber will print
17+ the log lines in a format that mimics the "legacy" ` stderr ` output in older
18+ versions of Rustup:
19+
20+ ``` console
21+ > rustup default stable
22+ info: using existing install for 'stable-aarch64-apple-darwin'
23+ info: default toolchain set to 'stable-aarch64-apple-darwin'
24+
25+ stable-aarch64-apple-darwin unchanged - rustc 1.79.0 (129f3b996 2024-06-10)
26+ ```
27+
28+ However, once ` RUSTUP_LOG ` is set to any value, Rustup's "custom logging mode" will
29+ be activated, and ` tracing_subscriber ` 's builtin output format will be used instead:
30+
31+ ``` console
32+ > RUSTUP_LOG=trace rustup default stable
33+ 2024-06-16T12:08:48.732894Z INFO rustup::cli::common: using existing install for 'stable-aarch64-apple-darwin'
34+ 2024-06-16T12:08:48.739232Z INFO rustup::cli::common: default toolchain set to 'stable-aarch64-apple-darwin'
35+
36+ stable-aarch64-apple-darwin unchanged - rustc 1.79.0 (129f3b996 2024-06-10)
37+ ```
38+
39+ Please note that since ` RUSTUP_LOG=trace ` essentially accepts log lines from
40+ all possible sources, you might sometimes see log lines coming from Rustup's
41+ dependencies, such as ` hyper_util ` in the following example:
42+
43+ ``` console
44+ > RUSTUP_LOG=trace rustup update
45+ [..]
46+ 2024-06-16T12:12:45.569428Z TRACE hyper_util::client::legacy::client: http1 handshake complete, spawning background dispatcher task
47+ 2024-06-16T12:12:45.648682Z TRACE hyper_util::client::legacy::pool: pool dropped, dropping pooled (("https", static.rust-lang.org))
848
9- ## Prerequisites
49+ stable-aarch64-apple-darwin unchanged - rustc 1.79.0 (129f3b996 2024-06-10)
50+ nightly-aarch64-apple-darwin unchanged - rustc 1.81.0-nightly (3cf924b93 2024-06-15)
1051
11- ` protoc ` must be installed, which can be downloaded from GitHub or installed via package manager.
52+ 2024-06-16T12:12:45.693350Z INFO rustup::cli::rustup_mode: cleaning up downloads & tmp directories
53+ ```
54+
55+ ### Caveats
56+
57+ Due to historical reasons, the names of logging levels in Rustup's "legacy" ` stderr `
58+ output and the names being used in the ` tracing ` crate are slightly different:
59+
60+ | Legacy | ` tracing ` |
61+ | ---------- | --------- |
62+ | ` debug: ` | ` TRACE ` |
63+ | ` verbose: ` | ` DEBUG ` |
64+ | ` info: ` | ` INFO ` |
65+ | ` warning: ` | ` WARN ` |
66+ | ` error: ` | ` ERROR ` |
67+
68+ When specifying a particular logging level using the ` RUSTUP_LOG ` environment variable,
69+ the ` tracing ` version should be used in all cases.
70+ For example, set ` RUSTUP_LOG=rustup=DEBUG ` to receive log lines from ` rustup ` itself
71+ with a maximal verbosity of ` DEBUG ` .
72+
73+ ## Opentelemetry tracing
74+
75+ > ** Prerequisites:** Before following the instructions in this section,
76+ > ` protoc ` must be installed, which can be downloaded from GitHub
77+ > or installed via a package manager.
78+
79+ The feature ` otel ` can be used when building rustup to turn on Opentelemetry
80+ tracing with an OLTP GRPC exporter.
1281
13- ## Usage
82+ This can be very useful for diagnosing performance or correctness issues in more
83+ complicated scenarios.
1484
1585The normal [ OTLP environment
1686variables] ( https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md )
@@ -21,31 +91,24 @@ run a Jaeger docker container on the same host:
2191docker run -d --name jaeger -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 -e COLLECTOR_OTLP_ENABLED=true -p 6831:6831/udp -p 6832:6832/udp -p 5778:5778 -p 16686:16686 -p 4317:4317 -p 4318:4318 -p 14250:14250 -p 14268:14268 -p 14269:14269 -p 9411:9411 jaegertracing/all-in-one:latest
2292```
2393
24- Then build rustup-init with tracing:
94+ Then build ` rustup-init ` with tracing:
2595
2696``` sh
2797cargo build --features=otel
2898```
2999
30- Run the operation you want to analyze:
100+ Run the operation you want to analyze. For example, we can now run ` rustup show ` with tracing :
31101
32102``` sh
33103RUSTUP_FORCE_ARG0=" rustup" ./target/debug/rustup-init show
34104```
35105
36106And [ look in Jaeger for a trace] ( http://localhost:16686/search?service=rustup ) .
37107
38- ## Tracing and tests
39-
40108Tracing can also be used in tests to get a trace of the operations taken during the test.
109+ To use this feature, build the project with ` --features=otel,test ` .
41110
42- The custom macro ` rustup_macros::test ` adds a prelude and suffix to each test to
43- ensure that there is a tracing context setup, that the test function is a span,
44- and that the spans from the test are flushed.
45-
46- Build with features=otel,test to use this feature.
47-
48- ## Adding instrumentation
111+ ### Adding instrumentation
49112
50113The ` otel ` feature uses conditional compilation to only add function instrument
51114when enabled. Instrumenting a currently uninstrumented function is mostly simply
@@ -71,11 +134,3 @@ Some good general heuristics:
71134- Be way of debug build timing - release optimisations make a huge difference,
72135 though debug is a lot faster to iterate on. If something isn't a problem in
73136 release don't pay it too much heed in debug.
74-
75- ## Caveats
76-
77- Cross-thread propagation isn't connected yet. This will cause instrumentation in
78- a thread to make a new root span until it is fixed. If any Tokio runtime-related
79- code gets added in those threads this will also cause a panic. We have a couple
80- of threadpools in use today; if you need to instrument within that context, use
81- a thunk to propagate the tokio runtime into those threads.
0 commit comments