Skip to content

Commit

Permalink
tracing: Instrument std::future::Future (tokio-rs#808)
Browse files Browse the repository at this point in the history
Authored-by: David Barsky <dbarsky@amazon.com>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
davidbarsky and hawkw committed Sep 25, 2020
1 parent 216b98e commit a8cc977
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 49 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ For more details, see [the documentation on closing spans][closing].
This problem can be solved using the [`Future::instrument`] combinator:

```rust
use tracing_futures::Instrument;
use tracing::Instrument;

let my_future = async {
// ...
Expand All @@ -248,10 +248,10 @@ Under the hood, the `#[instrument]` macro performs same the explicit span
attachment that `Future::instrument` does.

[std-future]: https://doc.rust-lang.org/stable/std/future/trait.Future.html
[tracing-futures-docs]: https://docs.rs/tracing-futures
[closing]: https://docs.rs/tracing/latest/tracing/span/index.html#closing-spans
[`Future::instrument`]: https://docs.rs/tracing-futures/latest/tracing_futures/trait.Instrument.html#method.instrument
[instrument]: https://docs.rs/tracing/0.1.14/tracing/attr.instrument.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/latest/tracing/trait.Instrument.html#method.instrument
[`#[instrument]`]: https://docs.rs/tracing/0.1.11/tracing/attr.instrument.html

## Supported Rust Versions

Expand Down
6 changes: 3 additions & 3 deletions examples/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error + Send + Sync + 'static>> {
Expand Down Expand Up @@ -117,6 +116,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
info!(message = "echo'd data", %peer_addr, size = n);
}
})
.instrument(info_span!("echo", %peer_addr));
.instrument(info_span!("echo", %peer_addr))
.await?;
}
}
4 changes: 1 addition & 3 deletions examples/examples/futures-proxy-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error + Send + Sync + 'static>;

Expand Down
3 changes: 1 addition & 2 deletions examples/examples/hyper-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Body>) -> Result<Response<Body>, hyper::Error> {
let span = span!(
Expand Down
3 changes: 1 addition & 2 deletions examples/examples/tokio-spawny-thing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error + Send + Sync + 'static>;

Expand Down
3 changes: 1 addition & 2 deletions examples/examples/tower-load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
1 change: 0 additions & 1 deletion tracing-attributes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ use syn::{
/// }
/// ```
///
/// If `tracing_futures` is specified as a dependency in `Cargo.toml`,
/// `async fn`s may also be instrumented:
///
/// ```
Expand Down Expand Up @@ -445,7 +444,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) => {
Expand All @@ -458,7 +457,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
)
Expand Down
12 changes: 6 additions & 6 deletions tracing-futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,10 @@ pub(crate) mod stdlib;
#[cfg(feature = "std-future")]
use crate::stdlib::{pin::Pin, task::Context};

use tracing::dispatcher;
use tracing::{Dispatch, Span};
#[cfg(feature = "std")]
use tracing::{dispatcher, Dispatch};

use tracing::Span;

/// Implementations for `Instrument`ed future executors.
pub mod executor;
Expand Down Expand Up @@ -673,8 +675,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();
Expand All @@ -694,8 +695,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()
Expand Down
2 changes: 1 addition & 1 deletion tracing-futures/tests/std_future.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions tracing-tower/src/request_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S, R, G = fn(&R) -> tracing::Span>
Expand Down Expand Up @@ -236,7 +236,7 @@ where
{
type Response = S::Response;
type Error = S::Error;
type Future = tracing_futures::Instrumented<S::Future>;
type Future = tracing::instrument::Instrumented<S::Future>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
Expand Down
2 changes: 2 additions & 0 deletions tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ tracing-core = { path = "../tracing-core", version = "0.1.15", default-features
log = { version = "0.4", optional = true }
tracing-attributes = { path = "../tracing-attributes", version = "0.1.10", optional = true }
cfg-if = "0.1.10"
pin-project-lite = "0.1"

[dev-dependencies]
futures = "0.1"
criterion = { version = "0.3", default_features = false }
log = "0.4"
tokio = { version = "0.2.21", features = ["rt-core"] }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "^0.3"
Expand Down
4 changes: 2 additions & 2 deletions tracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,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 {
// ...
Expand Down

0 comments on commit a8cc977

Please sign in to comment.