Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend/tracing: add fields to span again for error spantrace #567

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion nghe-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ tower-http = { version = "0.6.2", features = [
] }
tracing-error = { version = "0.2.1" }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
typed-path = { git = "https://github.com/vnghia/typed-path", rev = "bd796e64b3cee53181a3fc2f15245f5bf731bd8c" }
typed-path = { version = "0.10.0" }
unicode-normalization = { version = "0.1.24" }
xxhash-rust = { version = "0.8.12", features = ["xxh3"] }

Expand Down
18 changes: 13 additions & 5 deletions nghe-backend/src/config/cover_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ use serde::{Deserialize, Serialize};
use serde_with::formats::SpaceSeparator;
use serde_with::{serde_as, StringWithSeparator};
use typed_path::utils::utf8_temp_dir;
use typed_path::Utf8NativePathBuf;
use typed_path::Utf8PlatformPathBuf;

#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
#[educe(Default)]
pub struct CoverArt {
#[serde(with = "crate::filesystem::path::serde::option")]
#[educe(Default(
expression = Some(utf8_temp_dir().unwrap().join("nghe").join("cache").join("cover_art"))
expression = Some(
utf8_temp_dir()
.unwrap()
.join("nghe")
.join("cache")
.join("cover_art")
.with_platform_encoding_checked()
.unwrap()
)
))]
pub dir: Option<Utf8NativePathBuf>,
pub dir: Option<Utf8PlatformPathBuf>,
#[serde_as(as = "StringWithSeparator::<SpaceSeparator, String>")]
#[educe(Default(expression = vec!["cover.jpg".to_owned(), "cover.png".to_owned()]))]
pub names: Vec<String>,
Expand All @@ -23,13 +31,13 @@ pub struct CoverArt {
#[coverage(off)]
mod test {
use strum::IntoEnumIterator;
use typed_path::Utf8NativePath;
use typed_path::Utf8PlatformPath;

use super::*;
use crate::file::picture;

impl CoverArt {
pub fn with_prefix(self, prefix: impl AsRef<Utf8NativePath>) -> Self {
pub fn with_prefix(self, prefix: impl AsRef<Utf8PlatformPath>) -> Self {
Self {
dir: self.dir.map(|_| prefix.as_ref().join("cache").join("cover_art")),
names: picture::Format::iter().map(picture::Format::name).collect(),
Expand Down
14 changes: 11 additions & 3 deletions nghe-backend/src/config/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use educe::Educe;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use typed_path::utils::utf8_temp_dir;
use typed_path::Utf8NativePathBuf;
use typed_path::Utf8PlatformPathBuf;

#[serde_as]
#[derive(Clone, Serialize, Deserialize, Educe)]
Expand All @@ -14,9 +14,17 @@ pub struct Spotify {
pub secret: Option<String>,
#[serde(with = "crate::filesystem::path::serde::option")]
#[educe(Default(
expression = Some(utf8_temp_dir().unwrap().join("nghe").join("spotify").join("token.json"))
expression = Some(
utf8_temp_dir()
.unwrap()
.join("nghe")
.join("spotify")
.join("token.json")
.with_platform_encoding_checked()
.unwrap()
)
))]
pub token_path: Option<Utf8NativePathBuf>,
pub token_path: Option<Utf8PlatformPathBuf>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
Expand Down
13 changes: 10 additions & 3 deletions nghe-backend/src/config/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::net::{IpAddr, SocketAddr};
use educe::Educe;
use serde::{Deserialize, Serialize};
use typed_path::utils::utf8_current_dir;
use typed_path::Utf8NativePathBuf;
use typed_path::Utf8PlatformPathBuf;

#[derive(Debug, Serialize, Deserialize, Educe)]
#[educe(Default)]
Expand All @@ -13,8 +13,15 @@ pub struct Server {
#[educe(Default(expression = 3000))]
pub port: u16,
#[serde(with = "crate::filesystem::path::serde")]
#[educe(Default(expression = utf8_current_dir().unwrap().join("frontend").join("dist")))]
pub frontend_dir: Utf8NativePathBuf,
#[educe(Default(expression =
utf8_current_dir()
.unwrap()
.join("frontend")
.join("dist")
.with_platform_encoding_checked()
.unwrap()
))]
pub frontend_dir: Utf8PlatformPathBuf,
}

impl Server {
Expand Down
18 changes: 13 additions & 5 deletions nghe-backend/src/config/transcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use educe::Educe;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use typed_path::utils::utf8_temp_dir;
use typed_path::Utf8NativePathBuf;
use typed_path::Utf8PlatformPathBuf;

#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize, Educe)]
Expand All @@ -15,20 +15,28 @@ pub struct Transcode {
pub channel_size: Option<usize>,
#[serde(with = "crate::filesystem::path::serde::option")]
#[educe(Default(
expression = Some(utf8_temp_dir().unwrap().join("nghe").join("cache").join("transcode"))
expression = Some(
utf8_temp_dir()
.unwrap()
.join("nghe")
.join("cache")
.join("transcode")
.with_platform_encoding_checked()
.unwrap()
)
))]
pub cache_dir: Option<Utf8NativePathBuf>,
pub cache_dir: Option<Utf8PlatformPathBuf>,
}

#[cfg(test)]
#[coverage(off)]
mod test {
use typed_path::Utf8NativePath;
use typed_path::Utf8PlatformPath;

use super::*;

impl Transcode {
pub fn with_prefix(self, prefix: impl AsRef<Utf8NativePath>) -> Self {
pub fn with_prefix(self, prefix: impl AsRef<Utf8PlatformPath>) -> Self {
Self {
cache_dir: self.cache_dir.map(|_| prefix.as_ref().join("cache").join("transcode")),
..self
Expand Down
4 changes: 2 additions & 2 deletions nghe-backend/src/file/audio/information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
use diesel::ExpressionMethods;
use diesel_async::RunQueryDsl;
use o2o::o2o;
use typed_path::Utf8NativePath;
use typed_path::Utf8PlatformPath;
use uuid::Uuid;

use super::{Album, Artists, Genres};
Expand Down Expand Up @@ -51,7 +51,7 @@ impl Information<'_> {
pub async fn upsert_cover_art(
&self,
database: &Database,
dir: Option<&impl AsRef<Utf8NativePath>>,
dir: Option<&impl AsRef<Utf8PlatformPath>>,
) -> Result<Option<Uuid>, Error> {
Ok(
if let Some(ref picture) = self.metadata.picture
Expand Down
12 changes: 6 additions & 6 deletions nghe-backend/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::time::Duration;

use axum_extra::headers::{CacheControl, ETag};
use nghe_api::common::format;
use typed_path::{Utf8NativePath, Utf8NativePathBuf};
use typed_path::{Utf8PlatformPath, Utf8PlatformPathBuf};
use xxhash_rust::xxh3::xxh3_64;

use crate::http::binary::property;
Expand Down Expand Up @@ -47,7 +47,7 @@ impl<F: format::Trait> Property<F> {
Property { hash: self.hash, size: self.size, format }
}

fn path_dir(&self, base: impl AsRef<Utf8NativePath>) -> Utf8NativePathBuf {
fn path_dir(&self, base: impl AsRef<Utf8PlatformPath>) -> Utf8PlatformPathBuf {
let hash = self.hash.to_le_bytes();

// Avoid putting too many files in a single directory
Expand All @@ -59,9 +59,9 @@ impl<F: format::Trait> Property<F> {

pub fn path(
&self,
base: impl AsRef<Utf8NativePath>,
base: impl AsRef<Utf8PlatformPath>,
name: impl Into<Option<&str>>,
) -> Utf8NativePathBuf {
) -> Utf8PlatformPathBuf {
let path = self.path_dir(base);
if let Some(name) = name.into() {
path.join(name).with_extension(self.format.extension())
Expand All @@ -72,9 +72,9 @@ impl<F: format::Trait> Property<F> {

pub async fn path_create_dir(
&self,
base: impl AsRef<Utf8NativePath>,
base: impl AsRef<Utf8PlatformPath>,
name: &str,
) -> Result<Utf8NativePathBuf, Error> {
) -> Result<Utf8PlatformPathBuf, Error> {
let path = self.path_dir(base);
tokio::fs::create_dir_all(&path).await?;
Ok(path.join(name).with_extension(self.format.extension()))
Expand Down
8 changes: 4 additions & 4 deletions nghe-backend/src/file/picture/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use lofty::picture::{MimeType, Picture as LoftyPicture};
use nghe_api::common::format;
use o2o::o2o;
use strum::{EnumString, IntoStaticStr};
use typed_path::{Utf8NativePath, Utf8TypedPath, Utf8TypedPathBuf};
use typed_path::{Utf8PlatformPath, Utf8TypedPath, Utf8TypedPathBuf};
use uuid::Uuid;

use super::Property;
Expand Down Expand Up @@ -104,7 +104,7 @@ impl<'s, 'd> Picture<'s, 'd> {
Ok(Self { source, property, data })
}

pub async fn dump(&self, dir: impl AsRef<Utf8NativePath>) -> Result<(), Error> {
pub async fn dump(&self, dir: impl AsRef<Utf8PlatformPath>) -> Result<(), Error> {
let path = self.property.path_create_dir(dir, Self::FILENAME).await?;
tokio::fs::write(path, &self.data).await?;
Ok(())
Expand All @@ -113,7 +113,7 @@ impl<'s, 'd> Picture<'s, 'd> {
pub async fn upsert(
&self,
database: &Database,
dir: impl AsRef<Utf8NativePath>,
dir: impl AsRef<Utf8PlatformPath>,
) -> Result<Uuid, Error> {
// TODO: Checking for its existence before dump.
self.dump(dir).await?;
Expand Down Expand Up @@ -251,7 +251,7 @@ mod test {

impl<'s> Picture<'s, '_> {
async fn load_cache(
dir: impl AsRef<Utf8NativePath>,
dir: impl AsRef<Utf8PlatformPath>,
upsert: cover_arts::Upsert<'s>,
) -> Self {
let property: file::Property<Format> = upsert.property.try_into().unwrap();
Expand Down
12 changes: 7 additions & 5 deletions nghe-backend/src/filesystem/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs::Metadata;
use async_walkdir::WalkDir;
use futures_lite::stream::StreamExt;
use time::OffsetDateTime;
use typed_path::{TryAsRef as _, Utf8NativePath, Utf8TypedPath};
use typed_path::Utf8TypedPath;

use super::{entry, path};
use crate::file::{self, audio};
Expand Down Expand Up @@ -81,10 +81,12 @@ impl super::Trait for Filesystem {
source: &binary::Source<file::Property<audio::Format>>,
offset: Option<u64>,
) -> Result<binary::Response, Error> {
let path = source.path.to_path();
let path: &Utf8NativePath = path
.try_as_ref()
.ok_or_else(|| error::Kind::InvalidTypedPathPlatform(path.to_path_buf()))?;
let path = match source.path.to_path() {
Utf8TypedPath::Unix(path) => path.with_platform_encoding_checked(),
Utf8TypedPath::Windows(path) => path.with_platform_encoding_checked(),
}
.map_err(|_| error::Kind::InvalidTypedPathPlatform(source.path.clone()))?;

binary::Response::from_path_property(
path,
&source.property,
Expand Down
17 changes: 10 additions & 7 deletions nghe-backend/src/filesystem/path/serde.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
use ::serde::{Deserialize, Deserializer, Serializer};
use typed_path::Utf8NativePathBuf;
use typed_path::Utf8PlatformPathBuf;

pub fn serialize<S>(path: &Utf8NativePathBuf, serializer: S) -> Result<S::Ok, S::Error>
pub fn serialize<S>(path: &Utf8PlatformPathBuf, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(path.as_str())
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Utf8NativePathBuf, D::Error>
pub fn deserialize<'de, D>(deserializer: D) -> Result<Utf8PlatformPathBuf, D::Error>
where
D: Deserializer<'de>,
{
<String>::deserialize(deserializer).map(Utf8NativePathBuf::from)
<String>::deserialize(deserializer).map(Utf8PlatformPathBuf::from)
}

pub mod option {
#![allow(clippy::ref_option)]

use super::*;

pub fn serialize<S>(path: &Option<Utf8NativePathBuf>, serializer: S) -> Result<S::Ok, S::Error>
pub fn serialize<S>(
path: &Option<Utf8PlatformPathBuf>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Expand All @@ -31,12 +34,12 @@ pub mod option {
}
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Utf8NativePathBuf>, D::Error>
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Utf8PlatformPathBuf>, D::Error>
where
D: Deserializer<'de>,
{
<String>::deserialize(deserializer).map(|path| {
let path = Utf8NativePathBuf::from(path);
let path = Utf8PlatformPathBuf::from(path);
if path.is_absolute() { Some(path) } else { None }
})
}
Expand Down
6 changes: 3 additions & 3 deletions nghe-backend/src/http/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use nghe_api::common::format;
pub use source::Source;
use tokio::io::{AsyncRead, AsyncSeekExt, SeekFrom};
use tokio_util::io::ReaderStream;
use typed_path::Utf8NativePath;
use typed_path::Utf8PlatformPath;

#[cfg(test)]
use crate::test::transcode;
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Response {
}

pub async fn from_path(
path: impl AsRef<Utf8NativePath>,
path: impl AsRef<Utf8PlatformPath>,
format: impl format::Trait,
offset: impl Into<Option<u64>> + Copy,
#[cfg(test)] transcode_status: impl Into<Option<transcode::Status>>,
Expand All @@ -116,7 +116,7 @@ impl Response {
}

pub async fn from_path_property(
path: impl AsRef<Utf8NativePath>,
path: impl AsRef<Utf8PlatformPath>,
property: &impl property::Trait,
offset: impl Into<Option<u64>> + Copy,
#[cfg(test)] transcode_status: impl Into<Option<transcode::Status>>,
Expand Down
5 changes: 2 additions & 3 deletions nghe-backend/src/integration/informant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use diesel::{ExpressionMethods, QueryDsl, SelectableHelper};
use diesel_async::RunQueryDsl;
use nghe_api::media_annotation::update_artist_information::Request;
use rspotify::model::Id;
use typed_path::Utf8NativePath;
use typed_path::Utf8PlatformPath;
use uuid::Uuid;

use super::spotify;
Expand Down Expand Up @@ -37,7 +37,7 @@ impl Informant {
async fn upsert_artist_picture(
&self,
database: &Database,
dir: Option<&impl AsRef<Utf8NativePath>>,
dir: Option<&impl AsRef<Utf8PlatformPath>>,
source: Option<impl Into<Cow<'_, str>>>,
) -> Result<Option<Uuid>, Error> {
Ok(
Expand Down Expand Up @@ -89,7 +89,6 @@ impl Informant {
None
};

tracing::debug!(?spotify);
self.upsert_artist(database, config, id, spotify.as_ref()).await
}

Expand Down
1 change: 1 addition & 0 deletions nghe-backend/src/integration/spotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl Client {
}
}

#[tracing::instrument(skip_all, name = "spotify:search_artist", ret(level = "debug"))]
pub async fn search_artist(&self, name: &str) -> Result<Option<Artist>, Error> {
Ok(
if let SearchResult::Artists(artists) =
Expand Down
Loading
Loading