Skip to content

Commit b38122d

Browse files
jsgfdavidbarsky
authored andcommitted
attributes: improve docs; tests for using Levels in #[instrument] (#2350)
This branch adds documentation and tests noting that the `#[instrument]` macro accepts `tracing::Level` directly. Using `tracing::Level` directly allows for IDE autocomplete and earlier detection of typos. The documentation for tracing-attributes was also rewritten to remove the usage of the second-person perspective, making it more consistent with the rest of tracing's documentation. Co-authored-by: David Barsky <[email protected]> ; Conflicts: ; tracing-attributes/Cargo.toml ; tracing-attributes/src/lib.rs
1 parent 89d0ca8 commit b38122d

File tree

10 files changed

+85
-57
lines changed

10 files changed

+85
-57
lines changed

tracing-attributes/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ quote = "1"
4747
tracing = { path = "../tracing", version = "0.1.35" }
4848
tracing-mock = { path = "../tracing-mock", features = ["tokio-test"] }
4949
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3.0", features = ["env-filter"] }
50-
tokio-test = { version = "0.3.0" }
50+
tokio-test = "0.4.2"
5151
tracing-core = { path = "../tracing-core", version = "0.1.28"}
5252
async-trait = "0.1.56"
5353
trybuild = "1.0.64"

tracing-attributes/src/lib.rs

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//!
1313
//! ## Usage
1414
//!
15-
//! First, add this to your `Cargo.toml`:
15+
//! In the `Cargo.toml`:
1616
//!
1717
//! ```toml
1818
//! [dependencies]
@@ -24,7 +24,7 @@
2424
//! called. For example:
2525
//!
2626
//! ```
27-
//! use tracing_attributes::instrument;
27+
//! use tracing::instrument;
2828
//!
2929
//! #[instrument]
3030
//! pub fn my_function(my_arg: usize) {
@@ -204,16 +204,17 @@ mod expand;
204204
///
205205
/// # Adding Fields
206206
///
207-
/// Additional fields (key-value pairs with arbitrary data) may be added to the
208-
/// generated span using the `fields` argument on the `#[instrument]` macro. Any
207+
/// Additional fields (key-value pairs with arbitrary data) can be passed to
208+
/// to the generated span through the `fields` argument on the
209+
/// `#[instrument]` macro. Strings, integers or boolean literals are accepted values
210+
/// for each field. The name of the field must be a single valid Rust
211+
/// identifier, nested (dotted) field names are not supported. Any
209212
/// Rust expression can be used as a field value in this manner. These
210213
/// expressions will be evaluated at the beginning of the function's body, so
211214
/// arguments to the function may be used in these expressions. Field names may
212215
/// also be specified *without* values. Doing so will result in an [empty field]
213216
/// whose value may be recorded later within the function body.
214217
///
215-
/// This supports the same [field syntax] as the `span!` and `event!` macros.
216-
///
217218
/// Note that overlap between the names of fields and (non-skipped) arguments
218219
/// will result in a compile error.
219220
///
@@ -323,11 +324,15 @@ mod expand;
323324
/// Setting the level for the generated span:
324325
/// ```
325326
/// # use tracing_attributes::instrument;
326-
/// #[instrument(level = "debug")]
327+
/// # use tracing::Level;
328+
/// #[instrument(level = Level::DEBUG)]
327329
/// pub fn my_function() {
328330
/// // ...
329331
/// }
330332
/// ```
333+
/// Levels can be specified either with [`Level`] constants, literal strings
334+
/// (e.g., `"debug"`, `"info"`) or numerically (1—5, corresponding to [`Level::TRACE`]—[`Level::ERROR`]).
335+
///
331336
/// Overriding the generated span's name:
332337
/// ```
333338
/// # use tracing_attributes::instrument;
@@ -398,7 +403,7 @@ mod expand;
398403
/// }
399404
/// ```
400405
///
401-
/// To add an additional context to the span, pass key-value pairs to `fields`:
406+
/// To add additional context to the span, pass key-value pairs to `fields`:
402407
///
403408
/// ```
404409
/// # use tracing_attributes::instrument;
@@ -426,7 +431,8 @@ mod expand;
426431
///
427432
/// ```
428433
/// # use tracing_attributes::instrument;
429-
/// #[instrument(ret(level = "warn"))]
434+
/// # use tracing::Level;
435+
/// #[instrument(ret(level = Level::WARN))]
430436
/// fn my_function() -> i32 {
431437
/// 42
432438
/// }
@@ -447,8 +453,8 @@ mod expand;
447453
/// }
448454
/// ```
449455
///
450-
/// If the function returns a `Result<T, E>` and `E` implements `std::fmt::Display`, you can add
451-
/// `err` or `err(Display)` to emit error events when the function returns `Err`:
456+
/// If the function returns a `Result<T, E>` and `E` implements `std::fmt::Display`, adding
457+
/// `err` or `err(Display)` will emit error events when the function returns `Err`:
452458
///
453459
/// ```
454460
/// # use tracing_attributes::instrument;
@@ -458,19 +464,20 @@ mod expand;
458464
/// }
459465
/// ```
460466
///
461-
/// Similarly, you can override the level of the `err` event:
467+
/// Similarly, overriding the level of the `err` event :
462468
///
463469
/// ```
464470
/// # use tracing_attributes::instrument;
465-
/// #[instrument(err(level = "info"))]
471+
/// # use tracing::Level;
472+
/// #[instrument(err(level = Level::INFO))]
466473
/// fn my_function(arg: usize) -> Result<(), std::io::Error> {
467474
/// Ok(())
468475
/// }
469476
/// ```
470477
///
471478
/// By default, error values will be recorded using their `std::fmt::Display` implementations.
472479
/// If an error implements `std::fmt::Debug`, it can be recorded using its `Debug` implementation
473-
/// instead, by writing `err(Debug)`:
480+
/// instead by writing `err(Debug)`:
474481
///
475482
/// ```
476483
/// # use tracing_attributes::instrument;
@@ -529,33 +536,6 @@ mod expand;
529536
/// }
530537
/// ```
531538
///
532-
/// Note than on `async-trait` <= 0.1.43, references to the `Self`
533-
/// type inside the `fields` argument were only allowed when the instrumented
534-
/// function is a method (i.e., the function receives `self` as an argument).
535-
/// For example, this *used to not work* because the instrument function
536-
/// didn't receive `self`:
537-
/// ```
538-
/// # use tracing::instrument;
539-
/// use async_trait::async_trait;
540-
///
541-
/// #[async_trait]
542-
/// pub trait Bar {
543-
/// async fn bar();
544-
/// }
545-
///
546-
/// #[derive(Debug)]
547-
/// struct BarImpl(usize);
548-
///
549-
/// #[async_trait]
550-
/// impl Bar for BarImpl {
551-
/// #[instrument(fields(tmp = std::any::type_name::<Self>()))]
552-
/// async fn bar() {}
553-
/// }
554-
/// ```
555-
/// Instead, you should manually rewrite any `Self` types as the type for
556-
/// which you implement the trait: `#[instrument(fields(tmp = std::any::type_name::<Bar>()))]`
557-
/// (or maybe you can just bump `async-trait`).
558-
///
559539
/// [span]: https://docs.rs/tracing/latest/tracing/span/index.html
560540
/// [name]: https://docs.rs/tracing/latest/tracing/struct.Metadata.html#method.name
561541
/// [target]: https://docs.rs/tracing/latest/tracing/struct.Metadata.html#method.target
@@ -567,6 +547,9 @@ mod expand;
567547
/// [`follows_from`]: https://docs.rs/tracing/latest/tracing/struct.Span.html#method.follows_from
568548
/// [`tracing`]: https://github.com/tokio-rs/tracing
569549
/// [`fmt::Debug`]: std::fmt::Debug
550+
/// [`Level`]: https://docs.rs/tracing/latest/tracing/struct.Level.html
551+
/// [`Level::TRACE`]: https://docs.rs/tracing/latest/tracing/struct.Level.html#associatedconstant.TRACE
552+
/// [`Level::ERROR`]: https://docs.rs/tracing/latest/tracing/struct.Level.html#associatedconstant.ERROR
570553
#[proc_macro_attribute]
571554
pub fn instrument(
572555
args: proc_macro::TokenStream,

tracing-attributes/tests/err.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,15 @@ fn err_info() -> Result<u8, TryFromIntError> {
240240
#[test]
241241
fn test_err_info() {
242242
let span = span::mock().named("err_info");
243-
let (collector, handle) = collector::mock()
243+
let (subscriber, handle) = subscriber::mock()
244244
.new_span(span.clone())
245245
.enter(span.clone())
246246
.event(event::mock().at_level(Level::INFO))
247247
.exit(span.clone())
248248
.drop_span(span)
249249
.done()
250250
.run_with_handle();
251-
with_default(collector, || err_info().ok());
251+
with_default(subscriber, || err_info().ok());
252252
handle.assert_finished();
253253
}
254254

@@ -260,7 +260,7 @@ fn err_dbg_info() -> Result<u8, TryFromIntError> {
260260
#[test]
261261
fn test_err_dbg_info() {
262262
let span = span::mock().named("err_dbg_info");
263-
let (collector, handle) = collector::mock()
263+
let (subscriber, handle) = subscriber::mock()
264264
.new_span(span.clone())
265265
.enter(span.clone())
266266
.event(
@@ -277,7 +277,7 @@ fn test_err_dbg_info() {
277277
.drop_span(span)
278278
.done()
279279
.run_with_handle();
280-
with_default(collector, || err_dbg_info().ok());
280+
with_default(subscriber, || err_dbg_info().ok());
281281
handle.assert_finished();
282282
}
283283

@@ -289,14 +289,14 @@ fn err_warn_info() -> Result<u8, TryFromIntError> {
289289
#[test]
290290
fn test_err_warn_info() {
291291
let span = span::mock().named("err_warn_info").at_level(Level::WARN);
292-
let (collector, handle) = collector::mock()
292+
let (subscriber, handle) = subscriber::mock()
293293
.new_span(span.clone())
294294
.enter(span.clone())
295295
.event(event::mock().at_level(Level::INFO))
296296
.exit(span.clone())
297297
.drop_span(span)
298298
.done()
299299
.run_with_handle();
300-
with_default(collector, || err_warn_info().ok());
300+
with_default(subscriber, || err_warn_info().ok());
301301
handle.assert_finished();
302302
}

tracing-attributes/tests/instrument.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn override_everything() {
1717
#[instrument(target = "my_target", level = "debug")]
1818
fn my_fn() {}
1919

20-
#[instrument(level = "debug", target = "my_target")]
20+
#[instrument(level = Level::DEBUG, target = "my_target")]
2121
fn my_other_fn() {}
2222

2323
let span = span::mock()

tracing-attributes/tests/levels.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,49 @@ fn numeric_levels() {
9494

9595
handle.assert_finished();
9696
}
97+
98+
#[test]
99+
fn enum_levels() {
100+
#[instrument(level = Level::TRACE)]
101+
fn trace() {}
102+
103+
#[instrument(level = Level::DEBUG)]
104+
fn debug() {}
105+
106+
#[instrument(level = tracing::Level::INFO)]
107+
fn info() {}
108+
109+
#[instrument(level = Level::WARN)]
110+
fn warn() {}
111+
112+
#[instrument(level = Level::ERROR)]
113+
fn error() {}
114+
let (subscriber, handle) = subscriber::mock()
115+
.new_span(span::mock().named("trace").at_level(Level::TRACE))
116+
.enter(span::mock().named("trace").at_level(Level::TRACE))
117+
.exit(span::mock().named("trace").at_level(Level::TRACE))
118+
.new_span(span::mock().named("debug").at_level(Level::DEBUG))
119+
.enter(span::mock().named("debug").at_level(Level::DEBUG))
120+
.exit(span::mock().named("debug").at_level(Level::DEBUG))
121+
.new_span(span::mock().named("info").at_level(Level::INFO))
122+
.enter(span::mock().named("info").at_level(Level::INFO))
123+
.exit(span::mock().named("info").at_level(Level::INFO))
124+
.new_span(span::mock().named("warn").at_level(Level::WARN))
125+
.enter(span::mock().named("warn").at_level(Level::WARN))
126+
.exit(span::mock().named("warn").at_level(Level::WARN))
127+
.new_span(span::mock().named("error").at_level(Level::ERROR))
128+
.enter(span::mock().named("error").at_level(Level::ERROR))
129+
.exit(span::mock().named("error").at_level(Level::ERROR))
130+
.done()
131+
.run_with_handle();
132+
133+
with_default(subscriber, || {
134+
trace();
135+
debug();
136+
info();
137+
warn();
138+
error();
139+
});
140+
141+
handle.assert_finished();
142+
}

tracing-attributes/tests/ret.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ fn ret_warn_info() -> i32 {
262262
#[test]
263263
fn test_warn_info() {
264264
let span = span::mock().named("ret_warn_info").at_level(Level::WARN);
265-
let (collector, handle) = collector::mock()
265+
let (subscriber, handle) = subscriber::mock()
266266
.new_span(span.clone())
267267
.enter(span.clone())
268268
.event(
@@ -275,7 +275,7 @@ fn test_warn_info() {
275275
.done()
276276
.run_with_handle();
277277

278-
with_default(collector, ret_warn_info);
278+
with_default(subscriber, ret_warn_info);
279279
handle.assert_finished();
280280
}
281281

@@ -287,7 +287,7 @@ fn ret_dbg_warn() -> i32 {
287287
#[test]
288288
fn test_dbg_warn() {
289289
let span = span::mock().named("ret_dbg_warn").at_level(Level::INFO);
290-
let (collector, handle) = collector::mock()
290+
let (subscriber, handle) = subscriber::mock()
291291
.new_span(span.clone())
292292
.enter(span.clone())
293293
.event(
@@ -300,6 +300,6 @@ fn test_dbg_warn() {
300300
.done()
301301
.run_with_handle();
302302

303-
with_default(collector, ret_dbg_warn);
303+
with_default(subscriber, ret_dbg_warn);
304304
handle.assert_finished();
305305
}

tracing-core/src/field.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ pub trait Visit {
315315
/// <strong>Note</strong>: This is only enabled when the Rust standard library is
316316
/// present.
317317
/// </pre>
318+
/// </div>
318319
#[cfg(feature = "std")]
319320
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
320321
fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) {

tracing-flame/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@
133133
while_true
134134
)]
135135

136-
pub use error::Error;
137-
136+
use error::Error;
138137
use error::Kind;
139138
use once_cell::sync::Lazy;
140139
use std::cell::Cell;

tracing-futures/src/executor/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#[cfg(feature = "futures-01")]
22
mod futures_01;
3-
#[cfg(feature = "futures-01")]
4-
pub use self::futures_01::*;
53

64
#[cfg(feature = "futures_preview")]
75
mod futures_preview;

tracing/src/field.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ use crate::Metadata;
126126
/// string comparisons. Thus, if possible, once the key for a field is known, it
127127
/// should be used whenever possible.
128128
/// </pre>
129+
/// </div>
129130
pub trait AsField: crate::sealed::Sealed {
130131
/// Attempts to convert `&self` into a `Field` with the specified `metadata`.
131132
///

0 commit comments

Comments
 (0)