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"
bytes = "0.5.0"
futures = "0.3.4"
log = "0.4.8"
mime = "0.3.16"
thiserror = "1.0"
tokio = { version = "0.2.21", features = ["sync"] }
tracing = "0.1.15"
tracing-futures = "0.2.4"
[dev-dependencies]
actix-fs = { git = "https://git.asonix.dog/asonix/actix-fs" }
anyhow = "1.0"
env_logger = "0.7.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "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 futures::stream::{Stream, TryStreamExt};
use log::info;
use std::{
env,
pin::Pin,
@ -16,6 +15,7 @@ use std::{
Arc,
},
};
use tracing::info;
#[derive(Clone, Debug)]
struct AppState {
@ -85,8 +85,12 @@ async fn save_file(
#[actix_rt::main]
async fn main() -> Result<(), anyhow::Error> {
env::set_var("RUST_LOG", "upload=info");
env_logger::init();
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "upload=info");
}
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
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 tokio::sync::oneshot::channel;

View file

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

View file

@ -29,7 +29,6 @@ use futures::{
select,
stream::{FuturesUnordered, StreamExt},
};
use log::trace;
use std::{
collections::HashMap,
path::Path,
@ -38,6 +37,8 @@ use std::{
Arc,
},
};
use tracing::{trace, Span};
use tracing_futures::Instrument;
fn consolidate(mf: MultipartForm) -> Value {
mf.into_iter().fold(
@ -211,6 +212,8 @@ async fn handle_stream_field(
/// Handle multipart streams from Actix Web
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 file_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! {
opt = m.next() => {
if let Some(res) = opt {
let field = res?;
unordered.push(crate::spawn(handle_stream_field(field, form.clone())));
unordered.push(process_field(res?, form.clone(), &parent));
}
}
opt = unordered.next() => {
@ -246,6 +247,16 @@ pub async fn handle_multipart(m: actix_multipart::Multipart, form: Form) -> Resu
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(
content: &MultipartContent,
mut file_count: u32,