Skip to content

Commit

Permalink
chore: rearrange switch guard function
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed Nov 28, 2024
1 parent f537a94 commit bff4693
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
8 changes: 1 addition & 7 deletions sentry-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::sync::{Arc, RwLock};

use crate::hub_impl::SwitchGuard;
// use crate::hub_impl::SwitchGuard;
use crate::protocol::{Event, Level, SessionStatus};
use crate::types::Uuid;
use crate::{Integration, IntoBreadcrumbs, Scope, ScopeGuard};
Expand Down Expand Up @@ -243,10 +243,4 @@ impl Hub {
})
}}
}

#[inline(always)]
/// Consumes self to create switch guard, that can be used to modify current hub in thread local storage
pub fn into_switch_guard(self: Arc<Hub>) -> SwitchGuard {
crate::hub_impl::SwitchGuard::new(self)
}
}
10 changes: 6 additions & 4 deletions sentry-core/src/hub_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ thread_local! {
);
}

///Hub switch guard
///
///Used to temporarily swap active hub in thread local storage.
/// A Hub switch guard used to temporarily swap
/// active hub in thread local storage.
pub struct SwitchGuard {
inner: Option<(Arc<Hub>, bool)>,
}

impl SwitchGuard {
pub(crate) fn new(mut hub: Arc<Hub>) -> Self {
/// Swaps the current thread's Hub by the one provided
/// and returns a guard that, when dropped, replaces it
/// to the previous one.
pub fn new(mut hub: Arc<Hub>) -> Self {
let inner = THREAD_HUB.with(|(thread_hub, is_process_hub)| {
// SAFETY: `thread_hub` will always be a valid thread local hub,
// by definition not shared between threads.
Expand Down
4 changes: 2 additions & 2 deletions sentry-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub use crate::clientoptions::{BeforeCallback, ClientOptions, SessionMode};
pub use crate::error::{capture_error, event_from_error, parse_type_from_debug};
pub use crate::futures::{SentryFuture, SentryFutureExt};
pub use crate::hub::Hub;
pub use crate::hub_impl::SwitchGuard as HubSwitchGuard;
pub use crate::integration::Integration;
pub use crate::intodsn::IntoDsn;
pub use crate::performance::*;
Expand All @@ -150,8 +149,9 @@ pub mod metrics;
mod session;
#[cfg(all(feature = "client", feature = "metrics"))]
mod units;

#[cfg(feature = "client")]
pub use crate::client::Client;
pub use crate::{client::Client, hub_impl::SwitchGuard as HubSwitchGuard};

// test utilities
#[cfg(feature = "test")]
Expand Down
4 changes: 2 additions & 2 deletions sentry-tracing/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ where

let mut extensions = span.extensions_mut();
if let Some(data) = extensions.get_mut::<SentrySpanData>() {
data.hub_switch_guard = Some(data.hub.clone().into_switch_guard());
data.hub_switch_guard = Some(sentry_core::HubSwitchGuard::new(data.hub.clone()));
data.hub.configure_scope(|scope| {
scope.set_span(Some(data.sentry_span.clone()));
})
Expand All @@ -311,7 +311,7 @@ where
data.hub.configure_scope(|scope| {
scope.set_span(data.parent_sentry_span.clone());
});
data.hub_switch_guard = None;
data.hub_switch_guard.take();
}
}

Expand Down

0 comments on commit bff4693

Please sign in to comment.