Skip to content

ref: Deprecate public fields on Integrations #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sentry-anyhow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
57 changes: 16 additions & 41 deletions sentry-anyhow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@
//! # Example
//!
//! ```no_run
//! # fn function_that_might_fail() -> anyhow::Result<()> { Ok(()) }
//! use sentry_anyhow::capture_anyhow;
//! # fn test() -> anyhow::Result<()> {
//! let result = match function_that_might_fail() {
//! Ok(result) => result,
//! Err(err) => {
//! capture_anyhow(&err);
//! return Err(err);
//! }
//! };
//! # Ok(()) }
//! use sentry_anyhow::{capture_anyhow, AnyhowIntegration};
//!
//! fn function_that_might_fail() -> anyhow::Result<()> {
//! Err(anyhow::anyhow!("some kind of error"))
//! }
//!
//! let _sentry =
//! sentry::init(sentry::ClientOptions::new().add_integration(AnyhowIntegration));
//!
//! if let Err(err) = function_that_might_fail() {
//! capture_anyhow(&err);
//! }
//! ```

#![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")]
#![warn(missing_docs)]
#![deny(unsafe_code)]

use std::error::Error;
use std::fmt;

use sentry_core::types::Uuid;
use sentry_core::{ClientOptions, Hub, Integration};

Expand All @@ -32,7 +30,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()
}
Expand All @@ -57,36 +55,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)
}
}
26 changes: 20 additions & 6 deletions sentry-contexts/src/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +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.
pub add_os: bool,
/// Add `rust` context, enabled by default.
pub add_rust: bool,
/// Add `device` context, enabled by default.
pub add_device: bool,
add_os: bool,
add_rust: bool,
add_device: bool,
}

impl Default for ContextIntegration {
Expand All @@ -36,6 +33,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 {
Expand Down
7 changes: 2 additions & 5 deletions sentry-contexts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
3 changes: 1 addition & 2 deletions sentry-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
3 changes: 2 additions & 1 deletion sentry-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ mod hub;
mod integration;
mod intodsn;
mod scope;
mod session;
mod transport;

// public api or exports from this crate
Expand All @@ -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
Expand Down
14 changes: 12 additions & 2 deletions sentry-debug-images/src/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ use sentry_core::{ClientOptions, Integration};

/// The Sentry Debug Images Integration.
pub struct DebugImagesIntegration {
/// A custom filter for which Events should get debug images.
pub filter: Box<dyn Fn(&Event<'static>) -> bool + Send + Sync>,
filter: Box<dyn Fn(&Event<'_>) -> bool + Send + Sync>,
}

impl DebugImagesIntegration {
/// Creates a new Debug Images Integration.
pub fn new() -> Self {
Self::default()
}

/// Sets a custom filter function.
///
/// The filter specified which [`Event`]s should get debug images.
pub fn filter<F>(mut self, filter: F) -> Self
where
F: Fn(&Event<'_>) -> bool + Send + Sync + 'static,
{
self.filter = Box::new(filter);
self
}
}

impl Default for DebugImagesIntegration {
Expand Down
19 changes: 11 additions & 8 deletions sentry-debug-images/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
//! 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;
//! 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::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")]
Expand Down