From a6c1108d5b7a72aef95035f86001004fae821ef0 Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Wed, 18 Dec 2024 22:29:10 +0100 Subject: [PATCH] feat: switch registry to tracing logging --- Cargo.lock | 85 ++++++++++--------------------------- registry/Cargo.toml | 8 ++-- registry/src/auth/github.rs | 6 +-- registry/src/error.rs | 3 +- registry/src/main.rs | 42 ++++++++++++------ registry/src/search.rs | 2 +- 6 files changed, 59 insertions(+), 87 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 57760bd..8272274 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1294,19 +1294,6 @@ dependencies = [ "syn 2.0.90", ] -[[package]] -name = "env_logger" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -2614,12 +2601,6 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "1.5.1" @@ -2967,17 +2948,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "is-terminal" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" -dependencies = [ - "hermit-abi 0.4.0", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "is-wsl" version = "0.4.0" @@ -3352,6 +3322,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2195bf6aa996a481483b29d62a7663eed3fe39600c460e323f8ff41e90bdd89b" +[[package]] +name = "mutually_exclusive_features" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e94e1e6445d314f972ff7395df2de295fe51b71821694f0b0e1e79c4f12c8577" + [[package]] name = "native-tls" version = "0.2.12" @@ -3745,9 +3721,7 @@ dependencies = [ "futures", "git2", "gix", - "log", "pesde", - "pretty_env_logger", "reqwest", "rusty-s3", "semver", @@ -3763,7 +3737,9 @@ dependencies = [ "tokio", "tokio-tar", "toml", - "url", + "tracing", + "tracing-actix-web", + "tracing-subscriber", ] [[package]] @@ -3860,16 +3836,6 @@ dependencies = [ "zerocopy", ] -[[package]] -name = "pretty_env_logger" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "865724d4dbe39d9f3dd3b52b88d859d66bcb2d6a0acfd5ea68a65fb66d4bdc1c" -dependencies = [ - "env_logger", - "log", -] - [[package]] name = "proc-macro-crate" version = "3.2.0" @@ -4425,7 +4391,6 @@ dependencies = [ "sentry-contexts", "sentry-core", "sentry-debug-images", - "sentry-log", "sentry-panic", "sentry-tracing", "tokio", @@ -4494,16 +4459,6 @@ dependencies = [ "sentry-core", ] -[[package]] -name = "sentry-log" -version = "0.35.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efcbfbb74628eaef033c1154d4bb082437c7592ce2282c7c5ccb455c4c97a06d" -dependencies = [ - "log", - "sentry-core", -] - [[package]] name = "sentry-panic" version = "0.35.0" @@ -5029,15 +4984,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "thiserror" version = "1.0.69" @@ -5285,6 +5231,19 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-actix-web" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54a9f5c1aca50ebebf074ee665b9f99f2e84906dcf6b993a0d0090edb835166d" +dependencies = [ + "actix-web", + "mutually_exclusive_features", + "pin-project", + "tracing", + "uuid", +] + [[package]] name = "tracing-attributes" version = "0.1.28" diff --git a/registry/Cargo.toml b/registry/Cargo.toml index f73f9b3..9d1cd4b 100644 --- a/registry/Cargo.toml +++ b/registry/Cargo.toml @@ -14,7 +14,6 @@ thiserror = "2.0.7" tantivy = "0.22.0" semver = "1.0.24" chrono = { version = "0.4.39", features = ["serde"] } -url = "2.5.4" futures = "0.3.31" tokio = "1.42.0" tempfile = "3.14.0" @@ -41,10 +40,11 @@ constant_time_eq = "0.3.1" tokio-tar = "0.3.1" async-compression = { version = "0.4.18", features = ["tokio", "gzip"] } -log = "0.4.22" -pretty_env_logger = "0.5.0" +tracing = { version = "0.1.41", features = ["attributes"] } +tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } +tracing-actix-web = "0.7.15" -sentry = { version = "0.35.0", default-features = false, features = ["backtrace", "contexts", "debug-images", "panic", "reqwest", "rustls", "log"] } +sentry = { version = "0.35.0", default-features = false, features = ["backtrace", "contexts", "debug-images", "panic", "reqwest", "rustls"] } sentry-actix = "0.35.0" pesde = { path = "..", features = ["wally-compat"] } diff --git a/registry/src/auth/github.rs b/registry/src/auth/github.rs index fd81c78..09f9c65 100644 --- a/registry/src/auth/github.rs +++ b/registry/src/auth/github.rs @@ -45,7 +45,7 @@ impl AuthImpl for GitHubAuth { return Ok(None); } Err(_) => { - log::error!( + tracing::error!( "failed to get user: {}", response.into_error().await.unwrap_err() ); @@ -53,7 +53,7 @@ impl AuthImpl for GitHubAuth { } }, Err(e) => { - log::error!("failed to get user: {e}"); + tracing::error!("failed to get user: {e}"); return Ok(None); } }; @@ -61,7 +61,7 @@ impl AuthImpl for GitHubAuth { let user_id = match response.json::().await { Ok(resp) => resp.user.id, Err(e) => { - log::error!("failed to get user: {e}"); + tracing::error!("failed to get user: {e}"); return Ok(None); } }; diff --git a/registry/src/error.rs b/registry/src/error.rs index 558d05a..b19fb07 100644 --- a/registry/src/error.rs +++ b/registry/src/error.rs @@ -1,5 +1,4 @@ use actix_web::{body::BoxBody, HttpResponse, ResponseError}; -use log::error; use pesde::source::git_index::errors::{ReadFile, RefreshError, TreeError}; use serde::Serialize; use thiserror::Error; @@ -67,7 +66,7 @@ impl ResponseError for Error { error: format!("archive is invalid: {e}"), }), e => { - log::error!("unhandled error: {e:?}"); + tracing::error!("unhandled error: {e:?}"); HttpResponse::InternalServerError().finish() } } diff --git a/registry/src/main.rs b/registry/src/main.rs index 5f39128..9c39bad 100644 --- a/registry/src/main.rs +++ b/registry/src/main.rs @@ -6,19 +6,22 @@ use crate::{ use actix_cors::Cors; use actix_governor::{Governor, GovernorConfigBuilder}; use actix_web::{ - middleware::{from_fn, Compress, Logger, NormalizePath, TrailingSlash}, + middleware::{from_fn, Compress, NormalizePath, TrailingSlash}, rt::System, web, web::PayloadConfig, App, HttpServer, }; use fs_err::tokio as fs; -use log::info; use pesde::{ source::{pesde::PesdePackageSource, traits::PackageSource}, AuthConfig, Project, }; use std::{env::current_dir, path::PathBuf}; +use tracing::level_filters::LevelFilter; +use tracing_subscriber::{ + fmt::format::FmtSpan, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, +}; mod auth; mod endpoints; @@ -116,12 +119,12 @@ async fn run() -> std::io::Result<()> { let app_data = web::Data::new(AppState { storage: { let storage = get_storage_from_env(); - info!("storage: {storage}"); + tracing::info!("storage: {storage}"); storage }, auth: { let auth = get_auth_from_env(&config); - info!("auth: {auth}"); + tracing::info!("auth: {auth}"); auth }, source: tokio::sync::Mutex::new(source), @@ -140,14 +143,12 @@ async fn run() -> std::io::Result<()> { .finish() .unwrap(); - info!("listening on {address}:{port}"); - HttpServer::new(move || { App::new() .wrap(sentry_actix::Sentry::with_transaction()) .wrap(NormalizePath::new(TrailingSlash::Trim)) .wrap(Cors::permissive()) - .wrap(Logger::default()) + .wrap(tracing_actix_web::TracingLogger::default()) .wrap(Compress::default()) .app_data(app_data.clone()) .route( @@ -200,12 +201,25 @@ async fn run() -> std::io::Result<()> { fn main() -> std::io::Result<()> { let _ = dotenvy::dotenv(); - let mut log_builder = pretty_env_logger::formatted_builder(); - log_builder.parse_env(pretty_env_logger::env_logger::Env::default().default_filter_or("info")); + let tracing_env_filter = EnvFilter::builder() + .with_default_directive(LevelFilter::INFO.into()) + .from_env_lossy() + .add_directive("reqwest=info".parse().unwrap()) + .add_directive("rustls=info".parse().unwrap()) + .add_directive("tokio_util=info".parse().unwrap()) + .add_directive("goblin=info".parse().unwrap()) + .add_directive("tower=info".parse().unwrap()) + .add_directive("hyper=info".parse().unwrap()) + .add_directive("h2=info".parse().unwrap()); - let logger = sentry::integrations::log::SentryLogger::with_dest(log_builder.build()); - log::set_boxed_logger(Box::new(logger)).unwrap(); - log::set_max_level(log::LevelFilter::Info); + tracing_subscriber::registry() + .with(tracing_env_filter) + .with( + tracing_subscriber::fmt::layer() + .compact() + .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE), + ) + .init(); let guard = sentry::init(sentry::ClientOptions { release: sentry::release_name!(), @@ -218,9 +232,9 @@ fn main() -> std::io::Result<()> { if guard.is_enabled() { std::env::set_var("RUST_BACKTRACE", "full"); - info!("sentry initialized"); + tracing::info!("sentry initialized"); } else { - info!("sentry **NOT** initialized"); + tracing::info!("sentry **NOT** initialized"); } System::new().block_on(run()) diff --git a/registry/src/search.rs b/registry/src/search.rs index 0af22ee..bfcbba3 100644 --- a/registry/src/search.rs +++ b/registry/src/search.rs @@ -105,7 +105,7 @@ pub async fn make_search( while let Some((pkg_name, mut file)) = stream.next().await { let Some((_, latest_entry)) = file.pop_last() else { - log::warn!("no versions found for {pkg_name}"); + tracing::error!("no versions found for {pkg_name}"); continue; };