Add tracing spans to the server

This commit is contained in:
Filip Tibell 2025-04-28 12:54:41 +02:00
parent 91d3becfa7
commit 1d5535dac0
No known key found for this signature in database
3 changed files with 45 additions and 19 deletions

1
Cargo.lock generated
View file

@ -1913,6 +1913,7 @@ dependencies = [
"pin-project-lite",
"rustls 0.23.26",
"rustls-pki-types",
"tracing",
"url",
"urlencoding",
"webpki",

View file

@ -32,6 +32,7 @@ hyper = { version = "1.6", features = ["http1", "client", "server"] }
pin-project-lite = "0.2"
rustls = "0.23"
rustls-pki-types = "1.11"
tracing = "0.1"
url = "2.5"
urlencoding = "2.1"
webpki = "0.22"

View file

@ -50,7 +50,7 @@ impl HyperService<HyperRequest<Incoming>> for Service {
let lua = lua.clone();
async move {
if let Err(_err) = handle_websocket(lua, handler, req).await {
// TODO: Propagare the error somehow?
// TODO: Propagate the error somehow?
}
}
});
@ -67,7 +67,7 @@ impl HyperService<HyperRequest<Incoming>> for Service {
match handle_request(lua, handler, req, address).await {
Ok(response) => Ok(response),
Err(_err) => {
// TODO: Propagare the error somehow?
// TODO: Propagate the error somehow?
Ok(HyperResponse::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Full::new(Bytes::from("Lune: Internal server error")))
@ -78,42 +78,66 @@ impl HyperService<HyperRequest<Incoming>> for Service {
}
}
#[tracing::instrument(skip_all)]
async fn handle_request(
lua: Lua,
handler: LuaFunction,
request: HyperRequest<Incoming>,
address: SocketAddr,
) -> LuaResult<HyperResponse<Full<Bytes>>> {
let request = Request::from_incoming(request, true)
.await?
.with_address(address);
let request = tracing::debug_span!("request")
.in_scope(|| async {
let request = Request::from_incoming(request, true)
.await?
.with_address(address);
Ok::<_, LuaError>(request)
})
.await?;
let thread_id = lua.push_thread_back(handler, request)?;
lua.track_thread(thread_id);
lua.wait_for_thread(thread_id).await;
let thread_res = tracing::debug_span!("run")
.in_scope(|| async {
let thread_id = lua.push_thread_back(handler, request)?;
lua.track_thread(thread_id);
lua.wait_for_thread(thread_id).await;
let thread_res = lua
.get_thread_result(thread_id)
.expect("Missing handler thread result")?;
let thread_res = lua
.get_thread_result(thread_id)
.expect("Missing handler thread result")?;
Ok::<_, LuaError>(thread_res)
})
.await?;
let config = ResponseConfig::from_lua_multi(thread_res, &lua)?;
let response = Response::try_from(config)?;
let response = tracing::debug_span!("response").in_scope(|| {
let config = ResponseConfig::from_lua_multi(thread_res, &lua)?;
let response = Response::try_from(config)?;
Ok::<_, LuaError>(response.into_full())
})?;
Ok(response.into_full())
Ok(response)
}
#[tracing::instrument(skip_all)]
async fn handle_websocket(
lua: Lua,
handler: LuaFunction,
request: HyperRequest<Incoming>,
) -> LuaResult<()> {
let upgraded = hyper::upgrade::on(request).await.into_lua_err()?;
let upgraded = tracing::debug_span!("upgrade")
.in_scope(|| async { hyper::upgrade::on(request).await.into_lua_err() })
.await?;
let stream =
WebSocketStream::from_raw_socket(HyperIo::from(upgraded), Role::Server, None).await;
let websocket = Websocket::from(stream);
let stream = tracing::debug_span!("stream")
.in_scope(|| async {
WebSocketStream::from_raw_socket(HyperIo::from(upgraded), Role::Server, None).await
})
.await;
lua.push_thread_back(handler, websocket)?;
tracing::debug_span!("run")
.in_scope(|| async {
let websocket = Websocket::from(stream);
lua.push_thread_back(handler, websocket)
})
.await?;
Ok(())
}