Skip to content

Commit

Permalink
tokio-trace: Extend span! rules (#893)
Browse files Browse the repository at this point in the history
* tokio-trace: Extend `span!` rules

Add the ability to define a target and log level for
individual spans.

`level: ...` must be included since both `$lvl` and `$name` are `expr`.

The macro tests and documentation tests have been extended.

Signed-off-by: Kevin Leimkuhler <kevinl@buoyant.io>

* Add additional `span!` test

Change default log level of `span!` to `TRACE` to reflect the default
level used in `callsite!`.

Signed-off-by: Kevin Leimkuhler <kevinl@buoyant.io>
  • Loading branch information
kleimkuhler authored and hawkw committed Feb 12, 2019
1 parent 93e66e6 commit 4b163eb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
54 changes: 45 additions & 9 deletions tokio-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ macro_rules! callsite {
/// # }
/// ```
///
/// Note that a trailing comma on the final field is valid.
/// Note that a trailing comma on the final field is valid:
/// ```
/// # #[macro_use]
/// # extern crate tokio_trace;
Expand All @@ -443,9 +443,22 @@ macro_rules! callsite {
/// "my span",
/// foo = 2,
/// bar = "a string",
/// ).enter(|| {
/// // do work inside the span...
/// });
/// );
/// # }
/// ```
///
/// Creating a span with custom target and log level:
/// ```
/// # #[macro_use]
/// # extern crate tokio_trace;
/// # fn main() {
/// span!(
/// target: "app_span",
/// level: tokio_trace::Level::TRACE,
/// "my span",
/// foo = 3,
/// bar = "another string"
/// );
/// # }
/// ```
///
Expand Down Expand Up @@ -475,15 +488,19 @@ macro_rules! callsite {
/// ```
#[macro_export]
macro_rules! span {
($name:expr) => { span!($name,) };
($name:expr, $($k:ident $( = $val:expr )* ),*,) => {
span!($name, $($k $( = $val)* ),*)
(target: $target:expr, level: $lvl:expr, $name:expr, $($k:ident $( = $val:expr )* ),*,) => {
span!(target: $target, level: $lvl, $name, $($k $( = $val)*),*)
};
($name:expr, $($k:ident $( = $val:expr )* ),*) => {
(target: $target:expr, level: $lvl:expr, $name:expr, $($k:ident $( = $val:expr )* ),*) => {
{
use $crate::{callsite, field::{Value, ValueSet, AsField}, Span};
use $crate::callsite::Callsite;
let callsite = callsite! { name: $name, fields: $( $k ),* };
let callsite = callsite! {
name: $name,
target: $target,
level: $lvl,
fields: $($k),*
};
if is_enabled!(callsite) {
let meta = callsite.metadata();
Span::new(meta, &valueset!(meta.fields(), $($k $( = $val)*),*))
Expand All @@ -492,6 +509,25 @@ macro_rules! span {
}
}
};
(target: $target:expr, level: $lvl:expr, $name:expr) => {
span!(target: $target, level: $lvl, $name,)
};
(level: $lvl:expr, $name:expr, $($k:ident $( = $val:expr )* ),*,) => {
span!(target: module_path!(), level: $lvl, $name, $($k $( = $val)*),*)
};
(level: $lvl:expr, $name:expr, $($k:ident $( = $val:expr )* ),*) => {
span!(target: module_path!(), level: $lvl, $name, $($k $( = $val)*),*)
};
(level: $lvl:expr, $name:expr) => {
span!(target: module_path!(), level: $lvl, $name,)
};
($name:expr, $($k:ident $( = $val:expr)*),*,) => {
span!(target: module_path!(), level: $crate::Level::TRACE, $name, $($k $( = $val)*),*)
};
($name:expr, $($k:ident $( = $val:expr)*),*) => {
span!(target: module_path!(), level: $crate::Level::TRACE, $name, $($k $( = $val)*),*)
};
($name:expr) => { span!(target: module_path!(), level: $crate::Level::TRACE, $name,) };
}

/// Constructs a new `Event`.
Expand Down
16 changes: 16 additions & 0 deletions tokio-trace/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ extern crate tokio_trace;
// they're compile-time tests, so I want to get line numbers etc out of
// failures, and producing them with a macro would muddy the waters a bit.

#[test]
fn span() {
span!(target: "foo_events", level: tokio_trace::Level::DEBUG, "foo", bar = 2, baz = 3);
span!(target: "foo_events", level: tokio_trace::Level::DEBUG, "foo", bar = 2, baz = 4,);
span!(target: "foo_events", level: tokio_trace::Level::DEBUG, "foo");
span!(target: "foo_events", level: tokio_trace::Level::DEBUG, "bar",);
span!(level: tokio_trace::Level::DEBUG, "foo", bar = 2, baz = 3);
span!(level: tokio_trace::Level::DEBUG, "foo", bar = 2, baz = 4,);
span!(level: tokio_trace::Level::DEBUG, "foo");
span!(level: tokio_trace::Level::DEBUG, "bar",);
span!("foo", bar = 2, baz = 3);
span!("foo", bar = 2, baz = 4,);
span!("foo");
span!("bar",);
}

#[test]
fn event() {
event!(tokio_trace::Level::DEBUG, foo = 3, bar = 2, baz = false);
Expand Down
20 changes: 20 additions & 0 deletions tokio-trace/tests/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::thread;
use tokio_trace::{
dispatcher,
field::{debug, display},
subscriber::with_default,
Dispatch, Level, Span,
};

Expand Down Expand Up @@ -456,3 +457,22 @@ fn add_fields_only_after_new_span() {

handle.assert_finished();
}

#[test]
fn new_span_with_target_and_log_level() {
let (subscriber, handle) = subscriber::mock()
.new_span(
span::mock()
.named("foo")
.with_target("app_span")
.at_level(tokio_trace::Level::DEBUG),
)
.done()
.run_with_handle();

with_default(subscriber, || {
span!(target: "app_span", level: tokio_trace::Level::DEBUG, "foo");
});

handle.assert_finished();
}

0 comments on commit 4b163eb

Please sign in to comment.