From 19aa5eb52ca3e50ad6918f5f4847a0a1e6be2216 Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Sun, 10 Nov 2024 20:11:10 +0100 Subject: [PATCH] feat(registry): log errors with more details --- registry/src/auth/github.rs | 12 +++++++++--- registry/src/auth/mod.rs | 15 +++++++++++++++ registry/src/endpoints/publish_version.rs | 15 +++++++++++++++ registry/src/error.rs | 18 ++++++++++++++++++ registry/src/main.rs | 9 ++++++++- registry/src/storage/s3.rs | 14 ++++++++++---- 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/registry/src/auth/github.rs b/registry/src/auth/github.rs index 782f7c1..bc66c3b 100644 --- a/registry/src/auth/github.rs +++ b/registry/src/auth/github.rs @@ -1,4 +1,7 @@ -use crate::auth::{get_token_from_req, AuthImpl, UserId}; +use crate::{ + auth::{get_token_from_req, AuthImpl, UserId}, + error::ReqwestErrorExt, +}; use actix_web::{dev::ServiceRequest, Error as ActixError}; use reqwest::StatusCode; use serde::{Deserialize, Serialize}; @@ -41,8 +44,11 @@ impl AuthImpl for GitHubAuth { Err(e) if e.status().is_some_and(|s| s == StatusCode::UNAUTHORIZED) => { return Ok(None); } - Err(e) => { - log::error!("failed to get user: {e}"); + Err(_) => { + log::error!( + "failed to get user: {}", + response.into_error().await.unwrap_err() + ); return Ok(None); } }, diff --git a/registry/src/auth/mod.rs b/registry/src/auth/mod.rs index 2a4d19d..ef2ab53 100644 --- a/registry/src/auth/mod.rs +++ b/registry/src/auth/mod.rs @@ -14,6 +14,7 @@ use actix_web::{ web, HttpMessage, HttpResponse, }; use pesde::source::pesde::IndexConfig; +use sentry::add_breadcrumb; use sha2::{Digest, Sha256}; use std::fmt::Display; @@ -114,6 +115,13 @@ pub async fn write_mw( } }; + add_breadcrumb(sentry::Breadcrumb { + category: Some("auth".into()), + message: Some(format!("write request authorized as {}", user_id.0)), + level: sentry::Level::Info, + ..Default::default() + }); + req.extensions_mut().insert(user_id); next.call(req).await.map(|res| res.map_into_left_body()) @@ -134,6 +142,13 @@ pub async fn read_mw( } }; + add_breadcrumb(sentry::Breadcrumb { + category: Some("auth".into()), + message: Some(format!("read request authorized as {}", user_id.0)), + level: sentry::Level::Info, + ..Default::default() + }); + req.extensions_mut().insert(Some(user_id)); } else { req.extensions_mut().insert(None::); diff --git a/registry/src/endpoints/publish_version.rs b/registry/src/endpoints/publish_version.rs index ddc3b29..b266e5d 100644 --- a/registry/src/endpoints/publish_version.rs +++ b/registry/src/endpoints/publish_version.rs @@ -24,6 +24,7 @@ use pesde::{ }, MANIFEST_FILE_NAME, }; +use sentry::add_breadcrumb; use serde::Deserialize; use sha2::{Digest, Sha256}; use std::{ @@ -268,6 +269,20 @@ pub async fn publish_package( return Err(Error::InvalidArchive); }; + add_breadcrumb(sentry::Breadcrumb { + category: Some("publish".into()), + message: Some(format!( + "publish request for {}@{} {}. has readme: {}. docs: {}", + manifest.name, + manifest.version, + manifest.target, + readme.is_some(), + docs_pages.len() + )), + level: sentry::Level::Info, + ..Default::default() + }); + { let dependencies = manifest .all_dependencies() diff --git a/registry/src/error.rs b/registry/src/error.rs index abd37ec..5f805d6 100644 --- a/registry/src/error.rs +++ b/registry/src/error.rs @@ -15,6 +15,9 @@ pub enum Error { #[error("error deserializing file")] Deserialize(#[from] toml::de::Error), + #[error("failed to send request: {1}\nserver response: {0}")] + ReqwestResponse(String, #[source] reqwest::Error), + #[error("error sending request")] Reqwest(#[from] reqwest::Error), @@ -69,3 +72,18 @@ impl ResponseError for Error { } } } + +pub trait ReqwestErrorExt { + async fn into_error(self) -> Result + where + Self: Sized; +} + +impl ReqwestErrorExt for reqwest::Response { + async fn into_error(self) -> Result { + match self.error_for_status_ref() { + Ok(_) => Ok(self), + Err(e) => Err(Error::ReqwestResponse(self.text().await?, e)), + } + } +} diff --git a/registry/src/main.rs b/registry/src/main.rs index bc60cd9..1a949e0 100644 --- a/registry/src/main.rs +++ b/registry/src/main.rs @@ -144,7 +144,7 @@ async fn run() -> std::io::Result<()> { HttpServer::new(move || { App::new() - .wrap(sentry_actix::Sentry::new()) + .wrap(sentry_actix::Sentry::with_transaction()) .wrap(NormalizePath::new(TrailingSlash::Trim)) .wrap(Cors::permissive()) .wrap(Logger::default()) @@ -205,11 +205,18 @@ fn main() -> std::io::Result<()> { let guard = sentry::init(sentry::ClientOptions { release: sentry::release_name!(), + dsn: benv!(parse "SENTRY_DSN").ok(), + session_mode: sentry::SessionMode::Request, + traces_sample_rate: 1.0, + debug: true, ..Default::default() }); if guard.is_enabled() { std::env::set_var("RUST_BACKTRACE", "full"); + info!("sentry initialized"); + } else { + info!("sentry **NOT** initialized"); } System::new().block_on(run()) diff --git a/registry/src/storage/s3.rs b/registry/src/storage/s3.rs index 41a3424..7cc081f 100644 --- a/registry/src/storage/s3.rs +++ b/registry/src/storage/s3.rs @@ -1,4 +1,7 @@ -use crate::{error::Error, storage::StorageImpl}; +use crate::{ + error::{Error, ReqwestErrorExt}, + storage::StorageImpl, +}; use actix_web::{http::header::LOCATION, HttpResponse}; use pesde::{names::PackageName, source::version_id::VersionId}; use reqwest::header::{CONTENT_ENCODING, CONTENT_TYPE}; @@ -42,7 +45,8 @@ impl StorageImpl for S3Storage { .body(contents) .send() .await? - .error_for_status()?; + .into_error() + .await?; Ok(()) } @@ -92,7 +96,8 @@ impl StorageImpl for S3Storage { .body(contents) .send() .await? - .error_for_status()?; + .into_error() + .await?; Ok(()) } @@ -134,7 +139,8 @@ impl StorageImpl for S3Storage { .body(contents) .send() .await? - .error_for_status()?; + .into_error() + .await?; Ok(()) }