Skip to content

Commit a8cc977

Browse files
davidbarskyhawkw
andauthored
tracing: Instrument std::future::Future (#808)
Authored-by: David Barsky <dbarsky@amazon.com> Co-authored-by: Eliza Weisman <eliza@buoyant.io>
1 parent 216b98e commit a8cc977

File tree

16 files changed

+231
-49
lines changed

16 files changed

+231
-49
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ For more details, see [the documentation on closing spans][closing].
230230
This problem can be solved using the [`Future::instrument`] combinator:
231231

232232
```rust
233-
use tracing_futures::Instrument;
233+
use tracing::Instrument;
234234

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

250250
[std-future]: https://doc.rust-lang.org/stable/std/future/trait.Future.html
251-
[tracing-futures-docs]: https://docs.rs/tracing-futures
252-
[closing]: https://docs.rs/tracing/latest/tracing/span/index.html#closing-spans
253-
[`Future::instrument`]: https://docs.rs/tracing-futures/latest/tracing_futures/trait.Instrument.html#method.instrument
254-
[instrument]: https://docs.rs/tracing/0.1.14/tracing/attr.instrument.html
251+
[`tracing-futures`]: https://docs.rs/tracing-futures
252+
[closing]: https://docs.rs/tracing/latest/span/index.html#closing-spans
253+
[`Future::instrument`]: https://docs.rs/tracing/latest/tracing/trait.Instrument.html#method.instrument
254+
[`#[instrument]`]: https://docs.rs/tracing/0.1.11/tracing/attr.instrument.html
255255

256256
## Supported Rust Versions
257257

examples/examples/echo.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ use std::env;
3131
use std::error::Error;
3232
use std::net::SocketAddr;
3333

34-
use tracing::{debug, info, info_span, trace_span, warn};
35-
use tracing_futures::Instrument;
34+
use tracing::{debug, info, info_span, trace_span, warn, Instrument as _};
3635

3736
#[tokio::main]
3837
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
@@ -117,6 +116,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
117116
info!(message = "echo'd data", %peer_addr, size = n);
118117
}
119118
})
120-
.instrument(info_span!("echo", %peer_addr));
119+
.instrument(info_span!("echo", %peer_addr))
120+
.await?;
121121
}
122122
}

examples/examples/futures-proxy-server.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ use tokio::{
3232
self, io,
3333
net::{TcpListener, TcpStream},
3434
};
35-
use tracing::{debug, debug_span, info, warn};
36-
use tracing_attributes::instrument;
37-
use tracing_futures::Instrument;
35+
use tracing::{debug, debug_span, info, instrument, warn, Instrument as _};
3836

3937
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
4038

examples/examples/hyper-echo.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ use hyper::{
66
Body, Server,
77
};
88
use std::str;
9-
use tracing::{debug, info, span, Level};
10-
use tracing_futures::Instrument;
9+
use tracing::{debug, info, span, Instrument as _, Level};
1110

1211
async fn echo(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
1312
let span = span!(

examples/examples/tokio-spawny-thing.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
/// cargo run --example tokio-spawny-thing
99
/// ```
1010
use futures::future::try_join_all;
11-
use tracing::{debug, info, instrument, span, Level};
12-
use tracing_futures::Instrument;
11+
use tracing::{debug, info, instrument, span, Instrument as _, Level};
1312

1413
type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
1514

examples/examples/tower-load.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ use std::{
4242
};
4343
use tokio::{time, try_join};
4444
use tower::{Service, ServiceBuilder, ServiceExt};
45-
use tracing::{self, debug, error, info, span, trace, warn, Level, Span};
46-
use tracing_futures::Instrument;
45+
use tracing::{self, debug, error, info, span, trace, warn, Instrument as _, Level, Span};
4746
use tracing_subscriber::{filter::EnvFilter, reload::Handle};
4847
use tracing_tower::{request_span, request_span::make};
4948

tracing-attributes/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ quote = "1"
4545

4646
[dev-dependencies]
4747
tracing = { path = "../tracing", version = "0.1" }
48-
tracing-futures = { path = "../tracing-futures", version = "0.2" }
4948
tokio-test = { version = "0.2.0" }
5049
tracing-core = { path = "../tracing-core", version = "0.1"}
5150
async-trait = "0.1"

tracing-attributes/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ use syn::{
190190
/// }
191191
/// ```
192192
///
193-
/// If `tracing_futures` is specified as a dependency in `Cargo.toml`,
194193
/// `async fn`s may also be instrumented:
195194
///
196195
/// ```
@@ -445,7 +444,7 @@ fn gen_body(
445444
if err {
446445
quote_spanned! {block.span()=>
447446
let __tracing_attr_span = #span;
448-
tracing_futures::Instrument::instrument(async move {
447+
tracing::Instrument::instrument(async move {
449448
match async move { #block }.await {
450449
Ok(x) => Ok(x),
451450
Err(e) => {
@@ -458,7 +457,7 @@ fn gen_body(
458457
} else {
459458
quote_spanned!(block.span()=>
460459
let __tracing_attr_span = #span;
461-
tracing_futures::Instrument::instrument(
460+
tracing::Instrument::instrument(
462461
async move { #block },
463462
__tracing_attr_span
464463
)

tracing-futures/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ pub(crate) mod stdlib;
110110
#[cfg(feature = "std-future")]
111111
use crate::stdlib::{pin::Pin, task::Context};
112112

113-
use tracing::dispatcher;
114-
use tracing::{Dispatch, Span};
113+
#[cfg(feature = "std")]
114+
use tracing::{dispatcher, Dispatch};
115+
116+
use tracing::Span;
115117

116118
/// Implementations for `Instrument`ed future executors.
117119
pub mod executor;
@@ -673,8 +675,7 @@ mod tests {
673675
.drop_span(span::mock().named("foo"))
674676
.run_with_handle();
675677
with_default(subscriber, || {
676-
stream::iter(&[1, 2, 3])
677-
.instrument(tracing::trace_span!("foo"))
678+
Instrument::instrument(stream::iter(&[1, 2, 3]), tracing::trace_span!("foo"))
678679
.for_each(|_| future::ready(()))
679680
.now_or_never()
680681
.unwrap();
@@ -694,8 +695,7 @@ mod tests {
694695
.drop_span(span::mock().named("foo"))
695696
.run_with_handle();
696697
with_default(subscriber, || {
697-
sink::drain()
698-
.instrument(tracing::trace_span!("foo"))
698+
Instrument::instrument(sink::drain(), tracing::trace_span!("foo"))
699699
.send(1u8)
700700
.now_or_never()
701701
.unwrap()

tracing-futures/tests/std_future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mod support;
22
use support::*;
33

4+
use tracing::Instrument;
45
use tracing::{subscriber::with_default, Level};
5-
use tracing_futures::Instrument;
66

77
#[test]
88
fn enter_exit_is_reasonable() {

tracing-tower/src/request_span.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use futures::future::Future;
44
use std::marker::PhantomData;
55
use std::pin::Pin;
66
use std::task::{Context, Poll};
7-
use tracing_futures::Instrument;
7+
use tracing::Instrument;
88

99
#[derive(Debug)]
1010
pub struct Service<S, R, G = fn(&R) -> tracing::Span>
@@ -236,7 +236,7 @@ where
236236
{
237237
type Response = S::Response;
238238
type Error = S::Error;
239-
type Future = tracing_futures::Instrumented<S::Future>;
239+
type Future = tracing::instrument::Instrumented<S::Future>;
240240

241241
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
242242
self.inner.poll_ready(cx)

tracing/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ tracing-core = { path = "../tracing-core", version = "0.1.15", default-features
3131
log = { version = "0.4", optional = true }
3232
tracing-attributes = { path = "../tracing-attributes", version = "0.1.10", optional = true }
3333
cfg-if = "0.1.10"
34+
pin-project-lite = "0.1"
3435

3536
[dev-dependencies]
3637
futures = "0.1"
3738
criterion = { version = "0.3", default_features = false }
3839
log = "0.4"
40+
tokio = { version = "0.2.21", features = ["rt-core"] }
3941

4042
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
4143
wasm-bindgen-test = "^0.3"

tracing/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ it is polled, leading to very confusing and incorrect output.
228228
For more details, see [the documentation on closing spans](https://tracing.rs/tracing/span/index.html#closing-spans).
229229

230230
There are two ways to instrument asynchronous code. The first is through the
231-
[`Future::instrument`](https://docs.rs/tracing-futures/0.2.0/tracing_futures/trait.Instrument.html#method.instrument) combinator:
231+
[`Future::instrument`](https://docs.rs/tracing/latest/tracing/trait.Instrument.html#method.instrument) combinator:
232232

233233
```rust
234-
use tracing_futures::Instrument;
234+
use tracing::Instrument;
235235

236236
let my_future = async {
237237
// ...

0 commit comments

Comments
 (0)