Skip to content

Commit 2d19f3d

Browse files
authored
Merge pull request #1031 from rust-lang/log-error-details
Log the error we return to the client
2 parents 300a426 + 2c30311 commit 2d19f3d

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

ui/Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ strum = { version = "0.25.0", features = ["derive"] }
2929
tempfile = "3"
3030
tokio = { version = "1.9", features = ["macros", "time", "process", "rt-multi-thread"] }
3131
tokio-util = "0.7.9"
32-
tower-http = { version = "0.5", features = ["cors", "fs", "set-header", "trace"] }
32+
tower-http = { version = "0.5", features = ["cors", "fs", "request-id", "set-header", "trace"] }
3333
tracing = { version = "0.1.37", features = ["attributes"] }
3434
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }

ui/src/server_axum.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use axum::{
1919
extract::{self, ws::WebSocketUpgrade, Extension, Path},
2020
handler::Handler,
2121
http::{
22-
header, request::Parts, uri::PathAndQuery, HeaderValue, Method, Request, StatusCode, Uri,
22+
header, request::Parts, uri::PathAndQuery, HeaderName, HeaderValue, Method, Request,
23+
StatusCode, Uri,
2324
},
2425
middleware,
2526
response::IntoResponse,
@@ -44,10 +45,12 @@ use std::{
4445
use tokio::sync::Mutex;
4546
use tower_http::{
4647
cors::{self, CorsLayer},
48+
request_id::{MakeRequestUuid, PropagateRequestIdLayer, SetRequestIdLayer},
4749
services::ServeDir,
4850
set_header::SetResponseHeader,
4951
trace::TraceLayer,
5052
};
53+
use tracing::{error, error_span, field};
5154

5255
const ONE_HOUR: Duration = Duration::from_secs(60 * 60);
5356
const CORS_CACHE_TIME_TO_LIVE: Duration = ONE_HOUR;
@@ -113,8 +116,39 @@ pub(crate) async fn serve(config: Config) {
113116
});
114117
}
115118

119+
let x_request_id = HeaderName::from_static("x-request-id");
120+
116121
// Basic access logging
117-
app = app.layer(TraceLayer::new_for_http());
122+
app = app.layer(
123+
TraceLayer::new_for_http().make_span_with(move |req: &Request<_>| {
124+
const REQUEST_ID: &str = "request_id";
125+
126+
let method = req.method();
127+
let uri = req.uri();
128+
let request_id = req
129+
.headers()
130+
.get(&x_request_id)
131+
.and_then(|id| id.to_str().ok());
132+
133+
let span = error_span!("request", %method, %uri, { REQUEST_ID } = field::Empty);
134+
135+
if let Some(request_id) = request_id {
136+
span.record(REQUEST_ID, field::display(request_id));
137+
}
138+
139+
span
140+
}),
141+
);
142+
143+
let x_request_id = HeaderName::from_static("x-request-id");
144+
145+
// propagate `x-request-id` headers from request to response
146+
app = app.layer(PropagateRequestIdLayer::new(x_request_id.clone()));
147+
148+
app = app.layer(SetRequestIdLayer::new(
149+
x_request_id.clone(),
150+
MakeRequestUuid::default(),
151+
));
118152

119153
let listener = tokio::net::TcpListener::bind(config.server_socket_addr())
120154
.await
@@ -738,6 +772,7 @@ impl IntoResponse for Error {
738772
.map(|(_, s, _)| s)
739773
.reduce(|l, r| l + ": " + &r)
740774
.unwrap_or_default();
775+
error!(error, "Returning an error to the client");
741776
let resp = Json(ErrorJson { error });
742777
let resp = (StatusCode::INTERNAL_SERVER_ERROR, resp);
743778
resp.into_response()

0 commit comments

Comments
 (0)