From ee0fc2bc6b3881c2628a7ff8bbafcb287ac63b21 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Sat, 11 Jul 2020 20:55:54 -0400 Subject: [PATCH 01/15] Add Instrument and WithDispatch to `tracing` --- tracing/Cargo.toml | 1 + tracing/src/instrument.rs | 198 ++++++++++++++++++++++++++++++++++++++ tracing/src/lib.rs | 7 +- 3 files changed, 205 insertions(+), 1 deletion(-) create mode 100644 tracing/src/instrument.rs diff --git a/tracing/Cargo.toml b/tracing/Cargo.toml index 0f31addc3e..9ea865f6d1 100644 --- a/tracing/Cargo.toml +++ b/tracing/Cargo.toml @@ -31,6 +31,7 @@ tracing-core = { path = "../tracing-core", version = "0.1.11", default-features log = { version = "0.4", optional = true } tracing-attributes = { path = "../tracing-attributes", version = "0.1.9", optional = true } cfg-if = "0.1.10" +pin-project = "0.4" [dev-dependencies] futures = "0.1" diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs new file mode 100644 index 0000000000..6f18aba8d4 --- /dev/null +++ b/tracing/src/instrument.rs @@ -0,0 +1,198 @@ +use crate::stdlib::pin::Pin; +use crate::stdlib::task::{Context, Poll}; +use crate::stdlib::{future::Future, marker::Sized}; +use crate::{dispatcher, span::Span, Dispatch}; + +/// Attaches spans to a `std::future::Future`. +/// +/// Extension trait allowing futures to be +/// instrumented with a `tracing` [span]. +/// +/// [span]: https://docs.rs/tracing/latest/tracing/span/index.html +pub trait Instrument: Sized { + /// Instruments this type with the provided `Span`, returning an + /// `Instrumented` wrapper. + /// + /// If the instrumented type is a future, stream, or sink, the attached `Span` + /// will be [entered] every time it is polled. If the instrumented type + /// is a future executor, every future spawned on that executor will be + /// instrumented by the attached `Span`. + /// + /// # Examples + /// + /// Instrumenting a future: + /// + // TODO: ignored until async-await is stable... + /// ```rust,ignore + /// use tracing::Instrument; + /// + /// # async fn doc() { + /// let my_future = async { + /// // ... + /// }; + /// + /// my_future + /// .instrument(tracing::info_span!("my_future")) + /// .await + /// # } + /// ``` + /// + /// [entered]: https://docs.rs/tracing/latest/tracing/span/struct.Span.html#method.enter + fn instrument(self, span: Span) -> Instrumented { + Instrumented { inner: self, span } + } + + /// Instruments this type with the [current] `Span`, returning an + /// `Instrumented` wrapper. + /// + /// If the instrumented type is a future, stream, or sink, the attached `Span` + /// will be [entered] every time it is polled. If the instrumented type + /// is a future executor, every future spawned on that executor will be + /// instrumented by the attached `Span`. + /// + /// This can be used to propagate the current span when spawning a new future. + /// + /// # Examples + /// + // TODO: ignored until async-await is stable... + /// ```rust,ignore + /// use tracing::Instrument; + /// + /// # async fn doc() { + /// let span = tracing::info_span!("my_span"); + /// let _enter = span.enter(); + /// + /// // ... + /// + /// let future = async { + /// tracing::debug!("this event will occur inside `my_span`"); + /// // ... + /// }; + /// tokio::spawn(future.in_current_span()); + /// # } + /// ``` + /// + /// [current]: https://docs.rs/tracing/latest/tracing/span/struct.Span.html#method.current + /// [entered]: https://docs.rs/tracing/latest/tracing/span/struct.Span.html#method.enter + #[inline] + fn in_current_span(self) -> Instrumented { + self.instrument(Span::current()) + } +} + +/// Extension trait allowing futures to be instrumented with +/// a `tracing` [`Subscriber`]. +/// +/// [`Subscriber`]: https://docs.rs/tracing/latest/tracing/subscriber/trait.Subscriber.html +pub trait WithSubscriber: Sized { + /// Attaches the provided [`Subscriber`] to this type, returning a + /// `WithDispatch` wrapper. + /// + /// When the wrapped type is a future, stream, or sink, the attached + /// subscriber will be set as the [default] while it is being polled. + /// When the wrapped type is an executor, the subscriber will be set as the + /// default for any futures spawned on that executor. + /// + /// [`Subscriber`]: https://docs.rs/tracing/latest/tracing/subscriber/trait.Subscriber.html + /// [default]: https://docs.rs/tracing/latest/tracing/dispatcher/index.html#setting-the-default-subscriber + fn with_subscriber(self, subscriber: S) -> WithDispatch + where + S: Into, + { + WithDispatch { + inner: self, + dispatch: subscriber.into(), + } + } + + /// Attaches the current [default] [`Subscriber`] to this type, returning a + /// `WithDispatch` wrapper. + /// + /// When the wrapped type is a future, stream, or sink, the attached + /// subscriber will be set as the [default] while it is being polled. + /// When the wrapped type is an executor, the subscriber will be set as the + /// default for any futures spawned on that executor. + /// + /// This can be used to propagate the current dispatcher context when + /// spawning a new future. + /// + /// [`Subscriber`]: https://docs.rs/tracing/latest/tracing/subscriber/trait.Subscriber.html + /// [default]: https://docs.rs/tracing/latest/tracing/dispatcher/index.html#setting-the-default-subscriber + #[inline] + fn with_current_subscriber(self) -> WithDispatch { + WithDispatch { + inner: self, + dispatch: dispatcher::get_default(|default| default.clone()), + } + } +} + +/// A future, stream, sink, or executor that has been instrumented with a +/// `tracing` subscriber. +#[pin_project::pin_project] +#[derive(Clone, Debug)] +pub struct WithDispatch { + #[pin] + inner: T, + dispatch: Dispatch, +} + +/// A future that has been instrumented with a `tracing` span. +#[pin_project::pin_project] +#[derive(Debug, Clone)] +pub struct Instrumented { + #[pin] + inner: T, + span: Span, +} + +impl Future for Instrumented { + type Output = T::Output; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let this = self.project(); + let _enter = this.span.enter(); + this.inner.poll(cx) + } +} + +impl Instrument for T {} + +impl Instrumented { + /// Borrows the `Span` that this type is instrumented by. + pub fn span(&self) -> &Span { + &self.span + } + + /// Mutably borrows the `Span` that this type is instrumented by. + pub fn span_mut(&mut self) -> &mut Span { + &mut self.span + } + + /// Borrows the wrapped type. + pub fn inner(&self) -> &T { + &self.inner + } + + /// Mutably borrows the wrapped type. + pub fn inner_mut(&mut self) -> &mut T { + &mut self.inner + } + + /// Get a pinned reference to the wrapped type. + pub fn inner_pin_ref(self: Pin<&Self>) -> Pin<&T> { + self.project_ref().inner + } + + /// Get a pinned mutable reference to the wrapped type. + pub fn inner_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> { + self.project().inner + } + + /// Consumes the `Instrumented`, returning the wrapped type. + /// + /// Note that this drops the span. + pub fn into_inner(self) -> T { + self.inner + } +} diff --git a/tracing/src/lib.rs b/tracing/src/lib.rs index 7b31d6880e..7b2b554300 100644 --- a/tracing/src/lib.rs +++ b/tracing/src/lib.rs @@ -834,7 +834,10 @@ pub use log; #[doc(hidden)] use tracing_core::*; -pub use self::{dispatcher::Dispatch, event::Event, field::Value, subscriber::Subscriber}; +pub use self::{ + dispatcher::Dispatch, event::Event, field::Value, instrument::Instrument, + subscriber::Subscriber, +}; #[doc(hidden)] pub use self::span::Id; @@ -858,6 +861,8 @@ mod macros; pub mod dispatcher; pub mod field; +/// Attach a span to a `std::future::Future`. +pub mod instrument; pub mod level_filters; pub mod span; pub(crate) mod stdlib; From 5e3905ab1281f39f227821964061a0fd8176b0dc Mon Sep 17 00:00:00 2001 From: David Barsky Date: Sat, 11 Jul 2020 20:57:05 -0400 Subject: [PATCH 02/15] Update tracing-tower and tracing-attributes to depend on tracing's Instrument --- tracing-attributes/Cargo.toml | 1 - tracing-attributes/src/lib.rs | 4 ++-- tracing-tower/src/request_span.rs | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/tracing-attributes/Cargo.toml b/tracing-attributes/Cargo.toml index a7774c4615..61c29dc7e8 100644 --- a/tracing-attributes/Cargo.toml +++ b/tracing-attributes/Cargo.toml @@ -45,7 +45,6 @@ quote = "1" [dev-dependencies] tracing = { path = "../tracing", version = "0.1" } -tracing-futures = { path = "../tracing-futures", version = "0.2" } tokio-test = { version = "0.2.0" } tracing-core = { path = "../tracing-core", version = "0.1"} async-trait = "0.1" diff --git a/tracing-attributes/src/lib.rs b/tracing-attributes/src/lib.rs index 6845344a44..eaf4ec869d 100644 --- a/tracing-attributes/src/lib.rs +++ b/tracing-attributes/src/lib.rs @@ -386,7 +386,7 @@ fn gen_body( if err { quote_spanned! {block.span()=> let __tracing_attr_span = #span; - tracing_futures::Instrument::instrument(async move { + tracing::Instrument::instrument(async move { match async move { #block }.await { Ok(x) => Ok(x), Err(e) => { @@ -399,7 +399,7 @@ fn gen_body( } else { quote_spanned!(block.span()=> let __tracing_attr_span = #span; - tracing_futures::Instrument::instrument( + tracing::Instrument::instrument( async move { #block }, __tracing_attr_span ) diff --git a/tracing-tower/src/request_span.rs b/tracing-tower/src/request_span.rs index 9a04537c60..3c9ed61215 100644 --- a/tracing-tower/src/request_span.rs +++ b/tracing-tower/src/request_span.rs @@ -4,7 +4,7 @@ use futures::future::Future; use std::marker::PhantomData; use std::pin::Pin; use std::task::{Context, Poll}; -use tracing_futures::Instrument; +use tracing::Instrument; #[derive(Debug)] pub struct Service tracing::Span> @@ -236,7 +236,7 @@ where { type Response = S::Response; type Error = S::Error; - type Future = tracing_futures::Instrumented; + type Future = tracing::instrument::Instrumented; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { self.inner.poll_ready(cx) From 6de0d8a3e83a4c5f23edb5c5ca57458cc25ec764 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Sat, 11 Jul 2020 20:57:18 -0400 Subject: [PATCH 03/15] Update examples --- examples/examples/echo.rs | 3 +-- examples/examples/futures-proxy-server.rs | 4 +--- examples/examples/hyper-echo.rs | 3 +-- examples/examples/tokio-spawny-thing.rs | 3 +-- examples/examples/tower-load.rs | 3 +-- 5 files changed, 5 insertions(+), 11 deletions(-) diff --git a/examples/examples/echo.rs b/examples/examples/echo.rs index 380a224329..e26ebc0ae6 100644 --- a/examples/examples/echo.rs +++ b/examples/examples/echo.rs @@ -31,8 +31,7 @@ use std::env; use std::error::Error; use std::net::SocketAddr; -use tracing::{debug, info, info_span, trace_span, warn}; -use tracing_futures::Instrument; +use tracing::{debug, info, info_span, trace_span, warn, Instrument as _}; #[tokio::main] async fn main() -> Result<(), Box> { diff --git a/examples/examples/futures-proxy-server.rs b/examples/examples/futures-proxy-server.rs index db289f3baf..1010a87441 100644 --- a/examples/examples/futures-proxy-server.rs +++ b/examples/examples/futures-proxy-server.rs @@ -32,9 +32,7 @@ use tokio::{ self, io, net::{TcpListener, TcpStream}, }; -use tracing::{debug, debug_span, info, warn}; -use tracing_attributes::instrument; -use tracing_futures::Instrument; +use tracing::{debug, debug_span, info, instrument, warn, Instrument as _}; type Error = Box; diff --git a/examples/examples/hyper-echo.rs b/examples/examples/hyper-echo.rs index dc318eabdc..3404a8d5e9 100644 --- a/examples/examples/hyper-echo.rs +++ b/examples/examples/hyper-echo.rs @@ -6,8 +6,7 @@ use hyper::{ Body, Server, }; use std::str; -use tracing::{debug, info, span, Level}; -use tracing_futures::Instrument; +use tracing::{debug, info, span, Instrument as _, Level}; async fn echo(req: Request) -> Result, hyper::Error> { let span = span!( diff --git a/examples/examples/tokio-spawny-thing.rs b/examples/examples/tokio-spawny-thing.rs index 845b82d3b1..b97318a79a 100644 --- a/examples/examples/tokio-spawny-thing.rs +++ b/examples/examples/tokio-spawny-thing.rs @@ -8,8 +8,7 @@ /// cargo run --example tokio-spawny-thing /// ``` use futures::future::try_join_all; -use tracing::{debug, info, instrument, span, Level}; -use tracing_futures::Instrument; +use tracing::{debug, info, instrument, span, Instrument as _, Level}; type Error = Box; diff --git a/examples/examples/tower-load.rs b/examples/examples/tower-load.rs index 71417de9bd..443f74ddf2 100644 --- a/examples/examples/tower-load.rs +++ b/examples/examples/tower-load.rs @@ -42,8 +42,7 @@ use std::{ }; use tokio::{time, try_join}; use tower::{Service, ServiceBuilder, ServiceExt}; -use tracing::{self, debug, error, info, span, trace, warn, Level, Span}; -use tracing_futures::Instrument; +use tracing::{self, debug, error, info, span, trace, warn, Instrument as _, Level, Span}; use tracing_subscriber::{filter::EnvFilter, reload::Handle}; use tracing_tower::{request_span, request_span::make}; From 6841eeac3a6e86feaba27bd839c65013db3440bf Mon Sep 17 00:00:00 2001 From: David Barsky Date: Sat, 11 Jul 2020 20:58:15 -0400 Subject: [PATCH 04/15] BROKEN: update tracing-futures; not sure how to resolve conflict in blankets. --- tracing-futures/src/lib.rs | 15 +++------------ tracing-futures/tests/std_future.rs | 2 +- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/tracing-futures/src/lib.rs b/tracing-futures/src/lib.rs index 5f3325c3a8..6dfe364594 100644 --- a/tracing-futures/src/lib.rs +++ b/tracing-futures/src/lib.rs @@ -87,6 +87,9 @@ pub(crate) mod stdlib; #[cfg(feature = "std-future")] use crate::stdlib::{pin::Pin, task::Context}; +#[cfg(feature = "std-future")] +pub use tracing::Instrument as _; + use tracing::dispatcher; use tracing::{Dispatch, Span}; @@ -247,18 +250,6 @@ pub struct WithDispatch { impl Instrument for T {} -#[cfg(feature = "std-future")] -#[cfg_attr(docsrs, doc(cfg(feature = "std-future")))] -impl crate::stdlib::future::Future for Instrumented { - type Output = T::Output; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> crate::stdlib::task::Poll { - let this = self.project(); - let _enter = this.span.enter(); - this.inner.poll(cx) - } -} - #[cfg(feature = "futures-01")] #[cfg_attr(docsrs, doc(cfg(feature = "futures-01")))] impl futures_01::Future for Instrumented { diff --git a/tracing-futures/tests/std_future.rs b/tracing-futures/tests/std_future.rs index 5bc402cccb..a5431d37da 100644 --- a/tracing-futures/tests/std_future.rs +++ b/tracing-futures/tests/std_future.rs @@ -1,8 +1,8 @@ mod support; use support::*; +use tracing::Instrument; use tracing::{subscriber::with_default, Level}; -use tracing_futures::Instrument; #[test] fn enter_exit_is_reasonable() { From 83e15dda21d852cb27a01457e3202bdcd95a664a Mon Sep 17 00:00:00 2001 From: David Barsky Date: Sat, 11 Jul 2020 21:03:02 -0400 Subject: [PATCH 05/15] It "works" --- tracing/src/instrument.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index 6f18aba8d4..b9e135e242 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -156,7 +156,7 @@ impl Future for Instrumented { } } -impl Instrument for T {} +impl Instrument for T {} impl Instrumented { /// Borrows the `Span` that this type is instrumented by. From 58481f8fff4ef1103c697924d20ac8b933d1d999 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Sun, 12 Jul 2020 18:03:33 -0400 Subject: [PATCH 06/15] switch to pin-project-lite --- tracing/Cargo.toml | 2 +- tracing/src/instrument.rs | 33 ++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/tracing/Cargo.toml b/tracing/Cargo.toml index 9ea865f6d1..160eba05cc 100644 --- a/tracing/Cargo.toml +++ b/tracing/Cargo.toml @@ -31,7 +31,7 @@ tracing-core = { path = "../tracing-core", version = "0.1.11", default-features log = { version = "0.4", optional = true } tracing-attributes = { path = "../tracing-attributes", version = "0.1.9", optional = true } cfg-if = "0.1.10" -pin-project = "0.4" +pin-project-lite = "0.1" [dev-dependencies] futures = "0.1" diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index b9e135e242..1348d5459d 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -2,6 +2,7 @@ use crate::stdlib::pin::Pin; use crate::stdlib::task::{Context, Poll}; use crate::stdlib::{future::Future, marker::Sized}; use crate::{dispatcher, span::Span, Dispatch}; +use pin_project_lite::pin_project; /// Attaches spans to a `std::future::Future`. /// @@ -127,23 +128,25 @@ pub trait WithSubscriber: Sized { } } -/// A future, stream, sink, or executor that has been instrumented with a -/// `tracing` subscriber. -#[pin_project::pin_project] -#[derive(Clone, Debug)] -pub struct WithDispatch { - #[pin] - inner: T, - dispatch: Dispatch, +pin_project! { + /// A future, stream, sink, or executor that has been instrumented with a + /// `tracing` subscriber. + #[derive(Clone, Debug)] + pub struct WithDispatch { + #[pin] + inner: T, + dispatch: Dispatch, + } } -/// A future that has been instrumented with a `tracing` span. -#[pin_project::pin_project] -#[derive(Debug, Clone)] -pub struct Instrumented { - #[pin] - inner: T, - span: Span, +pin_project! { + /// A future that has been instrumented with a `tracing` span. + #[derive(Debug, Clone)] + pub struct Instrumented { + #[pin] + inner: T, + span: Span, + } } impl Future for Instrumented { From 65818d96713b2f0b96e2fa38295f72485cafba11 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Mon, 20 Jul 2020 16:57:55 -0400 Subject: [PATCH 07/15] Fix build issues --- tracing-futures/src/lib.rs | 25 +++++++++++++++++-------- tracing/src/instrument.rs | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/tracing-futures/src/lib.rs b/tracing-futures/src/lib.rs index 6dfe364594..06621f33c0 100644 --- a/tracing-futures/src/lib.rs +++ b/tracing-futures/src/lib.rs @@ -87,11 +87,10 @@ pub(crate) mod stdlib; #[cfg(feature = "std-future")] use crate::stdlib::{pin::Pin, task::Context}; -#[cfg(feature = "std-future")] -pub use tracing::Instrument as _; +#[cfg(feature = "std")] +use tracing::{dispatcher, Dispatch}; -use tracing::dispatcher; -use tracing::{Dispatch, Span}; +use tracing::Span; /// Implementations for `Instrument`ed future executors. pub mod executor; @@ -250,6 +249,18 @@ pub struct WithDispatch { impl Instrument for T {} +#[cfg(feature = "std-future")] +#[cfg_attr(docsrs, doc(cfg(feature = "std-future")))] +impl crate::stdlib::future::Future for Instrumented { + type Output = T::Output; + + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> crate::stdlib::task::Poll { + let this = self.project(); + let _enter = this.span.enter(); + this.inner.poll(cx) + } +} + #[cfg(feature = "futures-01")] #[cfg_attr(docsrs, doc(cfg(feature = "futures-01")))] impl futures_01::Future for Instrumented { @@ -641,8 +652,7 @@ mod tests { .drop_span(span::mock().named("foo")) .run_with_handle(); with_default(subscriber, || { - stream::iter(&[1, 2, 3]) - .instrument(tracing::trace_span!("foo")) + Instrument::instrument(stream::iter(&[1, 2, 3]), tracing::trace_span!("foo")) .for_each(|_| future::ready(())) .now_or_never() .unwrap(); @@ -662,8 +672,7 @@ mod tests { .drop_span(span::mock().named("foo")) .run_with_handle(); with_default(subscriber, || { - sink::drain() - .instrument(tracing::trace_span!("foo")) + Instrument::instrument(sink::drain(), tracing::trace_span!("foo")) .send(1u8) .now_or_never() .unwrap() diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index 1348d5459d..aa88c8e056 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -159,7 +159,7 @@ impl Future for Instrumented { } } -impl Instrument for T {} +impl Instrument for T {} impl Instrumented { /// Borrows the `Span` that this type is instrumented by. From 8e7b0a0a8b8106c49908fdab53c6f1cf17b61b45 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Mon, 20 Jul 2020 17:48:23 -0400 Subject: [PATCH 08/15] Update tracing/src/instrument.rs Co-authored-by: Eliza Weisman --- tracing/src/instrument.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index aa88c8e056..e6e9c78f5f 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -129,8 +129,7 @@ pub trait WithSubscriber: Sized { } pin_project! { - /// A future, stream, sink, or executor that has been instrumented with a - /// `tracing` subscriber. + /// A future that has been instrumented with a `tracing` subscriber. #[derive(Clone, Debug)] pub struct WithDispatch { #[pin] From 388a3d90e870b655ee14c7822b46e320fbad453a Mon Sep 17 00:00:00 2001 From: David Barsky Date: Mon, 20 Jul 2020 17:48:42 -0400 Subject: [PATCH 09/15] Update tracing/src/instrument.rs Co-authored-by: Eliza Weisman --- tracing/src/instrument.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index e6e9c78f5f..99c2c62031 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -14,10 +14,7 @@ pub trait Instrument: Sized { /// Instruments this type with the provided `Span`, returning an /// `Instrumented` wrapper. /// - /// If the instrumented type is a future, stream, or sink, the attached `Span` - /// will be [entered] every time it is polled. If the instrumented type - /// is a future executor, every future spawned on that executor will be - /// instrumented by the attached `Span`. + /// The attached `Span` will be [entered] every time the instrumented `Future` is polled. /// /// # Examples /// From a261cdb4418a983c4d6ff0358532b610a7e7f930 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Mon, 20 Jul 2020 17:48:51 -0400 Subject: [PATCH 10/15] Update tracing/src/instrument.rs Co-authored-by: Eliza Weisman --- tracing/src/instrument.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index 99c2c62031..9cb1a9b671 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -20,8 +20,7 @@ pub trait Instrument: Sized { /// /// Instrumenting a future: /// - // TODO: ignored until async-await is stable... - /// ```rust,ignore + /// ```rust /// use tracing::Instrument; /// /// # async fn doc() { From 7d549480f011ca4199533c0dd8c9569fe29e9124 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Mon, 20 Jul 2020 17:48:59 -0400 Subject: [PATCH 11/15] Update tracing/src/instrument.rs Co-authored-by: Eliza Weisman --- tracing/src/instrument.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index 9cb1a9b671..825808df94 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -51,8 +51,7 @@ pub trait Instrument: Sized { /// /// # Examples /// - // TODO: ignored until async-await is stable... - /// ```rust,ignore + /// ```rust /// use tracing::Instrument; /// /// # async fn doc() { From ed115671a8f106c5c95e7a14eccf70deb3f7b940 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Mon, 20 Jul 2020 20:29:46 -0400 Subject: [PATCH 12/15] Run with `tokio::spawn` in doc test --- tracing/Cargo.toml | 1 + tracing/src/lib.rs | 9 --------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/tracing/Cargo.toml b/tracing/Cargo.toml index 160eba05cc..d03b0e17fc 100644 --- a/tracing/Cargo.toml +++ b/tracing/Cargo.toml @@ -37,6 +37,7 @@ pin-project-lite = "0.1" futures = "0.1" criterion = { version = "0.3", default_features = false } log = "0.4" +tokio = { version = "0.2.21", features = ["rt-core"] } [features] default = ["std", "attributes"] diff --git a/tracing/src/lib.rs b/tracing/src/lib.rs index 7b2b554300..611f1ec362 100644 --- a/tracing/src/lib.rs +++ b/tracing/src/lib.rs @@ -170,15 +170,6 @@ //! # fn main() {} //! ``` //! -//!
-//!
Note
-//!
-//!
-//!
-//!     Note: Using #[instrument] on
-//!     async fns requires the 
-//!     tracing-futures crate as a dependency as well.
-//! 
//! //! You can find more examples showing how to use this crate [here][examples]. //! From 28ddc27734b508cafd5d2fde55c4485de3e3a172 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Tue, 21 Jul 2020 12:51:02 -0400 Subject: [PATCH 13/15] update doc links --- README.md | 4 ++-- tracing-attributes/src/lib.rs | 1 - tracing/README.md | 4 ++-- tracing/src/instrument.rs | 14 +++++++------- tracing/src/lib.rs | 7 +++---- tracing/src/span.rs | 14 ++++++-------- 6 files changed, 20 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5f36d12d5f..29a3d893e2 100644 --- a/README.md +++ b/README.md @@ -217,7 +217,7 @@ There are two ways to instrument asynchronous code. The first is through the [`Future::instrument`] combinator: ```rust -use tracing_futures::Instrument; +use tracing::Instrument; let my_future = async { // ... @@ -252,7 +252,7 @@ attachment that `Future::instrument` does. [std-future]: https://doc.rust-lang.org/stable/std/future/trait.Future.html [`tracing-futures`]: https://docs.rs/tracing-futures [closing]: https://docs.rs/tracing/latest/span/index.html#closing-spans -[`Future::instrument`]: https://docs.rs/tracing-futures/latest/tracing_futures/trait.Instrument.html#method.instrument +[`Future::instrument`]: https://docs.rs/tracing/latest/tracing/trait.Instrument.html#method.instrument [`#[instrument]`]: https://docs.rs/tracing/0.1.11/tracing/attr.instrument.html diff --git a/tracing-attributes/src/lib.rs b/tracing-attributes/src/lib.rs index eaf4ec869d..be7ae286ae 100644 --- a/tracing-attributes/src/lib.rs +++ b/tracing-attributes/src/lib.rs @@ -159,7 +159,6 @@ use syn::{ /// } /// ``` /// -/// If `tracing_futures` is specified as a dependency in `Cargo.toml`, /// `async fn`s may also be instrumented: /// /// ``` diff --git a/tracing/README.md b/tracing/README.md index 3ff080a291..e196d4a6c2 100644 --- a/tracing/README.md +++ b/tracing/README.md @@ -223,10 +223,10 @@ it is polled, leading to very confusing and incorrect output. For more details, see [the documentation on closing spans](https://tracing.rs/tracing/span/index.html#closing-spans). There are two ways to instrument asynchronous code. The first is through the -[`Future::instrument`](https://docs.rs/tracing-futures/0.2.0/tracing_futures/trait.Instrument.html#method.instrument) combinator: +[`Future::instrument`](https://docs.rs/tracing/latest/tracing/trait.Instrument.html#method.instrument) combinator: ```rust -use tracing_futures::Instrument; +use tracing::Instrument; let my_future = async { // ... diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index 825808df94..0091d7b2a5 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -9,7 +9,7 @@ use pin_project_lite::pin_project; /// Extension trait allowing futures to be /// instrumented with a `tracing` [span]. /// -/// [span]: https://docs.rs/tracing/latest/tracing/span/index.html +/// [span]: ../struct.Span.html pub trait Instrument: Sized { /// Instruments this type with the provided `Span`, returning an /// `Instrumented` wrapper. @@ -34,7 +34,7 @@ pub trait Instrument: Sized { /// # } /// ``` /// - /// [entered]: https://docs.rs/tracing/latest/tracing/span/struct.Span.html#method.enter + /// [entered]: ../struct.Span.html#method.enter fn instrument(self, span: Span) -> Instrumented { Instrumented { inner: self, span } } @@ -68,8 +68,8 @@ pub trait Instrument: Sized { /// # } /// ``` /// - /// [current]: https://docs.rs/tracing/latest/tracing/span/struct.Span.html#method.current - /// [entered]: https://docs.rs/tracing/latest/tracing/span/struct.Span.html#method.enter + /// [current]: ../struct.Span.html#method.current + /// [entered]: ../struct.Span.html#method.enter #[inline] fn in_current_span(self) -> Instrumented { self.instrument(Span::current()) @@ -79,7 +79,7 @@ pub trait Instrument: Sized { /// Extension trait allowing futures to be instrumented with /// a `tracing` [`Subscriber`]. /// -/// [`Subscriber`]: https://docs.rs/tracing/latest/tracing/subscriber/trait.Subscriber.html +/// [`Subscriber`]: ../trait.Subscriber.html pub trait WithSubscriber: Sized { /// Attaches the provided [`Subscriber`] to this type, returning a /// `WithDispatch` wrapper. @@ -89,7 +89,7 @@ pub trait WithSubscriber: Sized { /// When the wrapped type is an executor, the subscriber will be set as the /// default for any futures spawned on that executor. /// - /// [`Subscriber`]: https://docs.rs/tracing/latest/tracing/subscriber/trait.Subscriber.html + /// [`Subscriber`]: ../trait.Subscriber.html /// [default]: https://docs.rs/tracing/latest/tracing/dispatcher/index.html#setting-the-default-subscriber fn with_subscriber(self, subscriber: S) -> WithDispatch where @@ -112,7 +112,7 @@ pub trait WithSubscriber: Sized { /// This can be used to propagate the current dispatcher context when /// spawning a new future. /// - /// [`Subscriber`]: https://docs.rs/tracing/latest/tracing/subscriber/trait.Subscriber.html + /// [`Subscriber`]: ../trait.Subscriber.html /// [default]: https://docs.rs/tracing/latest/tracing/dispatcher/index.html#setting-the-default-subscriber #[inline] fn with_current_subscriber(self) -> WithDispatch { diff --git a/tracing/src/lib.rs b/tracing/src/lib.rs index 611f1ec362..c346cedebb 100644 --- a/tracing/src/lib.rs +++ b/tracing/src/lib.rs @@ -825,10 +825,9 @@ pub use log; #[doc(hidden)] use tracing_core::*; -pub use self::{ - dispatcher::Dispatch, event::Event, field::Value, instrument::Instrument, - subscriber::Subscriber, -}; +#[doc(inline)] +pub use self::instrument::Instrument; +pub use self::{dispatcher::Dispatch, event::Event, field::Value, subscriber::Subscriber}; #[doc(hidden)] pub use self::span::Id; diff --git a/tracing/src/span.rs b/tracing/src/span.rs index c3b022fc1e..944f9be752 100644 --- a/tracing/src/span.rs +++ b/tracing/src/span.rs @@ -580,8 +580,8 @@ impl Span { /// // ... /// } /// ``` - /// * For instrumenting asynchronous code, the [`tracing-futures` crate] - /// provides the [`Future::instrument` combinator][instrument] for + /// * For instrumenting asynchronous code, `tracing` provides the + /// [`Future::instrument` combinator][instrument] for /// attaching a span to a future (async function or block). This will /// enter the span _every_ time the future is polled, and exit it whenever /// the future yields. @@ -589,7 +589,7 @@ impl Span { /// `Instrument` can be used with an async block inside an async function: /// ```ignore /// # use tracing::info_span; - /// use tracing_futures::Instrument; + /// use tracing::Instrument; /// /// # async fn some_other_async_function() {} /// async fn my_async_function() { @@ -613,7 +613,7 @@ impl Span { /// callsite: /// ```ignore /// # use tracing::debug_span; - /// use tracing_futures::Instrument; + /// use tracing::Instrument; /// /// # async fn some_other_async_function() {} /// async fn my_async_function() { @@ -625,8 +625,7 @@ impl Span { /// } /// ``` /// - /// * Finally, if your crate depends on the `tracing-futures` crate, the - /// [`#[instrument]` attribute macro][attr] will automatically generate + /// * The [`#[instrument]` attribute macro][attr] can automatically generate /// correct code when used on an async function: /// /// ```ignore @@ -645,8 +644,7 @@ impl Span { /// /// [syntax]: https://rust-lang.github.io/async-book/01_getting_started/04_async_await_primer.html /// [`Span::in_scope`]: #method.in_scope - /// [`tracing-futures` crate]: https://docs.rs/tracing-futures/ - /// [instrument]: https://docs.rs/tracing-futures/latest/tracing_futures/trait.Instrument.html + /// [instrument]: https://docs.rs/tracing/latest/tracing/trait.Instrument.html /// [attr]: ../../attr.instrument.html /// /// # Examples From ea5c7b56bb767bd5c9739f38bd7ec651cabef3cb Mon Sep 17 00:00:00 2001 From: David Barsky Date: Tue, 21 Jul 2020 12:55:29 -0400 Subject: [PATCH 14/15] Update tracing/src/instrument.rs Co-authored-by: Eliza Weisman --- tracing/src/instrument.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index 0091d7b2a5..d90fa936a7 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -84,10 +84,7 @@ pub trait WithSubscriber: Sized { /// Attaches the provided [`Subscriber`] to this type, returning a /// `WithDispatch` wrapper. /// - /// When the wrapped type is a future, stream, or sink, the attached - /// subscriber will be set as the [default] while it is being polled. - /// When the wrapped type is an executor, the subscriber will be set as the - /// default for any futures spawned on that executor. + /// The attached subscriber will be set as the [default] when the returned `Future` is polled. /// /// [`Subscriber`]: ../trait.Subscriber.html /// [default]: https://docs.rs/tracing/latest/tracing/dispatcher/index.html#setting-the-default-subscriber From 2c4f3d1f8554affea348f1ac634ac14055341483 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Wed, 22 Jul 2020 11:36:46 -0400 Subject: [PATCH 15/15] uh oh --- examples/examples/echo.rs | 3 ++- tracing/src/instrument.rs | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/examples/echo.rs b/examples/examples/echo.rs index e26ebc0ae6..3edd63cef4 100644 --- a/examples/examples/echo.rs +++ b/examples/examples/echo.rs @@ -116,6 +116,7 @@ async fn main() -> Result<(), Box> { info!(message = "echo'd data", %peer_addr, size = n); } }) - .instrument(info_span!("echo", %peer_addr)); + .instrument(info_span!("echo", %peer_addr)) + .await?; } } diff --git a/tracing/src/instrument.rs b/tracing/src/instrument.rs index d90fa936a7..d1070ba8e2 100644 --- a/tracing/src/instrument.rs +++ b/tracing/src/instrument.rs @@ -123,6 +123,7 @@ pub trait WithSubscriber: Sized { pin_project! { /// A future that has been instrumented with a `tracing` subscriber. #[derive(Clone, Debug)] + #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct WithDispatch { #[pin] inner: T, @@ -133,6 +134,7 @@ pin_project! { pin_project! { /// A future that has been instrumented with a `tracing` span. #[derive(Debug, Clone)] + #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct Instrumented { #[pin] inner: T,