page: Fix tracing span in save_page

Span guards (i.e. `Span::enter`) cannot be used in asynchronous
methods:

> The drop guard returned by Span::enter exits the span when it is
> dropped. When an async function or async block yields at an .await
> point, the current scope is exited, but values in that scope are not
> dropped (because the async block will eventually resume execution
> from that await point). This means that another task will begin
> executing while remaining in the entered span. This results in an
> incorrect trace.

https://docs.rs/tracing/0.1.41/tracing/span/struct.Span.html#in-asynchronous-code

Instead, we can use the `instrument` attribute macro, which correctly
instruments asynchronous functions.  I originally did not use this
macro because it logs all function parameters' values by default, which
means every log line emitted while in the span contained the entire
contents of the page being saved.  Fortunately, I discovered you can
omit certain parameters using `skip`.
This commit is contained in:
2025-04-18 10:03:11 -05:00
parent 9375c5d991
commit 666ae19efd

View File

@@ -9,7 +9,7 @@ use rocket::response::Redirect;
use rocket::serde::json::Json;
use rocket::State;
use serde::Serialize;
use tracing::{debug, error, event, span, Level};
use tracing::{debug, error, event, instrument, Level};
use crate::auth::User;
use crate::config::Config;
@@ -70,6 +70,7 @@ pub async fn post_page(
}
/// Save the page
#[instrument(level = "info", skip(data, ctx, config))]
pub async fn save_page(
url: &str,
data: &str,
@@ -77,8 +78,6 @@ pub async fn save_page(
config: &Config,
user: &str,
) -> Result<Page, Error> {
let span = span!(Level::INFO, "save_page", url = url, user = user);
let _guard = span.enter();
let index_name = &config.meilisearch.index;
debug!("Saving page in Meilisearch index {}", index_name);
let index = ctx.client.get_index(index_name).await?;