-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attributes: support adding arbitrary fields to the instrument macro (#…
…596) This PR adds support for adding arbitrary key/value pairs to be used as fields to `tracing::instrument`. Current syntax: ```rust #[instrument(fields(key = "value", v = 1, b = true, empty))] ``` - Empty keys are supported - If a key is not a single identifier, it's value is not a string/int/bool (or missing), is repeated or shares a name with a parameter, an error is reported Fixes: #573
- Loading branch information
Showing
2 changed files
with
196 additions
and
8 deletions.
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
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,63 @@ | ||
mod support; | ||
use support::*; | ||
|
||
use crate::support::field::mock; | ||
use crate::support::span::NewSpan; | ||
use tracing::subscriber::with_default; | ||
use tracing_attributes::instrument; | ||
|
||
#[instrument(fields(foo = "bar", dsa = true, num = 1))] | ||
fn fn_no_param() {} | ||
|
||
#[instrument(fields(foo = "bar"))] | ||
fn fn_param(param: u32) {} | ||
|
||
#[instrument(fields(foo = "bar", empty))] | ||
fn fn_empty_field() {} | ||
|
||
#[test] | ||
fn fields() { | ||
let span = span::mock().with_field( | ||
mock("foo") | ||
.with_value(&"bar") | ||
.and(mock("dsa").with_value(&true)) | ||
.and(mock("num").with_value(&1)) | ||
.only(), | ||
); | ||
run_test(span, || { | ||
fn_no_param(); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn parameters_with_fields() { | ||
let span = span::mock().with_field( | ||
mock("foo") | ||
.with_value(&"bar") | ||
.and(mock("param").with_value(&format_args!("1"))) | ||
.only(), | ||
); | ||
run_test(span, || { | ||
fn_param(1); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn empty_field() { | ||
let span = span::mock().with_field(mock("foo").with_value(&"bar").only()); | ||
run_test(span, || { | ||
fn_empty_field(); | ||
}); | ||
} | ||
|
||
fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) { | ||
let (subscriber, handle) = subscriber::mock() | ||
.new_span(span) | ||
.enter(span::mock()) | ||
.exit(span::mock()) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, fun); | ||
handle.assert_finished(); | ||
} |