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 33d3e50
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 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
2 changes: 1 addition & 1 deletion 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 Down

0 comments on commit 33d3e50

Please sign in to comment.