Skip to content

Commit 3a65354

Browse files
ilslvdaxpeddahawkw
committed
tracing, tracing-futures: instrument Future inside Drop (#2562)
## Motivation Currently it is not possible to disable ANSI in `fmt::Subscriber` without enabling the "ansi" crate feature. This makes it difficult for users to implement interoperable settings that are controllable with crate features without having to pull in the dependencies "ansi" does. I hit this while writing an application with multiple logging options set during compile-time and I wanted to cut down on dependencies if possible. ## Solution This changes `fmt::Subscriber::with_ansi()` to not require the "ansi" feature flag. This way, `with_ansi(false)` can be called even when the "ansi" feature is disabled. Calling `with_ansi(true)` when the "ansi" feature is not enabled will panic in debug mode, or print a warning if debug assertions are disabled. Co-authored-by: daxpedda <[email protected]> Co-authored-by: Eliza Weisman <[email protected]>
1 parent 29d85b1 commit 3a65354

File tree

11 files changed

+360
-65
lines changed

11 files changed

+360
-65
lines changed

tracing-attributes/tests/async_fn.rs

+26
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ fn async_fn_only_enters_for_polls() {
9090
.exit(span::mock().named("test_async_fn"))
9191
.enter(span::mock().named("test_async_fn"))
9292
.exit(span::mock().named("test_async_fn"))
93+
.enter(span::mock().named("test_async_fn"))
94+
.exit(span::mock().named("test_async_fn"))
9395
.drop_span(span::mock().named("test_async_fn"))
9496
.done()
9597
.run_with_handle();
@@ -120,8 +122,12 @@ fn async_fn_nested() {
120122
.enter(span2.clone())
121123
.event(event::mock().with_fields(field::mock("nested").with_value(&true)))
122124
.exit(span2.clone())
125+
.enter(span2.clone())
126+
.exit(span2.clone())
123127
.drop_span(span2)
124128
.exit(span.clone())
129+
.enter(span.clone())
130+
.exit(span.clone())
125131
.drop_span(span)
126132
.done()
127133
.run_with_handle();
@@ -199,13 +205,19 @@ fn async_fn_with_async_trait() {
199205
.enter(span3.clone())
200206
.event(event::mock().with_fields(field::mock("val").with_value(&2u64)))
201207
.exit(span3.clone())
208+
.enter(span3.clone())
209+
.exit(span3.clone())
202210
.drop_span(span3)
203211
.new_span(span2.clone().with_field(field::mock("self")))
204212
.enter(span2.clone())
205213
.event(event::mock().with_fields(field::mock("val").with_value(&5u64)))
206214
.exit(span2.clone())
215+
.enter(span2.clone())
216+
.exit(span2.clone())
207217
.drop_span(span2)
208218
.exit(span.clone())
219+
.enter(span.clone())
220+
.exit(span.clone())
209221
.drop_span(span)
210222
.done()
211223
.run_with_handle();
@@ -256,6 +268,8 @@ fn async_fn_with_async_trait_and_fields_expressions() {
256268
)
257269
.enter(span.clone())
258270
.exit(span.clone())
271+
.enter(span.clone())
272+
.exit(span.clone())
259273
.drop_span(span)
260274
.done()
261275
.run_with_handle();
@@ -331,8 +345,12 @@ fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
331345
.with_field(field::mock("Self").with_value(&std::any::type_name::<TestImpl>())),
332346
)
333347
.enter(span4.clone())
348+
.exit(span4.clone())
349+
.enter(span4.clone())
334350
.exit(span4)
335351
.exit(span2.clone())
352+
.enter(span2.clone())
353+
.exit(span2.clone())
336354
.drop_span(span2)
337355
.new_span(
338356
span3
@@ -341,6 +359,8 @@ fn async_fn_with_async_trait_and_fields_expressions_with_generic_parameter() {
341359
)
342360
.enter(span3.clone())
343361
.exit(span3.clone())
362+
.enter(span3.clone())
363+
.exit(span3.clone())
344364
.drop_span(span3)
345365
.done()
346366
.run_with_handle();
@@ -382,6 +402,8 @@ fn out_of_scope_fields() {
382402
.new_span(span.clone())
383403
.enter(span.clone())
384404
.exit(span.clone())
405+
.enter(span.clone())
406+
.exit(span.clone())
385407
.drop_span(span)
386408
.done()
387409
.run_with_handle();
@@ -417,6 +439,8 @@ fn manual_impl_future() {
417439
.enter(span.clone())
418440
.event(poll_event())
419441
.exit(span.clone())
442+
.enter(span.clone())
443+
.exit(span.clone())
420444
.drop_span(span)
421445
.done()
422446
.run_with_handle();
@@ -448,6 +472,8 @@ fn manual_box_pin() {
448472
.enter(span.clone())
449473
.event(poll_event())
450474
.exit(span.clone())
475+
.enter(span.clone())
476+
.exit(span.clone())
451477
.drop_span(span)
452478
.done()
453479
.run_with_handle();

tracing-attributes/tests/err.rs

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ fn test_async() {
5757
.enter(span.clone())
5858
.event(event::mock().at_level(Level::ERROR))
5959
.exit(span.clone())
60+
.enter(span.clone())
61+
.exit(span.clone())
6062
.drop_span(span)
6163
.done()
6264
.run_with_handle();
@@ -111,6 +113,8 @@ fn test_mut_async() {
111113
.enter(span.clone())
112114
.event(event::mock().at_level(Level::ERROR))
113115
.exit(span.clone())
116+
.enter(span.clone())
117+
.exit(span.clone())
114118
.drop_span(span)
115119
.done()
116120
.run_with_handle();

tracing-attributes/tests/follows_from.rs

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ fn follows_from_async_test() {
5858
.follows_from(consequence.clone(), cause_b)
5959
.follows_from(consequence.clone(), cause_c)
6060
.enter(consequence.clone())
61+
.exit(consequence.clone())
62+
.enter(consequence.clone())
6163
.exit(consequence)
6264
.done()
6365
.run_with_handle();

tracing-attributes/tests/ret.rs

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ fn test_async() {
138138
.at_level(Level::INFO),
139139
)
140140
.exit(span.clone())
141+
.enter(span.clone())
142+
.exit(span.clone())
141143
.drop_span(span)
142144
.done()
143145
.run_with_handle();

tracing-futures/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ tokio-threadpool = { version = "0.1.18", optional = true }
4040
mio = { version = "0.6.23", optional = true }
4141

4242
[dev-dependencies]
43+
futures = "0.3.21"
4344
tokio-test = "0.4.2"
4445
tracing-core = { path = "../tracing-core", version = "0.1.28" }
4546
tracing-mock = { path = "../tracing-mock", features = ["tokio-test"] }

tracing-futures/src/executor/futures_01.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@ use futures_01::{
44
Future,
55
};
66

7-
macro_rules! deinstrument_err {
8-
($e:expr) => {
9-
$e.map_err(|e| {
10-
let kind = e.kind();
11-
let future = e.into_future().inner;
12-
ExecuteError::new(kind, future)
13-
})
14-
};
15-
}
16-
177
impl<T, F> Executor<F> for Instrumented<T>
188
where
199
T: Executor<Instrumented<F>>,
2010
F: Future<Item = (), Error = ()>,
2111
{
2212
fn execute(&self, future: F) -> Result<(), ExecuteError<F>> {
2313
let future = future.instrument(self.span.clone());
24-
deinstrument_err!(self.inner.execute(future))
14+
self.inner.execute(future).map_err(|e| {
15+
let kind = e.kind();
16+
let future = e.into_future().into_inner();
17+
ExecuteError::new(kind, future)
18+
})
2519
}
2620
}
2721

@@ -32,7 +26,11 @@ where
3226
{
3327
fn execute(&self, future: F) -> Result<(), ExecuteError<F>> {
3428
let future = self.with_dispatch(future);
35-
deinstrument_err!(self.inner.execute(future))
29+
self.inner.execute(future).map_err(|e| {
30+
let kind = e.kind();
31+
let future = e.into_future().inner;
32+
ExecuteError::new(kind, future)
33+
})
3634
}
3735
}
3836

0 commit comments

Comments
 (0)