Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace: Allow trace instrumentation to emit log records #992

Merged
merged 38 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
63fdcb0
appears to work...
hawkw Mar 18, 2019
44919a7
add names to spans
hawkw Mar 18, 2019
6b62e1b
add names to span log messages
hawkw Mar 18, 2019
179ffa6
prettify log formatting a tad
hawkw Mar 18, 2019
ea10e0f
add test for log formatting
hawkw Mar 18, 2019
6f38117
make log support tests nicer, run on Azure
hawkw Mar 18, 2019
933b664
fix `__tokio_trace_log` always expecting `=`
hawkw Mar 19, 2019
0666a8b
maybe fix build?
hawkw Mar 19, 2019
c2b5614
maybe fix macros
hawkw Mar 19, 2019
e1a53e1
fix test-not-workiness with --all-features
hawkw Mar 19, 2019
4cb1fc9
trace: Don't add punctuation to log messages
hawkw Mar 20, 2019
725439b
update tests too!
hawkw Mar 20, 2019
4675f82
trace: Log when spans are entered/exited as well
hawkw Mar 20, 2019
eb90924
trace: Remove metadata field from `Inner`
hawkw Mar 20, 2019
996b7fb
simplify log compat feature flags
hawkw Mar 21, 2019
5bfd2f3
handle env var in build script
hawkw Mar 21, 2019
a04f67b
fixup tests not compiling
hawkw Mar 21, 2019
fc46da2
fix build script not actually enabling trace
hawkw Mar 21, 2019
163fcfe
only enable trace when env var is set
hawkw Mar 21, 2019
6a57fca
only print warning if a subscriber would enable a span
hawkw Mar 21, 2019
438023c
enable trace feature flag when running tests on CI
hawkw Mar 21, 2019
47011e0
add logging to record
hawkw Mar 22, 2019
eb5a1a0
clean up code for making spans log their fields
hawkw Mar 22, 2019
3d9e9e4
Merge branch 'master' into eliza/log-backcompat
hawkw Mar 22, 2019
9d179e2
fix move into closure
hawkw Mar 22, 2019
b6c76f5
Merge branch 'eliza/log-backcompat' of https://github.com/tokio-rs/to…
hawkw Mar 22, 2019
f29b5e4
trace-core: `DebugValue`/`DisplayValue` forward debug
hawkw Mar 22, 2019
ab798ae
fix debug impl requiring display
hawkw Mar 22, 2019
dd99ea3
fix examples link in README
hawkw Mar 22, 2019
cd4ba9f
Revert "fix examples link in README"
hawkw Mar 22, 2019
8bcf54b
get rid of unused feature flags
hawkw Mar 22, 2019
e948562
trace: Give spans slightly more useful debug impls
hawkw Mar 23, 2019
3ca54ff
Fix FreeBSD CI build
hawkw Mar 25, 2019
f84385f
Merge branch 'master' into eliza/log-backcompat
hawkw Mar 25, 2019
8031bb8
actually enable trace on cirrus
hawkw Mar 25, 2019
e71c14c
Merge branch 'master' into eliza/log-backcompat
hawkw Mar 25, 2019
7380ce0
trace: Remove `TOKIO_TRACE_ENABLED` env var
hawkw Mar 26, 2019
6b32b53
rm unneeded doc(hidden) macros
hawkw Mar 26, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ task:
test_script:
- . $HOME/.cargo/env
- cargo test --all --no-fail-fast
- (cd tokio-trace/test-log-support && cargo test)
- (cd tokio-trace/test_static_max_level_features && cargo test)
- cargo doc --all
i686_test_script:
Expand Down
1 change: 1 addition & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
- tokio-timer
- tokio-trace
- tokio-trace/tokio-trace-core
- tokio-trace/test-log-support
- tokio-trace/test_static_max_level_features

- template: ci/azure-cargo-check.yml
Expand Down
11 changes: 6 additions & 5 deletions tokio-trace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ publish = false

[dependencies]
tokio-trace-core = "0.1"
log = { version = "0.4", optional = true }
cfg-if = "0.1.7"

[dev-dependencies]
ansi_term = "0.11"
humantime = "1.1.1"
futures = "0.1"
log = "0.4"
# These are used for the "basic" example from the tokio-trace-prototype repo,
# which is currently not included as it used the `tokio-trace-log` crate, and
# that crate is currently unstable.
# env_logger = "0.5"
# tokio-trace-log = { path = "../tokio-trace-log" }

[features]
max_level_off = []
Expand All @@ -39,8 +45,3 @@ release_max_level_info = []
release_max_level_debug = []
release_max_level_trace = []

# These are used for the "basic" example from the tokio-trace-prototype repo,
# which is currently not included as it used the `tokio-trace-log` crate, and
# that crate is currently unstable.
# env_logger = "0.5"
# tokio-trace-log = { path = "../tokio-trace-log" }
2 changes: 1 addition & 1 deletion tokio-trace/examples/sloggish/sloggish_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl SloggishSubscriber {
stderr: io::stderr(),
stack: Mutex::new(vec![]),
spans: Mutex::new(HashMap::new()),
ids: AtomicUsize::new(0),
ids: AtomicUsize::new(1),
}
}

Expand Down
4 changes: 4 additions & 0 deletions tokio-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@
extern crate cfg_if;
extern crate tokio_trace_core;

#[cfg(feature = "log")]
#[doc(hidden)]
pub extern crate log;

// Somehow this `use` statement is necessary for us to re-export the `core`
// macros on Rust 1.26.0. I'm not sure how this makes it work, but it does.
#[allow(unused_imports)]
Expand Down
152 changes: 122 additions & 30 deletions tokio-trace/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ macro_rules! span {
$name:expr,
$($k:ident $( = $val:expr )* ),*
) => {
if $lvl <= $crate::level_filters::STATIC_MAX_LEVEL {
{
use $crate::callsite;
use $crate::callsite::Callsite;
let callsite = callsite! {
Expand All @@ -110,18 +110,20 @@ macro_rules! span {
level: $lvl,
fields: $($k),*
};
if is_enabled!(callsite) {
let meta = callsite.metadata();
let meta = callsite.metadata();

if $lvl <= $crate::level_filters::STATIC_MAX_LEVEL && is_enabled!(callsite) {
$crate::Span::child_of(
$parent,
meta,
&valueset!(meta.fields(), $($k $( = $val)*),*),
)
} else {
$crate::Span::new_disabled()
__tokio_trace_disabled_span!(
meta,
&valueset!(meta.fields(), $($k $( = $val)*),*)
)
}
} else {
$crate::Span::new_disabled()
}
};
(
Expand All @@ -130,7 +132,7 @@ macro_rules! span {
$name:expr,
$($k:ident $( = $val:expr )* ),*
) => {
if $lvl <= $crate::level_filters::STATIC_MAX_LEVEL {
{
use $crate::callsite;
use $crate::callsite::Callsite;
let callsite = callsite! {
Expand All @@ -139,18 +141,21 @@ macro_rules! span {
level: $lvl,
fields: $($k),*
};
if is_enabled!(callsite) {
let meta = callsite.metadata();
let meta = callsite.metadata();

if $lvl <= $crate::level_filters::STATIC_MAX_LEVEL && is_enabled!(callsite) {
$crate::Span::new(
meta,
&valueset!(meta.fields(), $($k $( = $val)*),*),
)
} else {
$crate::Span::new_disabled()
__tokio_trace_disabled_span!(
meta,
&valueset!(meta.fields(), $($k $( = $val)*),*)
)
}
} else {
$crate::Span::new_disabled()
}

};
(target: $target:expr, level: $lvl:expr, parent: $parent:expr, $name:expr) => {
span!(target: $target, level: $lvl, parent: $parent, $name,)
Expand Down Expand Up @@ -338,24 +343,32 @@ macro_rules! span {
#[macro_export(local_inner_macros)]
macro_rules! event {
(target: $target:expr, $lvl:expr, { $( $k:ident = $val:expr ),* $(,)*} )=> ({
if $lvl <= $crate::level_filters::STATIC_MAX_LEVEL {
#[allow(unused_imports)]
use $crate::{callsite, dispatcher, Event, field::{Value, ValueSet}};
use $crate::callsite::Callsite;
let callsite = callsite! {
name: __tokio_trace_concat!(
"event ",
__tokio_trace_file!(),
":",
__tokio_trace_line!()
),
{
__tokio_trace_log!(
target: $target,
level: $lvl,
fields: $( $k ),*
};
if is_enabled!(callsite) {
let meta = callsite.metadata();
Event::dispatch(meta, &valueset!(meta.fields(), $( $k = $val),* ));
$lvl,
$( $k = $val ),*
);

if $lvl <= $crate::level_filters::STATIC_MAX_LEVEL {
#[allow(unused_imports)]
use $crate::{callsite, dispatcher, Event, field::{Value, ValueSet}};
use $crate::callsite::Callsite;
let callsite = callsite! {
name: __tokio_trace_concat!(
"event ",
__tokio_trace_file!(),
":",
__tokio_trace_line!()
),
target: $target,
level: $lvl,
fields: $( $k ),*
};
if is_enabled!(callsite) {
let meta = callsite.metadata();
Event::dispatch(meta, &valueset!(meta.fields(), $( $k = $val),* ));
}
}
}
});
Expand Down Expand Up @@ -526,7 +539,7 @@ macro_rules! trace {
/// let pos = Position { x: 3.234, y: -1.223 };
///
/// debug!(x = field::debug(pos.x), y = field::debug(pos.y));
/// debug!(target: "app_events", { position = field::debug(pos) }, "New position");
/// debug!(target: "app_events", { position = field::debug(&pos) }, "New position");
/// # }
/// ```
#[macro_export(local_inner_macros)]
Expand Down Expand Up @@ -1007,3 +1020,82 @@ macro_rules! __tokio_trace_stringify {
stringify!($s)
};
}

#[cfg(feature = "log")]
#[doc(hidden)]
#[macro_export]
macro_rules! level_to_log {
($level:expr) => {
match $level {
$crate::Level::ERROR => $crate::log::Level::Error,
$crate::Level::WARN => $crate::log::Level::Warn,
$crate::Level::INFO => $crate::log::Level::Info,
$crate::Level::DEBUG => $crate::log::Level::Debug,
_ => $crate::log::Level::Trace,
}
};
}

#[cfg(feature = "log")]
#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! __tokio_trace_log {
(target: $target:expr, $level:expr, $( $key:ident $( = $val:expr )* ),* $(,)* ) => {
use $crate::log;
let level = level_to_log!($level);
if level <= log::STATIC_MAX_LEVEL {
let log_meta = log::Metadata::builder()
.level(level)
.target($target)
.build();
let logger = log::logger();
if logger.enabled(&log_meta) {
logger.log(&log::Record::builder()
.file(Some(__tokio_trace_file!()))
.module_path(Some(__tokio_trace_module_path!()))
.line(Some(__tokio_trace_line!()))
.metadata(log_meta)
.args(__tokio_trace_format_args!(
__tokio_trace_concat!(
$(__tokio_trace_log!(@key $key)),*
),
$(
__tokio_trace_log!(@val_or $key $( = $val)* )
),*
))
.build());
}
}
};
(@key message) => { "{} " };
(@key $key:ident) => { __tokio_trace_concat!(__tokio_trace_stringify!( $key ), "={:?} ") };
(@val_or $k:ident = $v:expr) => { $v };
(@val_or $k:ident ) => { __tokio_trace_format_args!("?") };
}

#[cfg(not(feature = "log"))]
#[doc(hidden)]
#[macro_export]
macro_rules! __tokio_trace_log {
(target: $target:expr, $level:expr, $( $key:ident $( = $val:expr )* ),* $(,)* ) => {};
}

#[cfg(feature = "log")]
#[doc(hidden)]
#[macro_export]
macro_rules! __tokio_trace_disabled_span {
($meta:expr, $valueset:expr) => {{
let mut span = $crate::Span::new_disabled($meta);
span.record_all(&$valueset);
span
}};
}

#[cfg(not(feature = "log"))]
#[doc(hidden)]
#[macro_export]
macro_rules! __tokio_trace_disabled_span {
($meta:expr, $valueset:expr) => {
$crate::Span::new_disabled($meta)
};
}
Loading