Skip to content

Commit 4bed7bc

Browse files
authored
only depend on tokio if spawn capability is desired (#25)
1 parent 3aa942d commit 4bed7bc

File tree

8 files changed

+43
-7
lines changed

8 files changed

+43
-7
lines changed

Cargo.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "await-tree"
3-
version = "0.3.0-alpha.2"
3+
version = "0.3.0-alpha.3"
44
edition = "2021"
55
description = "Generate accurate and informative tree dumps of asynchronous tasks."
66
repository = "https://github.com/risingwavelabs/await-tree"
@@ -11,6 +11,7 @@ license = "Apache-2.0"
1111

1212
[features]
1313
serde = ["dep:serde", "flexstr/serde"]
14+
tokio = ["dep:tokio"]
1415

1516
[dependencies]
1617
coarsetime = "0.1"
@@ -22,15 +23,16 @@ itertools = "0.12"
2223
parking_lot = "0.12"
2324
pin-project = "1"
2425
serde = { version = "1", features = ["derive"], optional = true }
25-
tokio = { version = "1", features = ["rt"] }
26+
task-local = "0.1"
27+
tokio = { version = "1", features = ["rt"], optional = true }
2628
tracing = "0.1"
2729
weak-table = "0.3.2"
2830

2931
[dev-dependencies]
3032
criterion = { version = "0.5", features = ["async", "async_tokio"] }
3133
futures = { version = "0.3", default-features = false, features = ["alloc"] }
3234
serde_json = "1"
33-
tokio = { version = "1", features = ["rt", "rt-multi-thread", "time", "macros"] }
35+
tokio = { version = "1", features = ["full"] }
3436

3537
[[bench]]
3638
name = "basic"
@@ -49,3 +51,11 @@ rpath = false
4951
[[example]]
5052
name = "serde"
5153
required-features = ["serde"]
54+
55+
[[example]]
56+
name = "spawn"
57+
required-features = ["tokio"]
58+
59+
[[example]]
60+
name = "global"
61+
required-features = ["tokio"]

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ async fn foo() {
3333
// Init the global registry to start tracing the tasks.
3434
await_tree::init_global_registry(Default::default());
3535
// Spawn a task with root span "foo" and key "foo".
36+
// Note: The `spawn` function requires the `tokio` feature to be enabled.
3637
await_tree::spawn("foo", "foo", foo());
3738
// Let the tasks run for a while.
3839
sleep(Duration::from_secs(1)).await;
@@ -64,7 +65,20 @@ println!("{tree}");
6465
println!("{json}");
6566
```
6667

67-
### Compared to `async-backtrace`
68+
- `tokio`: Enables integration with the Tokio runtime, providing task spawning capabilities through `spawn` and `spawn_anonymous` functions. This feature is required for the examples that demonstrate spawning tasks.
69+
70+
```rust
71+
// Enable the tokio feature in Cargo.toml
72+
// await-tree = { version = "<version>", features = ["tokio"] }
73+
74+
// Then you can spawn tasks with await-tree instrumentation
75+
await_tree::spawn("task-key", "root_span", async {
76+
// Your async code here
77+
work().instrument_await("work_span").await;
78+
});
79+
```
80+
81+
## Compared to `async-backtrace`
6882

6983
[`tokio-rs/async-backtrace`](https://github.com/tokio-rs/async-backtrace) is a similar crate that also provides the ability to dump the execution tree of async tasks. Here are some differences between `await-tree` and `async-backtrace`:
7084

examples/global.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
// limitations under the License.
1414

1515
//! This example shows the usage of the global registry.
16+
//!
17+
//! Note: This example requires the `tokio` feature to be enabled.
18+
//! Run with: `cargo run --example global --features tokio`
19+
20+
#![cfg(feature = "tokio")]
1621

1722
use std::time::Duration;
1823

examples/spawn.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
//! This example shows how to spawn tasks with `await_tree::spawn` that are automatically registered
1616
//! to the current registry of the scope.
17+
//!
18+
//! Note: This example requires the `tokio` feature to be enabled.
19+
//! Run with: `cargo run --example spawn --features tokio`
20+
21+
#![cfg(feature = "tokio")]
1722

1823
use std::time::Duration;
1924

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ mod obj_utils;
7878
mod registry;
7979
mod root;
8080
mod span;
81+
#[cfg(feature = "tokio")]
8182
mod spawn;
8283

8384
pub use context::{current_tree, Tree};
@@ -86,6 +87,7 @@ pub use global::init_global_registry;
8687
pub use registry::{AnyKey, Config, ConfigBuilder, ConfigBuilderError, Key, Registry};
8788
pub use root::TreeRoot;
8889
pub use span::{Span, SpanExt};
90+
#[cfg(feature = "tokio")]
8991
pub use spawn::{spawn, spawn_anonymous};
9092

9193
#[doc(hidden)]

src/root.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct TreeRoot {
2626
pub(crate) registry: WeakRegistry,
2727
}
2828

29-
tokio::task_local! {
29+
task_local::task_local! {
3030
static ROOT: TreeRoot
3131
}
3232

src/spawn.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
// TODO: should we consider exposing `current_registry`
1616
// so that users can not only spawn tasks but also get and collect trees?
1717

18-
// TODO: should we support "global registry" for users to quick start?
19-
2018
use std::future::Future;
2119

2220
use tokio::task::JoinHandle;

src/tests/spawn.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#![cfg(feature = "tokio")]
16+
1517
use std::time::Duration;
1618

1719
use futures::future::pending;

0 commit comments

Comments
 (0)