From 2e62d07265013c6b0994ad8f9b6e627ea1dbcc22 Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Fri, 25 Oct 2024 12:07:07 +0200 Subject: [PATCH] refactor(registry): update sentry api usage --- Dockerfile | 4 +--- registry/.env.example | 2 +- registry/src/error.rs | 8 +++++--- registry/src/main.rs | 42 +++++++++++++++--------------------------- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1c86fd5..9a3bc6f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,9 +2,7 @@ FROM rust:1.82-bookworm AS builder COPY . . -WORKDIR /registry - -RUN cargo build --release +RUN cargo build --release -p pesde-registry FROM debian:bookworm-slim diff --git a/registry/.env.example b/registry/.env.example index dc07e16..b0716b1 100644 --- a/registry/.env.example +++ b/registry/.env.example @@ -38,4 +38,4 @@ S3_SECRET_KEY= # secret key of the S3 bucket # FS FS_STORAGE_ROOT= # root directory of the filesystem storage -SENTRY_URL= # optional url of sentry error tracking \ No newline at end of file +SENTRY_DSN= # optional DSN of Sentry error tracking \ No newline at end of file diff --git a/registry/src/error.rs b/registry/src/error.rs index 1324059..a5e7dcd 100644 --- a/registry/src/error.rs +++ b/registry/src/error.rs @@ -51,9 +51,11 @@ impl ResponseError for Error { Error::Query(e) => HttpResponse::BadRequest().json(ErrorResponse { error: format!("failed to parse query: {e}"), }), - Error::Tar(_) | Error::InvalidArchive => HttpResponse::BadRequest().json(ErrorResponse { - error: "invalid archive. ensure it has all the required files, and all the dependencies exist in the registry.".to_string(), - }), + Error::Tar(_) | Error::InvalidArchive => { + HttpResponse::BadRequest().json(ErrorResponse { + error: "invalid archive".to_string(), + }) + } e => { log::error!("unhandled error: {e:?}"); HttpResponse::InternalServerError().finish() diff --git a/registry/src/main.rs b/registry/src/main.rs index 7ef9984..a4ab4bd 100644 --- a/registry/src/main.rs +++ b/registry/src/main.rs @@ -1,7 +1,7 @@ use actix_cors::Cors; use actix_governor::{Governor, GovernorConfigBuilder}; use actix_web::{ - middleware::{from_fn, Compress, Condition, Logger, NormalizePath, TrailingSlash}, + middleware::{from_fn, Compress, Logger, NormalizePath, TrailingSlash}, rt::System, web, App, HttpServer, }; @@ -80,7 +80,7 @@ macro_rules! benv { }; } -async fn run(with_sentry: bool) -> std::io::Result<()> { +async fn run() -> std::io::Result<()> { let address = benv!("ADDRESS" => "127.0.0.1"); let port: u16 = benv!(parse "PORT" => "8080"); @@ -134,7 +134,7 @@ async fn run(with_sentry: bool) -> std::io::Result<()> { HttpServer::new(move || { App::new() - .wrap(Condition::new(with_sentry, sentry_actix::Sentry::new())) + .wrap(sentry_actix::Sentry::new()) .wrap(NormalizePath::new(TrailingSlash::Trim)) .wrap(Cors::permissive()) .wrap(Logger::default()) @@ -186,33 +186,21 @@ async fn run(with_sentry: bool) -> std::io::Result<()> { fn main() -> std::io::Result<()> { let _ = dotenvy::dotenv(); - let sentry_url = benv!("SENTRY_URL").ok(); - let with_sentry = sentry_url.is_some(); - let mut log_builder = pretty_env_logger::formatted_builder(); log_builder.parse_env(pretty_env_logger::env_logger::Env::default().default_filter_or("info")); - if with_sentry { - let logger = sentry_log::SentryLogger::with_dest(log_builder.build()); - log::set_boxed_logger(Box::new(logger)).unwrap(); - log::set_max_level(log::LevelFilter::Info); - } else { - log_builder.try_init().unwrap(); + let logger = sentry_log::SentryLogger::with_dest(log_builder.build()); + log::set_boxed_logger(Box::new(logger)).unwrap(); + log::set_max_level(log::LevelFilter::Info); + + let guard = sentry::init(sentry::ClientOptions { + release: sentry::release_name!(), + ..Default::default() + }); + + if guard.is_enabled() { + std::env::set_var("RUST_BACKTRACE", "full"); } - let _guard = if let Some(sentry_url) = sentry_url { - std::env::set_var("RUST_BACKTRACE", "full"); - - Some(sentry::init(( - sentry_url, - sentry::ClientOptions { - release: sentry::release_name!(), - ..Default::default() - }, - ))) - } else { - None - }; - - System::new().block_on(run(with_sentry)) + System::new().block_on(run()) }