From cd7561e33366d4e4e184412a2c57aad342c0d36a Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 19 Dec 2020 06:22:52 +0900 Subject: [PATCH 1/6] chore: disable default features of tracing dependencies (#1144) This avoids relatively heavy dependencies (`tracing-attributes`, `syn`, etc.) in some cases. --- tracing-error/Cargo.toml | 2 +- tracing-flame/Cargo.toml | 2 +- tracing-opentelemetry/Cargo.toml | 2 +- tracing-subscriber/Cargo.toml | 2 +- tracing-tower/Cargo.toml | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tracing-error/Cargo.toml b/tracing-error/Cargo.toml index c4b0ad3e51..6a3abb2827 100644 --- a/tracing-error/Cargo.toml +++ b/tracing-error/Cargo.toml @@ -39,7 +39,7 @@ traced-error = [] [dependencies] tracing-subscriber = { path = "../tracing-subscriber", version = "0.2.7", default-features = false, features = ["registry", "fmt"] } -tracing = { path = "../tracing", version = "0.1.12"} +tracing = { path = "../tracing", version = "0.1.12", default-features = false, features = ["std"] } [badges] maintenance = { status = "experimental" } diff --git a/tracing-flame/Cargo.toml b/tracing-flame/Cargo.toml index c071b08505..dd2a994581 100644 --- a/tracing-flame/Cargo.toml +++ b/tracing-flame/Cargo.toml @@ -26,7 +26,7 @@ smallvec = ["tracing-subscriber/smallvec"] [dependencies] tracing-subscriber = { path = "../tracing-subscriber", version = "0.2.3", default-features = false, features = ["registry", "fmt"] } -tracing = { path = "../tracing", version = "0.1.12"} +tracing = { path = "../tracing", version = "0.1.12", default-features = false, features = ["std"] } lazy_static = "1.3.0" [dev-dependencies] diff --git a/tracing-opentelemetry/Cargo.toml b/tracing-opentelemetry/Cargo.toml index f7c74dfa6c..465d1587cd 100644 --- a/tracing-opentelemetry/Cargo.toml +++ b/tracing-opentelemetry/Cargo.toml @@ -23,7 +23,7 @@ default = ["tracing-log"] [dependencies] opentelemetry = { version = "0.12", default-features = false, features = ["trace"] } -tracing = { path = "../tracing", version = "0.1" } +tracing = { path = "../tracing", version = "0.1", default-features = false, features = ["std"] } tracing-core = { path = "../tracing-core", version = "0.1" } tracing-subscriber = { path = "../tracing-subscriber", version = "0.2", default-features = false, features = ["registry"] } tracing-log = { path = "../tracing-log", version = "0.1", default-features = false, optional = true } diff --git a/tracing-subscriber/Cargo.toml b/tracing-subscriber/Cargo.toml index f36d51aafd..8e0740a2b9 100644 --- a/tracing-subscriber/Cargo.toml +++ b/tracing-subscriber/Cargo.toml @@ -34,7 +34,7 @@ json = ["tracing-serde", "serde", "serde_json"] tracing-core = { path = "../tracing-core", version = "0.1.17" } # only required by the filter feature -tracing = { optional = true, path = "../tracing", version = "0.1" } +tracing = { optional = true, path = "../tracing", version = "0.1", default-features = false, features = ["std"] } matchers = { optional = true, version = "0.0.1" } regex = { optional = true, version = "1", default-features = false, features = ["std"] } smallvec = { optional = true, version = "1" } diff --git a/tracing-tower/Cargo.toml b/tracing-tower/Cargo.toml index 9ea739cc4e..5527bb2698 100644 --- a/tracing-tower/Cargo.toml +++ b/tracing-tower/Cargo.toml @@ -24,7 +24,7 @@ tower-make = [ ] [dependencies] -tracing = { path = "../tracing", version = "0.1"} +tracing = { path = "../tracing", version = "0.1", default-features = false, features = ["std"] } tracing-futures = { version = "0.2.1", path = "../tracing-futures", features = ["std-future"] } futures = "0.3" tower-service = "0.3" From ab631219b44294d68ced4c1fe291c9571e7cc24c Mon Sep 17 00:00:00 2001 From: Kamil Rojewski Date: Mon, 21 Dec 2020 20:47:44 +0100 Subject: [PATCH 2/6] docs: add tracing-elastic-apm to related crates (#1146) ## Motivation From the readme > (if you're the maintainer of a tracing ecosystem crate not in this list, please let us know!) ## Solution Added links to tracing-elastic-apm. Co-authored-by: Eliza Weisman --- README.md | 3 +++ tracing/src/lib.rs | 3 +++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index 0413843e3f..3d877c9a74 100644 --- a/README.md +++ b/README.md @@ -399,6 +399,7 @@ are not maintained by the `tokio` project. These include: - [`diesel-tracing`] provides integration with [`diesel`] database connections. - [`tracing-tracy`] provides a way to collect [Tracy] profiles in instrumented applications. +- [`tracing-elastic-apm`] provides a layer for reporting traces to [Elastic APM]. (if you're the maintainer of a `tracing` ecosystem crate not in this list, please let us know!) @@ -426,6 +427,8 @@ please let us know!) [`diesel-tracing`]: https://crates.io/crates/diesel-tracing [`tracing-tracy`]: https://crates.io/crates/tracing-tracy [Tracy]: https://github.com/wolfpld/tracy +[`tracing-elastic-apm`]: https://crates.io/crates/tracing-elastic-apm +[Elastic APM]: https://www.elastic.co/apm **Note:** that some of the ecosystem crates are currently unreleased and undergoing active development. They may be less stable than `tracing` and diff --git a/tracing/src/lib.rs b/tracing/src/lib.rs index 43975c87db..307d5bbd9a 100644 --- a/tracing/src/lib.rs +++ b/tracing/src/lib.rs @@ -722,6 +722,7 @@ //! - [`diesel-tracing`] provides integration with [`diesel`] database connections. //! - [`tracing-tracy`] provides a way to collect [Tracy] profiles in instrumented //! applications. +//! - [`tracing-elastic-apm`] provides a layer for reporting traces to [Elastic APM]. //! //! If you're the maintainer of a `tracing` ecosystem crate not listed above, //! please let us know! We'd love to add your project to the list! @@ -747,6 +748,8 @@ //! [`diesel-tracing`]: https://crates.io/crates/diesel-tracing //! [`tracing-tracy`]: https://crates.io/crates/tracing-tracy //! [Tracy]: https://github.com/wolfpld/tracy +//! [`tracing-elastic-apm`]: https://crates.io/crates/tracing-elastic-apm +//! [Elastic APM]: https://www.elastic.co/apm //! //!
//!
Note
From e038d5619211f79bcb4007a879357ebb429618f5 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 28 Jan 2021 09:16:32 -0800 Subject: [PATCH 3/6] tracing: check `log` max level prior to dispatch check (#1175) Currently, when the `log` feature is enabled but the `log-always` feature is not, we check the (dynamic, but inexpensive) `dispatch::has_been_set` variable **before** checking if the event's level would be enabled by `log`'s static max level features. Although this check is not terribly costly in terms of performance, it *does* result in code being generated even when the `log` crate disables some levels statically. This means that we will always generate *some* code when the `log` feature is disabled. This isn't ideal --- the static max level features should result in us generating *no* code whatsoever for disabled trace points. This commit moves the static max level check outside of the dispatch check. Now, we should generate 0 bytes of assembly when both `log` and `tracing` disable an event with their static filtering features. I've also updated the docs to explain the relationship between `tracing` and `log`'s separate static filtering. Fixes #1174 Signed-off-by: Eliza Weisman --- tracing/src/level_filters.rs | 17 ++++++++++-- tracing/src/macros.rs | 54 +++++++++++++++++++++--------------- tracing/src/span.rs | 37 ++++++++++++------------ 3 files changed, 65 insertions(+), 43 deletions(-) diff --git a/tracing/src/level_filters.rs b/tracing/src/level_filters.rs index df9405b5e4..886e19f856 100644 --- a/tracing/src/level_filters.rs +++ b/tracing/src/level_filters.rs @@ -33,10 +33,23 @@ //! [dependencies] //! tracing = { version = "0.1", features = ["max_level_debug", "release_max_level_warn"] } //! ``` +//! ## Notes //! -//! *Compiler support: requires rustc 1.39+* +//! Please note that `tracing`'s static max level features do *not* control the +//! [`log`] records that may be emitted when [`tracing`'s "log" feature flag][f] is +//! enabled. This is to allow `tracing` to be disabled entirely at compile time +//! while still emitting `log` records --- such as when a library using +//! `tracing` is used by an application using `log` that doesn't want to +//! generate any `tracing`-related code, but does want to collect `log` records. //! -//! [`log` crate]: https://docs.rs/log/0.4.6/log/#compile-time-filters +//! This means that if the "log" feature is in use, some code may be generated +//! for `log` records emitted by disabled `tracing` events. If this is not +//! desirable, `log` records may be disabled separately using [`log`'s static +//! max level features][`log` crate]. +//! +//! [`log`]: https://docs.rs/log/ +//! [`log` crate]: https://docs.rs/log/latest/log/#compile-time-filters +//! [f]: : https://docs.rs/tracing/latest/tracing/#emitting-log-records pub use tracing_core::{metadata::ParseLevelFilterError, LevelFilter}; /// The statically configured maximum trace level. diff --git a/tracing/src/macros.rs b/tracing/src/macros.rs index 3222abb741..968ad443c8 100644 --- a/tracing/src/macros.rs +++ b/tracing/src/macros.rs @@ -45,7 +45,7 @@ macro_rules! span { ) } else { let span = CALLSITE.disabled_span(); - $crate::if_log_enabled! {{ + $crate::if_log_enabled! { $lvl, { span.record_all(&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*)); }}; span @@ -75,7 +75,7 @@ macro_rules! span { ) } else { let span = CALLSITE.disabled_span(); - $crate::if_log_enabled! {{ + $crate::if_log_enabled! { $lvl, { span.record_all(&$crate::valueset!(CALLSITE.metadata().fields(), $($fields)*)); }}; span @@ -2295,10 +2295,10 @@ macro_rules! __mk_format_args { #[macro_export] macro_rules! __tracing_log { (target: $target:expr, $level:expr, $($field:tt)+ ) => { - $crate::if_log_enabled! {{ + $crate::if_log_enabled! { $level, { use $crate::log; let level = $crate::level_to_log!($level); - if level <= log::STATIC_MAX_LEVEL && level <= log::max_level() { + if level <= log::max_level() { let log_meta = log::Metadata::builder() .level(level) .target($target) @@ -2322,13 +2322,13 @@ macro_rules! __tracing_log { #[doc(hidden)] #[macro_export] macro_rules! if_log_enabled { - ($e:expr;) => { - $crate::if_log_enabled! { $e } + ($lvl:expr, $e:expr;) => { + $crate::if_log_enabled! { $lvl, $e } }; - ($if_log:block) => { - $crate::if_log_enabled! { $if_log else {} } + ($lvl:expr, $if_log:block) => { + $crate::if_log_enabled! { $lvl, $if_log else {} } }; - ($if_log:block else $else_block:block) => { + ($lvl:expr, $if_log:block else $else_block:block) => { $else_block }; } @@ -2337,15 +2337,19 @@ macro_rules! if_log_enabled { #[doc(hidden)] #[macro_export] macro_rules! if_log_enabled { - ($e:expr;) => { - $crate::if_log_enabled! { $e } + ($lvl:expr, $e:expr;) => { + $crate::if_log_enabled! { $lvl, $e } }; - ($if_log:block) => { - $crate::if_log_enabled! { $if_log else {} } + ($lvl:expr, $if_log:block) => { + $crate::if_log_enabled! { $lvl, $if_log else {} } }; - ($if_log:block else $else_block:block) => { - if !$crate::dispatcher::has_been_set() { - $if_log + ($lvl:expr, $if_log:block else $else_block:block) => { + if $crate::level_to_log!($lvl) <= $crate::log::STATIC_MAX_LEVEL { + if !$crate::dispatcher::has_been_set() { + $if_log + } else { + $else_block + } } else { $else_block } @@ -2356,14 +2360,18 @@ macro_rules! if_log_enabled { #[doc(hidden)] #[macro_export] macro_rules! if_log_enabled { - ($e:expr;) => { - $crate::if_log_enabled! { $e } + ($lvl:expr, $e:expr;) => { + $crate::if_log_enabled! { $lvl, $e } }; - ($if_log:block) => { - $crate::if_log_enabled! { $if_log else {} } + ($lvl:expr, $if_log:block) => { + $crate::if_log_enabled! { $lvl, $if_log else {} } }; - ($if_log:block else $else_block:block) => { - #[allow(unused_braces)] - $if_log + ($lvl:expr, $if_log:block else $else_block:block) => { + if $crate::level_to_log!($lvl) <= $crate::log::STATIC_MAX_LEVEL { + #[allow(unused_braces)] + $if_log + } else { + $else_block + } }; } diff --git a/tracing/src/span.rs b/tracing/src/span.rs index c1e0ee1a0c..02edc81565 100644 --- a/tracing/src/span.rs +++ b/tracing/src/span.rs @@ -544,7 +544,7 @@ impl Span { meta: Some(meta), }; - if_log_enabled! {{ + if_log_enabled! { *meta.level(), { let target = if attrs.is_empty() { LIFECYCLE_LOG_TARGET } else { @@ -555,6 +555,7 @@ impl Span { span } + /// Enters this span, returning a guard that will exit the span when dropped. /// /// If this span is enabled by the current subscriber, then this function will @@ -749,7 +750,7 @@ impl Span { inner.subscriber.enter(&inner.id); } - if_log_enabled! {{ + if_log_enabled! { crate::Level::TRACE, { if let Some(ref meta) = self.meta { self.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("-> {}", meta.name())); } @@ -916,16 +917,16 @@ impl Span { inner.record(&record); } - if_log_enabled! {{ - if let Some(ref meta) = self.meta { + if let Some(ref _meta) = self.meta { + if_log_enabled! { *_meta.level(), { let target = if record.is_empty() { LIFECYCLE_LOG_TARGET } else { - meta.target() + _meta.target() }; - self.log(target, level_to_log!(*meta.level()), format_args!("{}{}", meta.name(), FmtValues(&record))); - } - }} + self.log(target, level_to_log!(*_meta.level()), format_args!("{}{}", _meta.name(), FmtValues(&record))); + }} + } self } @@ -1146,15 +1147,15 @@ impl Drop for Span { subscriber.try_close(id.clone()); } - if_log_enabled!({ - if let Some(ref meta) = self.meta { + if let Some(ref _meta) = self.meta { + if_log_enabled! { crate::Level::TRACE, { self.log( LIFECYCLE_LOG_TARGET, log::Level::Trace, - format_args!("-- {}", meta.name()), + format_args!("-- {}", _meta.name()), ); - } - }) + }} + } } } @@ -1231,11 +1232,11 @@ impl<'a> Drop for Entered<'a> { inner.subscriber.exit(&inner.id); } - if_log_enabled! {{ - if let Some(ref meta) = self.span.meta { - self.span.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("<- {}", meta.name())); - } - }} + if let Some(ref _meta) = self.span.meta { + if_log_enabled! { crate::Level::TRACE, { + self.span.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("<- {}", _meta.name())); + }} + } } } From 3f1a8bc77210d0d87f5175a374ef49810ec4cbd1 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 28 Jan 2021 18:17:04 +0100 Subject: [PATCH 4/6] subscriber: add sample implementation of `FormatEvent` (#1189) * Add sample implementation of `FormatEvent` The docs previously didn't provide much help implementing `FormatEvent`. Especially getting access to fields on spans could be tricky unless the user was aware of `FormattedFields`. This updates the docs with a basic, but working, example. * Apply suggestions from code review Co-authored-by: Eliza Weisman * Add comment explaining what `FormattedFields` is * Expand docs a bit * Fix formatting Co-authored-by: Eliza Weisman --- tracing-subscriber/src/fmt/format/mod.rs | 68 ++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tracing-subscriber/src/fmt/format/mod.rs b/tracing-subscriber/src/fmt/format/mod.rs index 9020a6390f..c00825ac06 100644 --- a/tracing-subscriber/src/fmt/format/mod.rs +++ b/tracing-subscriber/src/fmt/format/mod.rs @@ -45,6 +45,74 @@ use fmt::{Debug, Display}; /// This trait is already implemented for function pointers with the same /// signature as `format_event`. /// +/// # Examples +/// +/// ```rust +/// use std::fmt::{self, Write}; +/// use tracing_core::{Subscriber, Event}; +/// use tracing_subscriber::fmt::{FormatEvent, FormatFields, FmtContext, FormattedFields}; +/// use tracing_subscriber::registry::LookupSpan; +/// +/// struct MyFormatter; +/// +/// impl FormatEvent for MyFormatter +/// where +/// S: Subscriber + for<'a> LookupSpan<'a>, +/// N: for<'a> FormatFields<'a> + 'static, +/// { +/// fn format_event( +/// &self, +/// ctx: &FmtContext<'_, S, N>, +/// writer: &mut dyn fmt::Write, +/// event: &Event<'_>, +/// ) -> fmt::Result { +/// // Write level and target +/// let level = *event.metadata().level(); +/// let target = event.metadata().target(); +/// write!( +/// writer, +/// "{} {}: ", +/// level, +/// target, +/// )?; +/// +/// // Write spans and fields of each span +/// ctx.visit_spans(|span| { +/// write!(writer, "{}", span.name())?; +/// +/// let ext = span.extensions(); +/// +/// // `FormattedFields` is a a formatted representation of the span's +/// // fields, which is stored in its extensions by the `fmt` layer's +/// // `new_span` method. The fields will have been formatted +/// // by the same field formatter that's provided to the event +/// // formatter in the `FmtContext`. +/// let fields = &ext +/// .get::>() +/// .expect("will never be `None`"); +/// +/// if !fields.is_empty() { +/// write!(writer, "{{{}}}", fields)?; +/// } +/// write!(writer, ": ")?; +/// +/// Ok(()) +/// })?; +/// +/// // Write fields on the event +/// ctx.field_format().format_fields(writer, event)?; +/// +/// writeln!(writer) +/// } +/// } +/// ``` +/// +/// This formatter will print events like this: +/// +/// ```text +/// DEBUG yak_shaving::shaver: some-span{field-on-span=foo}: started shaving yak +/// ``` +/// /// [`fmt::Subscriber`]: ../struct.Subscriber.html /// [`fmt::Layer`]: ../struct.Layer.html pub trait FormatEvent From e7e9c38e93782903af33cb1e1f75164b08540fdc Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sun, 18 Oct 2020 13:57:44 -0700 Subject: [PATCH 5/6] chore(deps): update pin-project requirement from 0.4 to 1.0 (#1038) Updates the requirements on [pin-project](https://github.com/taiki-e/pin-project) to permit the latest version. - [Release notes](https://github.com/taiki-e/pin-project/releases) - [Changelog](https://github.com/taiki-e/pin-project/blob/master/CHANGELOG.md) - [Commits](https://github.com/taiki-e/pin-project/compare/v0.4.0...v1.0.0) Signed-off-by: dependabot-preview[bot] Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> --- tracing-futures/Cargo.toml | 2 +- tracing-tower/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tracing-futures/Cargo.toml b/tracing-futures/Cargo.toml index d5e2a6a822..09f60f0765 100644 --- a/tracing-futures/Cargo.toml +++ b/tracing-futures/Cargo.toml @@ -28,7 +28,7 @@ std = ["tracing/std"] futures_01 = { package = "futures", version = "0.1", optional = true } futures = { version = "0.3.0", optional = true } futures-task = { version = "0.3", optional = true } -pin-project = { version = "0.4", optional = true } +pin-project = { version = "1.0", optional = true } tracing = { path = "../tracing", version = "0.1", default-features = false } tokio-executor = { version = "0.1", optional = true } tokio = { version = "0.1", optional = true } diff --git a/tracing-tower/Cargo.toml b/tracing-tower/Cargo.toml index 5527bb2698..29703d0f76 100644 --- a/tracing-tower/Cargo.toml +++ b/tracing-tower/Cargo.toml @@ -30,7 +30,7 @@ futures = "0.3" tower-service = "0.3" tower-layer = { version = "0.3", optional = true } tower_make = { package = "tower-make", version = "0.3", optional = true } -pin-project = { version = "0.4", optional = true } +pin-project = { version = "1.0", optional = true } http = { version = "0.2", optional = true } [badges] From a8ad1aac956cfa627fd20d6158ea203bcffd2824 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 28 Jan 2021 09:41:53 -0800 Subject: [PATCH 6/6] docs: fix broken link due to typo Signed-off-by: Eliza Weisman --- tracing/src/level_filters.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracing/src/level_filters.rs b/tracing/src/level_filters.rs index 886e19f856..cfacee2efe 100644 --- a/tracing/src/level_filters.rs +++ b/tracing/src/level_filters.rs @@ -49,7 +49,7 @@ //! //! [`log`]: https://docs.rs/log/ //! [`log` crate]: https://docs.rs/log/latest/log/#compile-time-filters -//! [f]: : https://docs.rs/tracing/latest/tracing/#emitting-log-records +//! [f]: https://docs.rs/tracing/latest/tracing/#emitting-log-records pub use tracing_core::{metadata::ParseLevelFilterError, LevelFilter}; /// The statically configured maximum trace level.