Improve follows-from and parent span relationships

This commit is contained in:
Aode (lion) 2021-12-06 12:43:03 -06:00
parent 4eec906c5d
commit 920f1f154c
4 changed files with 77 additions and 26 deletions

33
Cargo.lock generated
View File

@ -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",

34
src/id_or_span.rs Normal file
View File

@ -0,0 +1,34 @@
use tracing::{Id, Span};
pub(crate) enum IdOrSpan {
Id(Option<Id>),
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<Id>) -> 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<Id>) -> 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")
}
}

View File

@ -31,6 +31,7 @@ mod error;
mod exiftool;
mod ffmpeg;
mod file;
mod id_or_span;
mod init_tracing;
mod magick;
mod map_error;

View File

@ -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<Id>,
}
struct DropHandle {
@ -31,7 +31,7 @@ pin_project_lite::pin_project! {
struct ProcessRead<I> {
#[pin]
inner: I,
span: Span,
span: IdOrSpan,
err_recv: Receiver<std::io::Error>,
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<Self> {
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<std::io::Result<()>> {
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) {