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
810
9- ## Prerequisites
11+ ## Console-based tracing
1012
11- ` protoc ` must be installed, which can be downloaded from GitHub or installed via package manager.
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+ ```
1227
13- ## Usage
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))
48+
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)
51+
52+ 2024-06-16T12:12:45.693350Z INFO rustup::cli::rustup_mode: cleaning up downloads & tmp directories
53+ ```
54+
55+ It is also possible to limit the sources of the log lines and the desired
56+ max level for each source. For example, set ` RUSTUP_LOG=rustup=DEBUG ` to
57+ receive log lines only from ` rustup ` itself with a max verbosity of ` DEBUG ` .
58+
59+ ## Opentelemetry tracing
60+
61+ > ** Prerequisites:** Before following the instructions in this section,
62+ > ` protoc ` must be installed, which can be downloaded from GitHub
63+ > or installed via a package manager.
64+
65+ The feature ` otel ` can be used when building rustup to turn on Opentelemetry
66+ tracing with an OLTP GRPC exporter.
67+
68+ This can be very useful for diagnosing performance or correctness issues in more
69+ complicated scenarios.
1470
1571The normal [ OTLP environment
1672variables] ( https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md )
@@ -21,31 +77,24 @@ run a Jaeger docker container on the same host:
2177docker 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
2278```
2379
24- Then build rustup-init with tracing:
80+ Then build ` rustup-init ` with tracing:
2581
2682``` sh
2783cargo build --features=otel
2884```
2985
30- Run the operation you want to analyze:
86+ Run the operation you want to analyze. For example, we can now run ` rustup show ` with tracing :
3187
3288``` sh
3389RUSTUP_FORCE_ARG0=" rustup" ./target/debug/rustup-init show
3490```
3591
3692And [ look in Jaeger for a trace] ( http://localhost:16686/search?service=rustup ) .
3793
38- ## Tracing and tests
39-
4094Tracing can also be used in tests to get a trace of the operations taken during the test.
95+ To use this feature, build the project with ` --features=otel,test ` .
4196
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
97+ ### Adding instrumentation
4998
5099The ` otel ` feature uses conditional compilation to only add function instrument
51100when enabled. Instrumenting a currently uninstrumented function is mostly simply
@@ -71,11 +120,3 @@ Some good general heuristics:
71120- Be way of debug build timing - release optimisations make a huge difference,
72121 though debug is a lot faster to iterate on. If something isn't a problem in
73122 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