refactor(registry): update sentry api usage

This commit is contained in:
daimond113 2024-10-25 12:07:07 +02:00
parent 50c7b4e542
commit 2e62d07265
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
4 changed files with 22 additions and 34 deletions

View file

@ -2,9 +2,7 @@ FROM rust:1.82-bookworm AS builder
COPY . . COPY . .
WORKDIR /registry RUN cargo build --release -p pesde-registry
RUN cargo build --release
FROM debian:bookworm-slim FROM debian:bookworm-slim

View file

@ -38,4 +38,4 @@ S3_SECRET_KEY= # secret key of the S3 bucket
# FS # FS
FS_STORAGE_ROOT= # root directory of the filesystem storage FS_STORAGE_ROOT= # root directory of the filesystem storage
SENTRY_URL= # optional url of sentry error tracking SENTRY_DSN= # optional DSN of Sentry error tracking

View file

@ -51,9 +51,11 @@ impl ResponseError for Error {
Error::Query(e) => HttpResponse::BadRequest().json(ErrorResponse { Error::Query(e) => HttpResponse::BadRequest().json(ErrorResponse {
error: format!("failed to parse query: {e}"), error: format!("failed to parse query: {e}"),
}), }),
Error::Tar(_) | Error::InvalidArchive => HttpResponse::BadRequest().json(ErrorResponse { Error::Tar(_) | Error::InvalidArchive => {
error: "invalid archive. ensure it has all the required files, and all the dependencies exist in the registry.".to_string(), HttpResponse::BadRequest().json(ErrorResponse {
}), error: "invalid archive".to_string(),
})
}
e => { e => {
log::error!("unhandled error: {e:?}"); log::error!("unhandled error: {e:?}");
HttpResponse::InternalServerError().finish() HttpResponse::InternalServerError().finish()

View file

@ -1,7 +1,7 @@
use actix_cors::Cors; use actix_cors::Cors;
use actix_governor::{Governor, GovernorConfigBuilder}; use actix_governor::{Governor, GovernorConfigBuilder};
use actix_web::{ use actix_web::{
middleware::{from_fn, Compress, Condition, Logger, NormalizePath, TrailingSlash}, middleware::{from_fn, Compress, Logger, NormalizePath, TrailingSlash},
rt::System, rt::System,
web, App, HttpServer, 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 address = benv!("ADDRESS" => "127.0.0.1");
let port: u16 = benv!(parse "PORT" => "8080"); let port: u16 = benv!(parse "PORT" => "8080");
@ -134,7 +134,7 @@ async fn run(with_sentry: bool) -> std::io::Result<()> {
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap(Condition::new(with_sentry, sentry_actix::Sentry::new())) .wrap(sentry_actix::Sentry::new())
.wrap(NormalizePath::new(TrailingSlash::Trim)) .wrap(NormalizePath::new(TrailingSlash::Trim))
.wrap(Cors::permissive()) .wrap(Cors::permissive())
.wrap(Logger::default()) .wrap(Logger::default())
@ -186,33 +186,21 @@ async fn run(with_sentry: bool) -> std::io::Result<()> {
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let _ = dotenvy::dotenv(); 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(); let mut log_builder = pretty_env_logger::formatted_builder();
log_builder.parse_env(pretty_env_logger::env_logger::Env::default().default_filter_or("info")); 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());
let logger = sentry_log::SentryLogger::with_dest(log_builder.build()); log::set_boxed_logger(Box::new(logger)).unwrap();
log::set_boxed_logger(Box::new(logger)).unwrap(); log::set_max_level(log::LevelFilter::Info);
log::set_max_level(log::LevelFilter::Info);
} else { let guard = sentry::init(sentry::ClientOptions {
log_builder.try_init().unwrap(); 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 { System::new().block_on(run())
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))
} }