From fa31f0e64c07ec41b8bf345d9728de07c41a2732 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Wed, 23 Sep 2020 18:02:05 +0200 Subject: [PATCH 1/5] ref: Deprecate public fields on Integrations --- sentry-anyhow/src/lib.rs | 39 ++++++-------------------- sentry-contexts/src/integration.rs | 22 +++++++++++++++ sentry-debug-images/src/integration.rs | 14 +++++++++ sentry-debug-images/src/lib.rs | 12 ++++---- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/sentry-anyhow/src/lib.rs b/sentry-anyhow/src/lib.rs index 91d6be423..171f9eff3 100644 --- a/sentry-anyhow/src/lib.rs +++ b/sentry-anyhow/src/lib.rs @@ -4,8 +4,11 @@ //! //! ```no_run //! # fn function_that_might_fail() -> anyhow::Result<()> { Ok(()) } -//! use sentry_anyhow::capture_anyhow; +//! use sentry_anyhow::{capture_anyhow, AnyhowIntegration}; //! # fn test() -> anyhow::Result<()> { +//! let _sentry = +//! sentry::init(sentry::ClientOptions::new().add_integration(AnyhowIntegration)); +//! //! let result = match function_that_might_fail() { //! Ok(result) => result, //! Err(err) => { @@ -21,9 +24,6 @@ #![warn(missing_docs)] #![deny(unsafe_code)] -use std::error::Error; -use std::fmt; - use sentry_core::types::Uuid; use sentry_core::{ClientOptions, Hub, Integration}; @@ -32,7 +32,7 @@ use sentry_core::{ClientOptions, Hub, Integration}; pub struct AnyhowIntegration; impl AnyhowIntegration { - /// Creates a new Failure Integration. + /// Creates a new anyhow Integration. pub fn new() -> Self { Self::default() } @@ -57,36 +57,13 @@ pub fn capture_anyhow(e: &anyhow::Error) -> Uuid { /// Hub extension methods for working with `anyhow`. pub trait AnyhowHubExt { - /// Captures an `anyhow::Error` on a specific hub. + /// Captures an [`anyhow::Error`] on a specific hub. fn capture_anyhow(&self, e: &anyhow::Error) -> Uuid; } impl AnyhowHubExt for Hub { fn capture_anyhow(&self, e: &anyhow::Error) -> Uuid { - self.capture_error(&AnyhowError(e)) - } -} - -// `anyhow::Error` itself does not impl `std::error::Error`, because it would -// be incoherent. This can be worked around by wrapping it in a newtype -// which impls `std::error::Error`. -// Code adopted from: https://github.com/dtolnay/anyhow/issues/63#issuecomment-590983511 -struct AnyhowError<'a>(&'a anyhow::Error); - -impl fmt::Debug for AnyhowError<'_> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(fmt) - } -} - -impl fmt::Display for AnyhowError<'_> { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(fmt) - } -} - -impl Error for AnyhowError<'_> { - fn source(&self) -> Option<&(dyn Error + 'static)> { - self.0.source() + let e: &dyn std::error::Error = e.as_ref(); + self.capture_error(e) } } diff --git a/sentry-contexts/src/integration.rs b/sentry-contexts/src/integration.rs index bb3d7420d..7288982ff 100644 --- a/sentry-contexts/src/integration.rs +++ b/sentry-contexts/src/integration.rs @@ -1,3 +1,5 @@ +#![allow(deprecated)] + use std::borrow::Cow; use sentry_core::protocol::map::Entry; @@ -14,10 +16,13 @@ use crate::utils::{device_context, os_context, rust_context, server_name}; #[derive(Debug)] pub struct ContextIntegration { /// Add `os` context, enabled by default. + #[deprecated = "use builder functions instead; direct field access will be removed soon"] pub add_os: bool, /// Add `rust` context, enabled by default. + #[deprecated = "use builder functions instead; direct field access will be removed soon"] pub add_rust: bool, /// Add `device` context, enabled by default. + #[deprecated = "use builder functions instead; direct field access will be removed soon"] pub add_device: bool, } @@ -36,6 +41,23 @@ impl ContextIntegration { pub fn new() -> Self { Self::default() } + + /// Add `os` context, enabled by default. + pub fn add_os(mut self, add_os: bool) -> Self { + self.add_os = add_os; + self + } + /// Add `rust` context, enabled by default. + pub fn add_rust(mut self, add_rust: bool) -> Self { + self.add_rust = add_rust; + self + } + + /// Add `device` context, enabled by default. + pub fn add_device(mut self, add_device: bool) -> Self { + self.add_device = add_device; + self + } } impl Integration for ContextIntegration { diff --git a/sentry-debug-images/src/integration.rs b/sentry-debug-images/src/integration.rs index c2f419091..a2d81ff69 100644 --- a/sentry-debug-images/src/integration.rs +++ b/sentry-debug-images/src/integration.rs @@ -1,3 +1,5 @@ +#![allow(deprecated)] + use std::borrow::Cow; use sentry_core::protocol::{DebugMeta, Event}; @@ -6,6 +8,7 @@ use sentry_core::{ClientOptions, Integration}; /// The Sentry Debug Images Integration. pub struct DebugImagesIntegration { /// A custom filter for which Events should get debug images. + #[deprecated = "use builder functions instead; direct field access will be removed soon"] pub filter: Box) -> bool + Send + Sync>, } @@ -14,6 +17,17 @@ impl DebugImagesIntegration { pub fn new() -> Self { Self::default() } + + /// Sets a custom filter function. + /// + /// The filter specified which Events should get debug images. + pub fn filter(mut self, filter: F) -> Self + where + F: Fn(&Event<'static>) -> bool + Send + Sync + 'static, + { + self.filter = Box::new(filter); + self + } } impl Default for DebugImagesIntegration { diff --git a/sentry-debug-images/src/lib.rs b/sentry-debug-images/src/lib.rs index 929f1258d..b31f9ecc0 100644 --- a/sentry-debug-images/src/lib.rs +++ b/sentry-debug-images/src/lib.rs @@ -1,7 +1,7 @@ //! The Sentry Debug Images Integration. //! -//! The `DebugImagesIntegration` adds metadata about the loaded shared libraries -//! to Sentry `Event`s. +//! The [`DebugImagesIntegration`] adds metadata about the loaded shared libraries +//! to Sentry [`Event`]s. //! //! # Configuration //! @@ -10,11 +10,11 @@ //! //! ``` //! use sentry_core::Level; -//! let integration = sentry_debug_images::DebugImagesIntegration { -//! filter: Box::new(|event| event.level >= Level::Warning), -//! ..Default::default() -//! }; +//! let integration = sentry_debug_images::DebugImagesIntegration::new() +//! .filter(|event| event.level >= Level::Warning); //! ``` +//! +//! [`Event`]: sentry_core::Event #![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")] #![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")] From fe5367c163cb0653a51f58fe316a11eeb7df631a Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Thu, 24 Sep 2020 09:49:27 +0200 Subject: [PATCH 2/5] make fielids private directly --- sentry-anyhow/Cargo.toml | 3 +++ sentry-contexts/src/integration.rs | 14 +++----------- sentry-contexts/src/lib.rs | 7 ++----- sentry-debug-images/src/integration.rs | 8 ++------ sentry-debug-images/src/lib.rs | 11 +++++++---- 5 files changed, 17 insertions(+), 26 deletions(-) diff --git a/sentry-anyhow/Cargo.toml b/sentry-anyhow/Cargo.toml index 3c1a23540..3a598c792 100644 --- a/sentry-anyhow/Cargo.toml +++ b/sentry-anyhow/Cargo.toml @@ -17,3 +17,6 @@ all-features = true [dependencies] sentry-core = { version = "0.20.1", path = "../sentry-core" } anyhow = "1.0.30" + +[dev-dependencies] +sentry = { version = "0.20.1", path = "../sentry", default-features = false, features = ["test"] } diff --git a/sentry-contexts/src/integration.rs b/sentry-contexts/src/integration.rs index 7288982ff..e86eb2502 100644 --- a/sentry-contexts/src/integration.rs +++ b/sentry-contexts/src/integration.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - use std::borrow::Cow; use sentry_core::protocol::map::Entry; @@ -15,15 +13,9 @@ use crate::utils::{device_context, os_context, rust_context, server_name}; /// [Contexts Interface]: https://develop.sentry.dev/sdk/event-payloads/contexts/ #[derive(Debug)] pub struct ContextIntegration { - /// Add `os` context, enabled by default. - #[deprecated = "use builder functions instead; direct field access will be removed soon"] - pub add_os: bool, - /// Add `rust` context, enabled by default. - #[deprecated = "use builder functions instead; direct field access will be removed soon"] - pub add_rust: bool, - /// Add `device` context, enabled by default. - #[deprecated = "use builder functions instead; direct field access will be removed soon"] - pub add_device: bool, + add_os: bool, + add_rust: bool, + add_device: bool, } impl Default for ContextIntegration { diff --git a/sentry-contexts/src/lib.rs b/sentry-contexts/src/lib.rs index 8ea18c287..cbdcd17e4 100644 --- a/sentry-contexts/src/lib.rs +++ b/sentry-contexts/src/lib.rs @@ -9,11 +9,8 @@ //! # Examples //! //! ``` -//! let integration = sentry_contexts::ContextIntegration { -//! add_os: false, -//! ..Default::default() -//! }; -//! let _sentry = sentry::init(sentry::ClientOptions::default().add_integration(integration)); +//! let integration = sentry_contexts::ContextIntegration::new().add_os(false); +//! let _sentry = sentry::init(sentry::ClientOptions::new().add_integration(integration)); //! ``` //! //! [Contexts Interface]: https://develop.sentry.dev/sdk/event-payloads/contexts/ diff --git a/sentry-debug-images/src/integration.rs b/sentry-debug-images/src/integration.rs index a2d81ff69..ce310f676 100644 --- a/sentry-debug-images/src/integration.rs +++ b/sentry-debug-images/src/integration.rs @@ -1,5 +1,3 @@ -#![allow(deprecated)] - use std::borrow::Cow; use sentry_core::protocol::{DebugMeta, Event}; @@ -7,9 +5,7 @@ use sentry_core::{ClientOptions, Integration}; /// The Sentry Debug Images Integration. pub struct DebugImagesIntegration { - /// A custom filter for which Events should get debug images. - #[deprecated = "use builder functions instead; direct field access will be removed soon"] - pub filter: Box) -> bool + Send + Sync>, + filter: Box) -> bool + Send + Sync>, } impl DebugImagesIntegration { @@ -20,7 +16,7 @@ impl DebugImagesIntegration { /// Sets a custom filter function. /// - /// The filter specified which Events should get debug images. + /// The filter specified which [`Event`]s should get debug images. pub fn filter(mut self, filter: F) -> Self where F: Fn(&Event<'static>) -> bool + Send + Sync + 'static, diff --git a/sentry-debug-images/src/lib.rs b/sentry-debug-images/src/lib.rs index b31f9ecc0..4b079b97b 100644 --- a/sentry-debug-images/src/lib.rs +++ b/sentry-debug-images/src/lib.rs @@ -1,12 +1,15 @@ //! The Sentry Debug Images Integration. //! -//! The [`DebugImagesIntegration`] adds metadata about the loaded shared libraries -//! to Sentry [`Event`]s. +//! The [`DebugImagesIntegration`] adds metadata about the loaded shared +//! libraries to Sentry [`Event`]s. +//! +//! This Integration only works on Unix-like OSs right now. Support for Windows +//! will be added in the future. //! //! # Configuration //! -//! The integration by default attaches this information to all Events, but a -//! custom filter can be defined as well. +//! The integration by default attaches this information to all [`Event`]s, but +//! a custom filter can be defined as well. //! //! ``` //! use sentry_core::Level; From 43f2958305c58cb2368546258e1ec4fecc4ad771 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Fri, 25 Sep 2020 10:32:33 +0200 Subject: [PATCH 3/5] fix docs issues --- sentry-core/src/hub.rs | 3 +-- sentry-core/src/lib.rs | 3 ++- sentry-debug-images/src/lib.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sentry-core/src/hub.rs b/sentry-core/src/hub.rs index 5c05ddeb7..9b9d71e20 100644 --- a/sentry-core/src/hub.rs +++ b/sentry-core/src/hub.rs @@ -9,11 +9,10 @@ use std::thread; use std::time::Duration; use crate::protocol::{Breadcrumb, Event, Level, SessionStatus}; -use crate::session::Session; use crate::types::Uuid; use crate::{event_from_error, Integration, IntoBreadcrumbs, Scope, ScopeGuard}; #[cfg(feature = "client")] -use crate::{scope::Stack, Client, Envelope}; +use crate::{scope::Stack, session::Session, Client, Envelope}; #[cfg(feature = "client")] lazy_static::lazy_static! { diff --git a/sentry-core/src/lib.rs b/sentry-core/src/lib.rs index c6aa5c216..9f2d61562 100644 --- a/sentry-core/src/lib.rs +++ b/sentry-core/src/lib.rs @@ -63,7 +63,6 @@ mod hub; mod integration; mod intodsn; mod scope; -mod session; mod transport; // public api or exports from this crate @@ -82,6 +81,8 @@ pub use crate::transport::{Transport, TransportFactory}; #[cfg(feature = "client")] mod client; #[cfg(feature = "client")] +mod session; +#[cfg(feature = "client")] pub use crate::client::Client; // test utilities diff --git a/sentry-debug-images/src/lib.rs b/sentry-debug-images/src/lib.rs index 4b079b97b..715361578 100644 --- a/sentry-debug-images/src/lib.rs +++ b/sentry-debug-images/src/lib.rs @@ -17,7 +17,7 @@ //! .filter(|event| event.level >= Level::Warning); //! ``` //! -//! [`Event`]: sentry_core::Event +//! [`Event`]: sentry_core::protocol::Event #![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")] #![doc(html_logo_url = "https://sentry-brand.storage.googleapis.com/sentry-glyph-black.png")] From ecd3f943d75b742087d8bf3175124d520263208d Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Fri, 2 Oct 2020 14:01:18 +0200 Subject: [PATCH 4/5] review --- sentry-anyhow/src/lib.rs | 18 ++++++++---------- sentry-debug-images/src/integration.rs | 4 ++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/sentry-anyhow/src/lib.rs b/sentry-anyhow/src/lib.rs index 171f9eff3..dff612325 100644 --- a/sentry-anyhow/src/lib.rs +++ b/sentry-anyhow/src/lib.rs @@ -3,20 +3,18 @@ //! # Example //! //! ```no_run -//! # fn function_that_might_fail() -> anyhow::Result<()> { Ok(()) } //! use sentry_anyhow::{capture_anyhow, AnyhowIntegration}; -//! # fn test() -> anyhow::Result<()> { +//! +//! fn function_that_might_fail() -> anyhow::Result<()> { +//! Err(anyhow::anyhow!("some kind of error")) +//! } +//! //! let _sentry = //! sentry::init(sentry::ClientOptions::new().add_integration(AnyhowIntegration)); //! -//! let result = match function_that_might_fail() { -//! Ok(result) => result, -//! Err(err) => { -//! capture_anyhow(&err); -//! return Err(err); -//! } -//! }; -//! # Ok(()) } +//! if let Err(err) = match function_that_might_fail() { +//! capture_anyhow(&err); +//! } //! ``` #![doc(html_favicon_url = "https://sentry-brand.storage.googleapis.com/favicon.ico")] diff --git a/sentry-debug-images/src/integration.rs b/sentry-debug-images/src/integration.rs index ce310f676..6efc8f340 100644 --- a/sentry-debug-images/src/integration.rs +++ b/sentry-debug-images/src/integration.rs @@ -5,7 +5,7 @@ use sentry_core::{ClientOptions, Integration}; /// The Sentry Debug Images Integration. pub struct DebugImagesIntegration { - filter: Box) -> bool + Send + Sync>, + filter: Box) -> bool + Send + Sync>, } impl DebugImagesIntegration { @@ -19,7 +19,7 @@ impl DebugImagesIntegration { /// The filter specified which [`Event`]s should get debug images. pub fn filter(mut self, filter: F) -> Self where - F: Fn(&Event<'static>) -> bool + Send + Sync + 'static, + F: Fn(&Event<'_>) -> bool + Send + Sync + 'static, { self.filter = Box::new(filter); self From bef2906e0efb98e069481e6cea8c61482c1d16c9 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Fri, 2 Oct 2020 14:25:51 +0200 Subject: [PATCH 5/5] fix typo --- sentry-anyhow/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry-anyhow/src/lib.rs b/sentry-anyhow/src/lib.rs index dff612325..140d99f9f 100644 --- a/sentry-anyhow/src/lib.rs +++ b/sentry-anyhow/src/lib.rs @@ -12,7 +12,7 @@ //! let _sentry = //! sentry::init(sentry::ClientOptions::new().add_integration(AnyhowIntegration)); //! -//! if let Err(err) = match function_that_might_fail() { +//! if let Err(err) = function_that_might_fail() { //! capture_anyhow(&err); //! } //! ```