mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-02-28 08:37:36 +00:00
Use a quieter root span builder, don't pretty-print logs
This commit is contained in:
parent
cffed4aa27
commit
7a1e6c2f36
3 changed files with 75 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
||||||
#![recursion_limit = "512"]
|
#![recursion_limit = "512"]
|
||||||
pub mod api_routes;
|
pub mod api_routes;
|
||||||
pub mod code_migrations;
|
pub mod code_migrations;
|
||||||
|
pub mod root_span_builder;
|
||||||
pub mod scheduled_tasks;
|
pub mod scheduled_tasks;
|
||||||
|
|
||||||
use lemmy_utils::LemmyError;
|
use lemmy_utils::LemmyError;
|
||||||
|
@ -13,7 +14,7 @@ pub fn init_tracing() -> Result<(), LemmyError> {
|
||||||
LogTracer::init()?;
|
LogTracer::init()?;
|
||||||
|
|
||||||
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
||||||
let format_layer = tracing_subscriber::fmt::layer().pretty();
|
let format_layer = tracing_subscriber::fmt::layer();
|
||||||
|
|
||||||
let subscriber = Registry::default()
|
let subscriber = Registry::default()
|
||||||
.with(env_filter)
|
.with(env_filter)
|
||||||
|
|
|
@ -18,6 +18,7 @@ use lemmy_server::{
|
||||||
api_routes,
|
api_routes,
|
||||||
code_migrations::run_advanced_migrations,
|
code_migrations::run_advanced_migrations,
|
||||||
init_tracing,
|
init_tracing,
|
||||||
|
root_span_builder::QuieterRootSpanBuilder,
|
||||||
scheduled_tasks,
|
scheduled_tasks,
|
||||||
};
|
};
|
||||||
use lemmy_utils::{
|
use lemmy_utils::{
|
||||||
|
@ -124,7 +125,7 @@ async fn main() -> Result<(), LemmyError> {
|
||||||
let rate_limiter = rate_limiter.clone();
|
let rate_limiter = rate_limiter.clone();
|
||||||
App::new()
|
App::new()
|
||||||
.wrap(actix_web::middleware::Logger::default())
|
.wrap(actix_web::middleware::Logger::default())
|
||||||
.wrap(TracingLogger::default())
|
.wrap(TracingLogger::<QuieterRootSpanBuilder>::new())
|
||||||
.app_data(Data::new(context))
|
.app_data(Data::new(context))
|
||||||
// The routes
|
// The routes
|
||||||
.configure(|cfg| api_routes::config(cfg, &rate_limiter))
|
.configure(|cfg| api_routes::config(cfg, &rate_limiter))
|
||||||
|
|
71
src/root_span_builder.rs
Normal file
71
src/root_span_builder.rs
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
use actix_web::{http::StatusCode, ResponseError};
|
||||||
|
use tracing::Span;
|
||||||
|
use tracing_actix_web::RootSpanBuilder;
|
||||||
|
|
||||||
|
// Code in this module adapted from DefaultRootSpanBuilder
|
||||||
|
// https://github.com/LukeMathWalker/tracing-actix-web/blob/main/src/root_span_builder.rs
|
||||||
|
// and root_span!
|
||||||
|
// https://github.com/LukeMathWalker/tracing-actix-web/blob/main/src/root_span_macro.rs
|
||||||
|
|
||||||
|
pub struct QuieterRootSpanBuilder;
|
||||||
|
|
||||||
|
impl RootSpanBuilder for QuieterRootSpanBuilder {
|
||||||
|
fn on_request_start(request: &actix_web::dev::ServiceRequest) -> Span {
|
||||||
|
let request_id = tracing_actix_web::root_span_macro::private::get_request_id(request);
|
||||||
|
|
||||||
|
tracing::info_span!(
|
||||||
|
"HTTP request",
|
||||||
|
http.method = %request.method(),
|
||||||
|
http.scheme = request.connection_info().scheme(),
|
||||||
|
http.host = %request.connection_info().host(),
|
||||||
|
http.target = %request.uri().path(),
|
||||||
|
http.status_code = tracing::field::Empty,
|
||||||
|
otel.kind = "server",
|
||||||
|
otel.status_code = tracing::field::Empty,
|
||||||
|
trace_id = tracing::field::Empty,
|
||||||
|
request_id = %request_id,
|
||||||
|
exception.message = tracing::field::Empty,
|
||||||
|
// Not proper OpenTelemetry, but their terminology is fairly exception-centric
|
||||||
|
exception.details = tracing::field::Empty,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_request_end<B>(
|
||||||
|
span: tracing::Span,
|
||||||
|
outcome: &Result<actix_web::dev::ServiceResponse<B>, actix_web::Error>,
|
||||||
|
) {
|
||||||
|
match &outcome {
|
||||||
|
Ok(response) => {
|
||||||
|
if let Some(error) = response.response().error() {
|
||||||
|
// use the status code already constructed for the outgoing HTTP response
|
||||||
|
handle_error(span, response.status(), error.as_response_error());
|
||||||
|
} else {
|
||||||
|
let code: i32 = response.response().status().as_u16().into();
|
||||||
|
span.record("http.status_code", &code);
|
||||||
|
span.record("otel.status_code", &"OK");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
let response_error = error.as_response_error();
|
||||||
|
handle_error(span, response_error.status_code(), response_error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_error(span: Span, status_code: StatusCode, response_error: &dyn ResponseError) {
|
||||||
|
// pre-formatting errors is a workaround for https://github.com/tokio-rs/tracing/issues/1565
|
||||||
|
let display = format!("{}", response_error);
|
||||||
|
let debug = format!("{:?}", response_error);
|
||||||
|
span.record("exception.message", &tracing::field::display(display));
|
||||||
|
span.record("exception.details", &tracing::field::display(debug));
|
||||||
|
let code: i32 = status_code.as_u16().into();
|
||||||
|
|
||||||
|
span.record("http.status_code", &code);
|
||||||
|
|
||||||
|
if status_code.is_client_error() {
|
||||||
|
span.record("otel.status_code", &"OK");
|
||||||
|
} else {
|
||||||
|
span.record("otel.status_code", &"ERROR");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue