diff --git a/Cargo.toml b/Cargo.toml index f6011ef2..5911a8ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -82,7 +82,7 @@ tracing-futures = { version = "0.2.5", optional = true, features = [ "std-future", "futures-03", ] } -tracinglib = { version = "0.1.25", optional = true, package = "tracing" } +tracinglib = { version = "0.1.35", optional = true, package = "tracing" } url = { version = "2.2.1", optional = true } uuid = { version = "1.0.0", optional = true, features = ["v4", "serde"] } uuid08 = { version = "0.8", package = "uuid", optional = true, features = [ diff --git a/src/dataloader/mod.rs b/src/dataloader/mod.rs index f9d998c6..b71190ae 100644 --- a/src/dataloader/mod.rs +++ b/src/dataloader/mod.rs @@ -75,6 +75,10 @@ use fnv::FnvHashMap; use futures_channel::oneshot; use futures_timer::Delay; use futures_util::future::BoxFuture; +#[cfg(feature = "tracing")] +use tracinglib as tracing; +#[cfg(feature = "tracing")] +use tracing::{instrument, info_span, Instrument}; #[allow(clippy::type_complexity)] struct ResSender> { @@ -128,6 +132,7 @@ struct DataLoaderInner { } impl DataLoaderInner { + #[cfg_attr(feature = "tracing", instrument(skip_all))] async fn do_load(&self, disable_cache: bool, (keys, senders): KeysAndSender) where K: Send + Sync + Hash + Eq + Clone + 'static, @@ -275,6 +280,7 @@ impl DataLoader { } /// Use this `DataLoader` load a data. + #[cfg_attr(feature = "tracing", instrument(skip_all))] pub async fn load_one(&self, key: K) -> Result, T::Error> where K: Send + Sync + Hash + Eq + Clone + 'static, @@ -285,6 +291,7 @@ impl DataLoader { } /// Use this `DataLoader` to load some data. + #[cfg_attr(feature = "tracing", instrument(skip_all))] pub async fn load_many(&self, keys: I) -> Result, T::Error> where K: Send + Sync + Hash + Eq + Clone + 'static, @@ -357,16 +364,17 @@ impl DataLoader { Action::ImmediateLoad(keys) => { let inner = self.inner.clone(); let disable_cache = self.disable_cache.load(Ordering::SeqCst); - (self.spawner)(Box::pin( - async move { inner.do_load(disable_cache, keys).await }, - )); + let task = async move { inner.do_load(disable_cache, keys).await }; + #[cfg(feature = "tracing")] + let task = task.instrument(info_span!("immediate_load")); + (self.spawner)(Box::pin(task)); } Action::StartFetch => { let inner = self.inner.clone(); let disable_cache = self.disable_cache.load(Ordering::SeqCst); let delay = self.delay; - (self.spawner)(Box::pin(async move { + let task = async move { Delay::new(delay).await; let keys = { @@ -382,7 +390,10 @@ impl DataLoader { if !keys.0.is_empty() { inner.do_load(disable_cache, keys).await } - })) + }; + #[cfg(feature = "tracing")] + let task = task.instrument(info_span!("start_fetch")); + (self.spawner)(Box::pin(task)) } Action::Delay => {} } @@ -394,6 +405,7 @@ impl DataLoader { /// /// **NOTE: If the cache type is [NoCache], this function will not take /// effect. ** + #[cfg_attr(feature = "tracing", instrument(skip_all))] pub async fn feed_many(&self, values: I) where K: Send + Sync + Hash + Eq + Clone + 'static, @@ -418,6 +430,7 @@ impl DataLoader { /// /// **NOTE: If the cache type is [NoCache], this function will not take /// effect. ** + #[cfg_attr(feature = "tracing", instrument(skip_all))] pub async fn feed_one(&self, key: K, value: T::Value) where K: Send + Sync + Hash + Eq + Clone + 'static, @@ -430,6 +443,7 @@ impl DataLoader { /// /// **NOTE: If the cache type is [NoCache], this function will not take /// effect. ** + #[cfg_attr(feature = "tracing", instrument(skip_all))] pub fn clear(&self) where K: Send + Sync + Hash + Eq + Clone + 'static,