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", "pin-project-lite",
"rustls 0.23.26", "rustls 0.23.26",
"rustls-pki-types", "rustls-pki-types",
"tracing",
"url", "url",
"urlencoding", "urlencoding",
"webpki", "webpki",

View file

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

View file

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