Switch to tracing

This commit is contained in:
asonix 2020-06-14 13:39:12 -05:00
parent 2a69e0a4a3
commit eefb669e0d
5 changed files with 27 additions and 11 deletions

View file

@ -16,15 +16,16 @@ actix-rt = "1.1.1"
actix-web = "3.0.0-alpha.2" actix-web = "3.0.0-alpha.2"
bytes = "0.5.0" bytes = "0.5.0"
futures = "0.3.4" futures = "0.3.4"
log = "0.4.8"
mime = "0.3.16" mime = "0.3.16"
thiserror = "1.0" thiserror = "1.0"
tokio = { version = "0.2.21", features = ["sync"] } tokio = { version = "0.2.21", features = ["sync"] }
tracing = "0.1.15"
tracing-futures = "0.2.4"
[dev-dependencies] [dev-dependencies]
actix-fs = { git = "https://git.asonix.dog/asonix/actix-fs" } actix-fs = { git = "https://git.asonix.dog/asonix/actix-fs" }
anyhow = "1.0" anyhow = "1.0"
env_logger = "0.7.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
thiserror = "1.0" thiserror = "1.0"
tracing-subscriber = { version = "0.2.5", features = ["fmt", "tracing-log"] }

View file

@ -7,7 +7,6 @@ use actix_web::{
}; };
use bytes::Bytes; use bytes::Bytes;
use futures::stream::{Stream, TryStreamExt}; use futures::stream::{Stream, TryStreamExt};
use log::info;
use std::{ use std::{
env, env,
pin::Pin, pin::Pin,
@ -16,6 +15,7 @@ use std::{
Arc, Arc,
}, },
}; };
use tracing::info;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
struct AppState { struct AppState {
@ -85,8 +85,12 @@ async fn save_file(
#[actix_rt::main] #[actix_rt::main]
async fn main() -> Result<(), anyhow::Error> { async fn main() -> Result<(), anyhow::Error> {
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "upload=info"); env::set_var("RUST_LOG", "upload=info");
env_logger::init(); }
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
let file_count = Arc::new(AtomicUsize::new(0)); let file_count = Arc::new(AtomicUsize::new(0));

View file

@ -1,4 +1,4 @@
use log::error; use tracing::error;
use std::future::Future; use std::future::Future;
use tokio::sync::oneshot::channel; use tokio::sync::oneshot::channel;

View file

@ -20,7 +20,7 @@
use crate::Error; use crate::Error;
use bytes::Bytes; use bytes::Bytes;
use futures::Stream; use futures::Stream;
use log::trace; use tracing::trace;
use mime::Mime; use mime::Mime;
use std::{ use std::{
collections::{HashMap, VecDeque}, collections::{HashMap, VecDeque},

View file

@ -29,7 +29,6 @@ use futures::{
select, select,
stream::{FuturesUnordered, StreamExt}, stream::{FuturesUnordered, StreamExt},
}; };
use log::trace;
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::Path, path::Path,
@ -38,6 +37,8 @@ use std::{
Arc, Arc,
}, },
}; };
use tracing::{trace, Span};
use tracing_futures::Instrument;
fn consolidate(mf: MultipartForm) -> Value { fn consolidate(mf: MultipartForm) -> Value {
mf.into_iter().fold( mf.into_iter().fold(
@ -211,6 +212,8 @@ async fn handle_stream_field(
/// Handle multipart streams from Actix Web /// Handle multipart streams from Actix Web
pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Result<Value, Error> { pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Result<Value, Error> {
let parent = Span::current();
let mut multipart_form = Vec::new(); let mut multipart_form = Vec::new();
let mut file_count: u32 = 0; let mut file_count: u32 = 0;
let mut field_count: u32 = 0; let mut field_count: u32 = 0;
@ -223,9 +226,7 @@ pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Resu
select! { select! {
opt = m.next() => { opt = m.next() => {
if let Some(res) = opt { if let Some(res) = opt {
let field = res?; unordered.push(process_field(res?, form.clone(), &parent));
unordered.push(crate::spawn(handle_stream_field(field, form.clone())));
} }
} }
opt = unordered.next() => { opt = unordered.next() => {
@ -246,6 +247,16 @@ pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Resu
Ok(consolidate(multipart_form)) Ok(consolidate(multipart_form))
} }
async fn process_field(
field: actix_multipart::Field,
form: Form,
parent: &Span,
) -> Result<Result<MultipartHash, Error>, crate::spawn::Canceled> {
let span = tracing::info_span!(parent: parent, "field");
crate::spawn(handle_stream_field(field, form).instrument(span)).await
}
fn count( fn count(
content: &MultipartContent, content: &MultipartContent,
mut file_count: u32, mut file_count: u32,