From 920f1f154c9f8192210ab39db93de63646d887e2 Mon Sep 17 00:00:00 2001 From: "Aode (lion)" Date: Mon, 6 Dec 2021 12:43:03 -0600 Subject: [PATCH] Improve follows-from and parent span relationships --- Cargo.lock | 33 +++++++++++++++++++++++---------- src/id_or_span.rs | 34 ++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/process.rs | 35 +++++++++++++++++++---------------- 4 files changed, 77 insertions(+), 26 deletions(-) create mode 100644 src/id_or_span.rs diff --git a/Cargo.lock b/Cargo.lock index 2e76604..53ed759 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,9 +127,9 @@ dependencies = [ [[package]] name = "actix-server" -version = "2.0.0-beta.9" +version = "2.0.0-rc.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "411dd3296dd317ff5eff50baa13f31923ea40ec855dd7f2d3ed8639948f0195f" +checksum = "78c9b22794b8af1c2e02434873ef858f2a7db40dbbf861ce77a04cd81ac6b767" dependencies = [ "actix-rt", "actix-service", @@ -137,7 +137,7 @@ dependencies = [ "futures-core", "futures-util", "log", - "mio", + "mio 0.8.0", "num_cpus", "socket2", "tokio", @@ -1042,9 +1042,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.108" +version = "0.2.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" +checksum = "f98a04dce437184842841303488f70d0188c5f51437d2a834dc097eafa909a01" [[package]] name = "linked-hash-map" @@ -1154,6 +1154,19 @@ dependencies = [ "winapi", ] +[[package]] +name = "mio" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba272f85fa0b41fc91872be579b3bbe0f56b792aa361a380eb669469f68dafb2" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + [[package]] name = "miow" version = "0.3.7" @@ -1442,9 +1455,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" +checksum = "fb37d2df5df740e582f28f8560cf425f52bb267d872fe58358eadb554909f07a" dependencies = [ "unicode-xid", ] @@ -2096,7 +2109,7 @@ dependencies = [ "bytes", "libc", "memchr", - "mio", + "mio 0.7.14", "num_cpus", "once_cell", "parking_lot", @@ -2302,9 +2315,9 @@ dependencies = [ [[package]] name = "tracing-awc" -version = "0.1.0-beta.11" +version = "0.1.0-beta.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b79f7237bb46dbb931e2daafc1b89371dcf0759054ec9a2b1df40096a937a8f" +checksum = "8093ad66e1a7a3832eb450180abaf1e5f62ace52ff547b46a0b3fddfda8abf78" dependencies = [ "actix-http", "actix-service", diff --git a/src/id_or_span.rs b/src/id_or_span.rs new file mode 100644 index 0000000..8c78887 --- /dev/null +++ b/src/id_or_span.rs @@ -0,0 +1,34 @@ +use tracing::{Id, Span}; + +pub(crate) enum IdOrSpan { + Id(Option), + Span(Span), +} + +impl IdOrSpan { + pub(crate) fn take(&mut self) -> Self { + std::mem::replace(self, IdOrSpan::Id(None)) + } + + pub(crate) fn from_id(id: Option) -> Self { + IdOrSpan::Id(id) + } + + fn span(&self) -> Option<&Span> { + match self { + IdOrSpan::Span(ref span) => Some(span), + _ => None, + } + } + + pub(crate) fn as_span(&mut self, f: impl Fn(Option) -> Span) -> &Span { + let span = match self.take() { + Self::Id(opt) => f(opt), + Self::Span(span) => span, + }; + + *self = Self::Span(span); + + self.span().expect("Span should always exist") + } +} diff --git a/src/main.rs b/src/main.rs index 3c5ccce..1806d30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ mod error; mod exiftool; mod ffmpeg; mod file; +mod id_or_span; mod init_tracing; mod magick; mod map_error; diff --git a/src/process.rs b/src/process.rs index 057d809..8e8c428 100644 --- a/src/process.rs +++ b/src/process.rs @@ -1,4 +1,4 @@ -use crate::store::Store; +use crate::{id_or_span::IdOrSpan, store::Store}; use actix_rt::task::JoinHandle; use actix_web::web::Bytes; use std::{ @@ -13,14 +13,14 @@ use tokio::{ sync::oneshot::{channel, Receiver}, }; use tracing::Instrument; -use tracing::Span; +use tracing::{Id, Span}; #[derive(Debug)] struct StatusError; pub(crate) struct Process { child: Child, - span: Span, + id: Option, } struct DropHandle { @@ -31,7 +31,7 @@ pin_project_lite::pin_project! { struct ProcessRead { #[pin] inner: I, - span: Span, + span: IdOrSpan, err_recv: Receiver, err_closed: bool, handle: DropHandle, @@ -46,21 +46,19 @@ impl Process { fn spawn_span(&self) -> Span { let span = tracing::info_span!(parent: None, "Spawned command writer",); - span.follows_from(self.span.clone()); + span.follows_from(self.id.clone()); span } + #[tracing::instrument(name = "Spawning Command")] pub(crate) fn spawn(cmd: &mut Command) -> std::io::Result { let cmd = cmd.stdin(Stdio::piped()).stdout(Stdio::piped()); - let span = tracing::info_span!( - "Spawning Command", - command = &tracing::field::debug(&cmd), - exception.message = &tracing::field::Empty, - exception.details = &tracing::field::Empty, - ); - cmd.spawn().map(|child| Process { child, span }) + cmd.spawn().map(|child| Process { + child, + id: Span::current().id(), + }) } pub(crate) async fn wait(mut self) -> std::io::Result<()> { @@ -104,7 +102,7 @@ impl Process { Some(ProcessRead { inner: stdout, - span: self.span, + span: IdOrSpan::from_id(self.id), err_recv: rx, err_closed: false, handle: DropHandle { inner: handle }, @@ -137,7 +135,7 @@ impl Process { Some(ProcessRead { inner: stdout, - span: self.span, + span: IdOrSpan::from_id(self.id), err_recv: rx, err_closed: false, handle: DropHandle { inner: handle }, @@ -181,7 +179,7 @@ impl Process { Some(ProcessRead { inner: stdout, - span: self.span, + span: IdOrSpan::from_id(self.id), err_recv: rx, err_closed: false, handle: DropHandle { inner: handle }, @@ -200,11 +198,16 @@ where ) -> Poll> { let this = self.as_mut().project(); - let span = this.span; let err_recv = this.err_recv; let err_closed = this.err_closed; let inner = this.inner; + let span = this.span.as_span(|id| { + let span = tracing::info_span!("Processing Command"); + span.follows_from(id); + span + }); + span.in_scope(|| { if !*err_closed { if let Poll::Ready(res) = Pin::new(err_recv).poll(cx) {