Skip to content

Commit

Permalink
Adds tracing feature flag for deadpool-sync and sqlite. (#238)
Browse files Browse the repository at this point in the history
With deadpool-sqlite, the database interactions happen in a sync context
(due to using rusqlite) on a separate thread. The tracing crate can add
conetxt to any logs (events). Due to the database interactions happening
on a separate thread, this context gets lost. Enabling the tracing
feature allows the information to be preserved.

Related: dbrgn/tracing-test#23

Note: I don't know if this is something that should rather be
implemented in the `spawn_blocking` method of Tokio.
  • Loading branch information
xfbs authored Sep 6, 2023
1 parent 45c42bb commit b60a269
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ default = ["rt_tokio_1"]
rt_tokio_1 = ["deadpool/rt_tokio_1"]
rt_async-std_1 = ["deadpool/rt_async-std_1"]
serde = ["deadpool/serde", "serde_1"]
tracing = ["deadpool-sync/tracing"]

[dependencies]
deadpool = { path = "../", version = "0.10.0", default-features = false, features = [
Expand Down
1 change: 1 addition & 0 deletions sqlite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ inside a separate thread.
| `rt_tokio_1` | Enable support for [tokio](https://crates.io/crates/tokio) crate | `deadpool/rt_tokio_1` | yes |
| `rt_async-std_1` | Enable support for [async-std](https://crates.io/crates/config) crate | `deadpool/rt_async-std_1` | no |
| `serde` | Enable support for [serde](https://crates.io/crates/serde) crate | `deadpool/serde`, `serde/derive` | no |
| `tracing` | Enable support for [tracing](https://github.com/tokio-rs/tracing) by propagating Spans in the `interact()` calls. Enable this if you use the `tracing` crate and you want to get useful traces from within `interact()` calls. | `tracing` | no |

## Example

Expand Down
4 changes: 4 additions & 0 deletions sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
tracing = ["dep:tracing"]

[dependencies]
deadpool-runtime = { version = "0.1.2", path = "../runtime" }
tracing = { version = "0.1", optional = true }
4 changes: 4 additions & 0 deletions sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ where
R: Send + 'static,
{
let arc = self.obj.clone();
#[cfg(feature = "tracing")]
let span = tracing::Span::current();
self.runtime
.spawn_blocking(move || {
let mut guard = arc.lock().unwrap();
let conn = guard.as_mut().unwrap();
#[cfg(feature = "tracing")]
let _span = span.enter();
f(conn)
})
.await
Expand Down

0 comments on commit b60a269

Please sign in to comment.