Skip to content

Commit 3d96c0f

Browse files
authored
docs: document valuable support (#1887)
Depends on #1881 Right now, the `valuable` stuff isn't very discoverable --- enabling the feature just adds some trait impls and stuff that aren't particularly visible in the documentation. This PR adds some top-level docs on using `valuable`. In particular: - Added a section to the `tracing` and `tracing-core` lib.rs docs explaining the unstable features versioning policy and how to turn on unstable features - Added a section in the `field` module that explains how to use `valuable` to record fields. - It turns out the `tracing::field` module didn't really have docs, since it doesn't re-export the `tracing_core::field` module but re-exports its _types_ in a new module (because it adds a trait). It had a single line of docs that just said something about "structured key-value data". I fixed this by coping the docs from `tracing-core`. :/ - Enabled unstable features in the documentation on docs.rs and netlify.
1 parent 67d4547 commit 3d96c0f

File tree

8 files changed

+270
-13
lines changed

8 files changed

+270
-13
lines changed

netlify.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
[build.environment]
99
RUSTDOCFLAGS="""
1010
-D warnings \
11-
--cfg docsrs
11+
--cfg docsrs \
12+
--cfg tracing_unstable
1213
"""
14+
RUSTFLAGS="--cfg tracing_unstable"
1315

1416
[[redirects]]
1517
from = "/"

tracing-core/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ name = "tracing-core"
88
# - README.md
99
# - Update CHANGELOG.md.
1010
# - Create "v0.1.x" git tag.
11-
version = "0.1.21"
11+
version = "0.1.22"
1212
authors = ["Tokio Contributors <team@tokio.rs>"]
1313
license = "MIT"
1414
readme = "README.md"
@@ -41,4 +41,8 @@ valuable = { version = "0.1.0", optional = true, default_features = false }
4141

4242
[package.metadata.docs.rs]
4343
all-features = true
44-
rustdoc-args = ["--cfg", "docsrs"]
44+
# enable unstable features in the documentation
45+
rustdoc-args = ["--cfg", "docsrs", "--cfg", "tracing_unstable"]
46+
# it's necessary to _also_ pass `--cfg tracing_unstable` to rustc, or else
47+
# dependencies will not be enabled, and the docs build will fail.
48+
rustc-args = ["--cfg", "tracing_unstable"]

tracing-core/src/field.rs

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Span and `Event` key-value data.
1+
//! `Span` and `Event` key-value data.
22
//!
33
//! Spans and events may be annotated with key-value data, referred to as known
44
//! as _fields_. These fields consist of a mapping from a key (corresponding to
@@ -24,8 +24,81 @@
2424
//! recorded as typed data by calling the [`Value::record`] method on these
2525
//! trait objects with a _visitor_ implementing the [`Visit`] trait. This trait
2626
//! represents the behavior used to record values of various types. For example,
27-
//! we might record integers by incrementing counters for their field names,
28-
//! rather than printing them.
27+
//! an implementation of `Visit` might record integers by incrementing counters
28+
//! for their field names rather than printing them.
29+
//!
30+
//!
31+
//! # Using `valuable`
32+
//!
33+
//! `tracing`'s [`Value`] trait is intentionally minimalist: it supports only a small
34+
//! number of Rust primitives as typed values, and only permits recording
35+
//! user-defined types with their [`fmt::Debug`] or [`fmt::Display`]
36+
//! implementations. However, there are some cases where it may be useful to record
37+
//! nested values (such as arrays, `Vec`s, or `HashMap`s containing values), or
38+
//! user-defined `struct` and `enum` types without having to format them as
39+
//! unstructured text.
40+
//!
41+
//! To address `Value`'s limitations, `tracing` offers experimental support for
42+
//! the [`valuable`] crate, which provides object-safe inspection of structured
43+
//! values. User-defined types can implement the [`valuable::Valuable`] trait,
44+
//! and be recorded as a `tracing` field by calling their [`as_value`] method.
45+
//! If the [`Subscriber`] also supports the `valuable` crate, it can
46+
//! then visit those types fields as structured values using `valuable`.
47+
//!
48+
//! <pre class="ignore" style="white-space:normal;font:inherit;">
49+
//! <strong>Note</strong>: <code>valuable</code> support is an
50+
//! <a href = "../index.html#unstable-features">unstable feature</a>. See
51+
//! the documentation on unstable features for details on how to enable it.
52+
//! </pre>
53+
//!
54+
//! For example:
55+
//! ```ignore
56+
//! // Derive `Valuable` for our types:
57+
//! use valuable::Valuable;
58+
//!
59+
//! #[derive(Clone, Debug, Valuable)]
60+
//! struct User {
61+
//! name: String,
62+
//! age: u32,
63+
//! address: Address,
64+
//! }
65+
//!
66+
//! #[derive(Clone, Debug, Valuable)]
67+
//! struct Address {
68+
//! country: String,
69+
//! city: String,
70+
//! street: String,
71+
//! }
72+
//!
73+
//! let user = User {
74+
//! name: "Arwen Undomiel".to_string(),
75+
//! age: 3000,
76+
//! address: Address {
77+
//! country: "Middle Earth".to_string(),
78+
//! city: "Rivendell".to_string(),
79+
//! street: "leafy lane".to_string(),
80+
//! },
81+
//! };
82+
//!
83+
//! // Recording `user` as a `valuable::Value` will allow the `tracing` subscriber
84+
//! // to traverse its fields as a nested, typed structure:
85+
//! tracing::info!(current_user = user.as_value());
86+
//! ```
87+
//!
88+
//! Alternatively, the [`valuable()`] function may be used to convert a type
89+
//! implementing [`Valuable`] into a `tracing` field value.
90+
//!
91+
//! When the `valuable` feature is enabled, the [`Visit`] trait will include an
92+
//! optional [`record_value`] method. `Visit` implementations that wish to
93+
//! record `valuable` values can implement this method with custom behavior.
94+
//! If a visitor does not implement `record_value`, the [`valuable::Value`] will
95+
//! be forwarded to the visitor's [`record_debug`] method.
96+
//!
97+
//! [`valuable`]: https://crates.io/crates/valuable
98+
//! [`as_value`]: valuable::Valuable::as_value
99+
//! [`Subscriber`]: crate::Subscriber
100+
//! [`record_value`]: Visit::record_value
101+
//! [`record_debug`]: Visit::record_debug
29102
//!
30103
//! [`Value`]: trait.Value.html
31104
//! [span]: ../span/

tracing-core/src/lib.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
//! be used with the `tracing` ecosystem. It includes a collection of
4444
//! `Subscriber` implementations, as well as utility and adapter crates.
4545
//!
46-
//! ### Crate Feature Flags
46+
//! ## Crate Feature Flags
4747
//!
48-
//! The following crate feature flags are available:
48+
//! The following crate [feature flags] are available:
4949
//!
5050
//! * `std`: Depend on the Rust standard library (enabled by default).
5151
//!
@@ -58,6 +58,37 @@
5858
//!
5959
//! **Note**:`tracing-core`'s `no_std` support requires `liballoc`.
6060
//!
61+
//! ### Unstable Features
62+
//!
63+
//! These feature flags enable **unstable** features. The public API may break in 0.1.x
64+
//! releases. To enable these features, the `--cfg tracing_unstable` must be passed to
65+
//! `rustc` when compiling.
66+
//!
67+
//! The following unstable feature flags are currently available:
68+
//!
69+
//! * `valuable`: Enables support for recording [field values] using the
70+
//! [`valuable`] crate.
71+
//!
72+
//! #### Enabling Unstable Features
73+
//!
74+
//! The easiest way to set the `tracing_unstable` cfg is to use the `RUSTFLAGS`
75+
//! env variable when running `cargo` commands:
76+
//!
77+
//! ```shell
78+
//! RUSTFLAGS="--cfg tracing_unstable" cargo build
79+
//! ```
80+
//! Alternatively, the following can be added to the `.cargo/config` file in a
81+
//! project to automatically enable the cfg flag for that project:
82+
//!
83+
//! ```toml
84+
//! [build]
85+
//! rustflags = ["--cfg", "tracing_unstable"]
86+
//! ```
87+
//!
88+
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
89+
//! [field values]: crate::field
90+
//! [`valuable`]: https://crates.io/crates/valuable
91+
//!
6192
//! ## Supported Rust Versions
6293
//!
6394
//! Tracing is built against the latest stable release. The minimum supported

tracing/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ edition = "2018"
2828
rust-version = "1.42.0"
2929

3030
[dependencies]
31-
tracing-core = { path = "../tracing-core", version = "0.1.21", default-features = false }
31+
tracing-core = { path = "../tracing-core", version = "0.1.22", default-features = false }
3232
log = { version = "0.4", optional = true }
3333
tracing-attributes = { path = "../tracing-attributes", version = "0.1.18", optional = true }
3434
cfg-if = "1.0.0"
@@ -81,4 +81,8 @@ maintenance = { status = "actively-developed" }
8181

8282
[package.metadata.docs.rs]
8383
all-features = true
84-
rustdoc-args = ["--cfg", "docsrs"]
84+
# enable unstable features in the documentation
85+
rustdoc-args = ["--cfg", "docsrs", "--cfg", "tracing_unstable"]
86+
# it's necessary to _also_ pass `--cfg tracing_unstable` to rustc, or else
87+
# dependencies will not be enabled, and the docs build will fail.
88+
rustc-args = ["--cfg", "tracing_unstable"]

tracing/src/field.rs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,116 @@
1-
//! Structured data associated with `Span`s and `Event`s.
1+
//! `Span` and `Event` key-value data.
2+
//!
3+
//! Spans and events may be annotated with key-value data, referred to as known
4+
//! as _fields_. These fields consist of a mapping from a key (corresponding to
5+
//! a `&str` but represented internally as an array index) to a [`Value`].
6+
//!
7+
//! # `Value`s and `Subscriber`s
8+
//!
9+
//! `Subscriber`s consume `Value`s as fields attached to [span]s or [`Event`]s.
10+
//! The set of field keys on a given span or is defined on its [`Metadata`].
11+
//! When a span is created, it provides [`Attributes`] to the `Subscriber`'s
12+
//! [`new_span`] method, containing any fields whose values were provided when
13+
//! the span was created; and may call the `Subscriber`'s [`record`] method
14+
//! with additional [`Record`]s if values are added for more of its fields.
15+
//! Similarly, the [`Event`] type passed to the subscriber's [`event`] method
16+
//! will contain any fields attached to each event.
17+
//!
18+
//! `tracing` represents values as either one of a set of Rust primitives
19+
//! (`i64`, `u64`, `f64`, `bool`, and `&str`) or using a `fmt::Display` or
20+
//! `fmt::Debug` implementation. `Subscriber`s are provided these primitive
21+
//! value types as `dyn Value` trait objects.
22+
//!
23+
//! These trait objects can be formatted using `fmt::Debug`, but may also be
24+
//! recorded as typed data by calling the [`Value::record`] method on these
25+
//! trait objects with a _visitor_ implementing the [`Visit`] trait. This trait
26+
//! represents the behavior used to record values of various types. For example,
27+
//! an implementation of `Visit` might record integers by incrementing counters
28+
//! for their field names rather than printing them.
29+
//!
30+
//!
31+
//! # Using `valuable`
32+
//!
33+
//! `tracing`'s [`Value`] trait is intentionally minimalist: it supports only a small
34+
//! number of Rust primitives as typed values, and only permits recording
35+
//! user-defined types with their [`fmt::Debug`] or [`fmt::Display`]
36+
//! implementations. However, there are some cases where it may be useful to record
37+
//! nested values (such as arrays, `Vec`s, or `HashMap`s containing values), or
38+
//! user-defined `struct` and `enum` types without having to format them as
39+
//! unstructured text.
40+
//!
41+
//! To address `Value`'s limitations, `tracing` offers experimental support for
42+
//! the [`valuable`] crate, which provides object-safe inspection of structured
43+
//! values. User-defined types can implement the [`valuable::Valuable`] trait,
44+
//! and be recorded as a `tracing` field by calling their [`as_value`] method.
45+
//! If the [`Subscriber`] also supports the `valuable` crate, it can
46+
//! then visit those types fields as structured values using `valuable`.
47+
//!
48+
//! <pre class="ignore" style="white-space:normal;font:inherit;">
49+
//! <strong>Note</strong>: <code>valuable</code> support is an
50+
//! <a href = "../index.html#unstable-features">unstable feature</a>. See
51+
//! the documentation on unstable features for details on how to enable it.
52+
//! </pre>
53+
//!
54+
//! For example:
55+
//! ```ignore
56+
//! // Derive `Valuable` for our types:
57+
//! use valuable::Valuable;
58+
//!
59+
//! #[derive(Clone, Debug, Valuable)]
60+
//! struct User {
61+
//! name: String,
62+
//! age: u32,
63+
//! address: Address,
64+
//! }
65+
//!
66+
//! #[derive(Clone, Debug, Valuable)]
67+
//! struct Address {
68+
//! country: String,
69+
//! city: String,
70+
//! street: String,
71+
//! }
72+
//!
73+
//! let user = User {
74+
//! name: "Arwen Undomiel".to_string(),
75+
//! age: 3000,
76+
//! address: Address {
77+
//! country: "Middle Earth".to_string(),
78+
//! city: "Rivendell".to_string(),
79+
//! street: "leafy lane".to_string(),
80+
//! },
81+
//! };
82+
//!
83+
//! // Recording `user` as a `valuable::Value` will allow the `tracing` subscriber
84+
//! // to traverse its fields as a nested, typed structure:
85+
//! tracing::info!(current_user = user.as_value());
86+
//! ```
87+
//!
88+
//! Alternatively, the [`valuable()`] function may be used to convert a type
89+
//! implementing [`Valuable`] into a `tracing` field value.
90+
//!
91+
//! When the `valuable` feature is enabled, the [`Visit`] trait will include an
92+
//! optional [`record_value`] method. `Visit` implementations that wish to
93+
//! record `valuable` values can implement this method with custom behavior.
94+
//! If a visitor does not implement `record_value`, the [`valuable::Value`] will
95+
//! be forwarded to the visitor's [`record_debug`] method.
96+
//!
97+
//! [`fmt::Debug`]: std::fmt::Debug
98+
//! [`fmt::Display`]: std::fmt::Debug
99+
//! [`valuable`]: https://crates.io/crates/valuable
100+
//! [`valuable::Valuable`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html
101+
//! [`as_value`]: https://docs.rs/valuable/latest/valuable/trait.Valuable.html#tymethod.as_value
102+
//! [`valuable::Value`]: https://docs.rs/valuable/latest/valuable/enum.Value.html
103+
//! [`Subscriber`]: crate::Subscriber
104+
//! [`record_value`]: Visit::record_value
105+
//! [`record_debug`]: Visit::record_debug
106+
//! [span]: mod@crate::span
107+
//! [`Event`]: crate::event::Event
108+
//! [`Metadata`]: crate::Metadata
109+
//! [`Attributes`]: crate::span::Attributes
110+
//! [`Record`]: crate::span::Record
111+
//! [`new_span`]: crate::Subscriber::new_span
112+
//! [`record`]: crate::Subscriber::record
113+
//! [`event`]: crate::Subscriber::event
2114
pub use tracing_core::field::*;
3115

4116
use crate::Metadata;

tracing/src/instrument.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub trait Instrument: Sized {
124124
}
125125

126126
/// Extension trait allowing futures to be instrumented with
127-
/// a `tracing` [`Subscriber`].
127+
/// a `tracing` [`Subscriber`](crate::Subscriber).
128128
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
129129
pub trait WithSubscriber: Sized {
130130
/// Attaches the provided [`Subscriber`] to this type, returning a

tracing/src/lib.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@
781781
//!
782782
//! ## Crate Feature Flags
783783
//!
784-
//! The following crate feature flags are available:
784+
//! The following crate [feature flags] are available:
785785
//!
786786
//! * A set of features controlling the [static verbosity level].
787787
//! * `log`: causes trace instrumentation points to emit [`log`] records as well
@@ -810,6 +810,37 @@
810810
//! requires <code>liballoc</code>.
811811
//! </pre>
812812
//!
813+
//! ### Unstable Features
814+
//!
815+
//! These feature flags enable **unstable** features. The public API may break in 0.1.x
816+
//! releases. To enable these features, the `--cfg tracing_unstable` must be passed to
817+
//! `rustc` when compiling.
818+
//!
819+
//! The following unstable feature flags are currently available:
820+
//!
821+
//! * `valuable`: Enables support for recording [field values] using the
822+
//! [`valuable`] crate.
823+
//!
824+
//! #### Enabling Unstable Features
825+
//!
826+
//! The easiest way to set the `tracing_unstable` cfg is to use the `RUSTFLAGS`
827+
//! env variable when running `cargo` commands:
828+
//!
829+
//! ```shell
830+
//! RUSTFLAGS="--cfg tracing_unstable" cargo build
831+
//! ```
832+
//! Alternatively, the following can be added to the `.cargo/config` file in a
833+
//! project to automatically enable the cfg flag for that project:
834+
//!
835+
//! ```toml
836+
//! [build]
837+
//! rustflags = ["--cfg", "tracing_unstable"]
838+
//! ```
839+
//!
840+
//! [feature flags]: https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section
841+
//! [field values]: crate::field
842+
//! [`valuable`]: https://crates.io/crates/valuable
843+
//!
813844
//! ## Supported Rust Versions
814845
//!
815846
//! Tracing is built against the latest stable release. The minimum supported

0 commit comments

Comments
 (0)