mirror of
https://github.com/meilisearch/meilisearch.git
synced 2024-11-27 04:25:06 +08:00
update the actix-web trace
This commit is contained in:
parent
1502382316
commit
cfb3e6b51f
@ -38,7 +38,7 @@ use meilisearch_types::versioning::{check_version_file, create_version_file};
|
|||||||
use meilisearch_types::{compression, milli, VERSION_FILE_NAME};
|
use meilisearch_types::{compression, milli, VERSION_FILE_NAME};
|
||||||
pub use option::Opt;
|
pub use option::Opt;
|
||||||
use option::ScheduleSnapshot;
|
use option::ScheduleSnapshot;
|
||||||
use tracing::error;
|
use tracing::{error, info_span};
|
||||||
use tracing_subscriber::filter::Targets;
|
use tracing_subscriber::filter::Targets;
|
||||||
|
|
||||||
use crate::error::MeilisearchHttpError;
|
use crate::error::MeilisearchHttpError;
|
||||||
@ -136,11 +136,49 @@ pub fn create_app(
|
|||||||
.allow_any_method()
|
.allow_any_method()
|
||||||
.max_age(86_400), // 24h
|
.max_age(86_400), // 24h
|
||||||
)
|
)
|
||||||
.wrap(tracing_actix_web::TracingLogger::default())
|
.wrap(tracing_actix_web::TracingLogger::<AwebTracingLogger>::new())
|
||||||
.wrap(actix_web::middleware::Compress::default())
|
.wrap(actix_web::middleware::Compress::default())
|
||||||
.wrap(actix_web::middleware::NormalizePath::new(actix_web::middleware::TrailingSlash::Trim))
|
.wrap(actix_web::middleware::NormalizePath::new(actix_web::middleware::TrailingSlash::Trim))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct AwebTracingLogger;
|
||||||
|
|
||||||
|
impl tracing_actix_web::RootSpanBuilder for AwebTracingLogger {
|
||||||
|
fn on_request_start(request: &actix_web::dev::ServiceRequest) -> tracing::Span {
|
||||||
|
use tracing::field::Empty;
|
||||||
|
|
||||||
|
let conn_info = request.connection_info();
|
||||||
|
let headers = request.headers();
|
||||||
|
let user_agent = headers
|
||||||
|
.get(http::header::USER_AGENT)
|
||||||
|
.map(|value| String::from_utf8_lossy(value.as_bytes()).into_owned())
|
||||||
|
.unwrap_or_default();
|
||||||
|
info_span!("HTTP request", method = %request.method(), host = conn_info.host(), route = %request.path(), query_parameters = %request.query_string(), %user_agent, status_code = Empty, error = Empty)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_request_end<B: MessageBody>(
|
||||||
|
span: tracing::Span,
|
||||||
|
outcome: &Result<ServiceResponse<B>, actix_web::Error>,
|
||||||
|
) {
|
||||||
|
match &outcome {
|
||||||
|
Ok(response) => {
|
||||||
|
let code: i32 = response.response().status().as_u16().into();
|
||||||
|
span.record("status_code", code);
|
||||||
|
|
||||||
|
if let Some(error) = response.response().error() {
|
||||||
|
// use the status code already constructed for the outgoing HTTP response
|
||||||
|
span.record("error", &tracing::field::display(error.as_response_error()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
let code: i32 = error.error_response().status().as_u16().into();
|
||||||
|
span.record("status_code", code);
|
||||||
|
span.record("error", &tracing::field::display(error.as_response_error()));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
enum OnFailure {
|
enum OnFailure {
|
||||||
RemoveDb,
|
RemoveDb,
|
||||||
KeepDb,
|
KeepDb,
|
||||||
|
@ -34,7 +34,7 @@ fn setup(opt: &Opt) -> anyhow::Result<LogRouteHandle> {
|
|||||||
|
|
||||||
let subscriber = tracing_subscriber::registry().with(route_layer).with(
|
let subscriber = tracing_subscriber::registry().with(route_layer).with(
|
||||||
tracing_subscriber::fmt::layer()
|
tracing_subscriber::fmt::layer()
|
||||||
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::NEW)
|
.with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE)
|
||||||
.with_filter(
|
.with_filter(
|
||||||
tracing_subscriber::filter::LevelFilter::from_str(&opt.log_level.to_string())
|
tracing_subscriber::filter::LevelFilter::from_str(&opt.log_level.to_string())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
Loading…
Reference in New Issue
Block a user