-
-
Notifications
You must be signed in to change notification settings - Fork 165
feat: Add scope listener API #469
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
Draft
timfish
wants to merge
5
commits into
getsentry:master
Choose a base branch
from
timfish:feat/scope-listener
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
743e86f
PoC
timfish 4e9209b
Merge branch 'master' into feat/scope-listener
timfish 9341aee
Allow serialisation of ScopeUpdate
timfish cf3dfe9
Merge branch 'feat/scope-listener' of https://github.com/timfish/sent…
timfish cb7a480
Expose ScopeUpdate with not a care in the world
timfish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,12 +8,24 @@ use crate::protocol::{Attachment, Breadcrumb, Context, Event, Level, User, Value | |||||
use crate::session::Session; | ||||||
use crate::Client; | ||||||
|
||||||
#[non_exhaustive] | ||||||
pub enum ScopeUpdate { | ||||||
AddBreadcrumb(Breadcrumb), | ||||||
ClearBreadcrumbs, | ||||||
User(Option<User>), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
SetExtra(String, Value), | ||||||
RemoveExtra(String), | ||||||
SetTag(String, String), | ||||||
RemoveTag(String), | ||||||
} | ||||||
|
||||||
#[derive(Debug)] | ||||||
pub struct Stack { | ||||||
layers: Vec<StackLayer>, | ||||||
} | ||||||
|
||||||
pub type EventProcessor = Arc<dyn Fn(Event<'static>) -> Option<Event<'static>> + Send + Sync>; | ||||||
pub type ScopeListener = Arc<dyn Fn(&ScopeUpdate) + Send + Sync>; | ||||||
|
||||||
/// Holds contextual data for the current scope. | ||||||
/// | ||||||
|
@@ -44,6 +56,7 @@ pub struct Scope { | |||||
pub(crate) tags: Arc<HashMap<String, String>>, | ||||||
pub(crate) contexts: Arc<HashMap<String, Context>>, | ||||||
pub(crate) event_processors: Arc<Vec<EventProcessor>>, | ||||||
pub(crate) scope_listeners: Arc<Vec<ScopeListener>>, | ||||||
pub(crate) session: Arc<Mutex<Option<Session>>>, | ||||||
pub(crate) span: Arc<Option<TransactionOrSpan>>, | ||||||
pub(crate) attachments: Arc<Vec<Attachment>>, | ||||||
|
@@ -146,6 +159,7 @@ impl Scope { | |||||
|
||||||
/// Deletes current breadcrumbs from the scope. | ||||||
pub fn clear_breadcrumbs(&mut self) { | ||||||
self.notify_scope_listeners(|| ScopeUpdate::ClearBreadcrumbs); | ||||||
self.breadcrumbs = Default::default(); | ||||||
} | ||||||
|
||||||
|
@@ -178,18 +192,21 @@ impl Scope { | |||||
|
||||||
/// Sets the user for the current scope. | ||||||
pub fn set_user(&mut self, user: Option<User>) { | ||||||
self.notify_scope_listeners(|| ScopeUpdate::User(user.clone())); | ||||||
self.user = user.map(Arc::new); | ||||||
} | ||||||
|
||||||
/// Sets a tag to a specific value. | ||||||
pub fn set_tag<V: ToString>(&mut self, key: &str, value: V) { | ||||||
self.notify_scope_listeners(|| ScopeUpdate::SetTag(key.to_string(), value.to_string())); | ||||||
Arc::make_mut(&mut self.tags).insert(key.to_string(), value.to_string()); | ||||||
} | ||||||
|
||||||
/// Removes a tag. | ||||||
/// | ||||||
/// If the tag is not set, does nothing. | ||||||
pub fn remove_tag(&mut self, key: &str) { | ||||||
self.notify_scope_listeners(|| ScopeUpdate::RemoveTag(key.to_string())); | ||||||
Arc::make_mut(&mut self.tags).remove(key); | ||||||
} | ||||||
|
||||||
|
@@ -205,11 +222,13 @@ impl Scope { | |||||
|
||||||
/// Sets a extra to a specific value. | ||||||
pub fn set_extra(&mut self, key: &str, value: Value) { | ||||||
self.notify_scope_listeners(|| ScopeUpdate::SetExtra(key.to_string(), value.clone())); | ||||||
Arc::make_mut(&mut self.extra).insert(key.to_string(), value); | ||||||
} | ||||||
|
||||||
/// Removes a extra. | ||||||
pub fn remove_extra(&mut self, key: &str) { | ||||||
self.notify_scope_listeners(|| ScopeUpdate::RemoveExtra(key.to_string())); | ||||||
Arc::make_mut(&mut self.extra).remove(key); | ||||||
} | ||||||
|
||||||
|
@@ -221,6 +240,14 @@ impl Scope { | |||||
Arc::make_mut(&mut self.event_processors).push(Arc::new(f)); | ||||||
} | ||||||
|
||||||
/// Add an scope listener to the scope. | ||||||
pub fn add_scope_listener<F>(&mut self, f: F) | ||||||
where | ||||||
F: Fn(&ScopeUpdate) + Send + Sync + 'static, | ||||||
{ | ||||||
Arc::make_mut(&mut self.scope_listeners).push(Arc::new(f)); | ||||||
} | ||||||
|
||||||
/// Adds an attachment to the scope | ||||||
pub fn add_attachment(&mut self, attachment: Attachment) { | ||||||
Arc::make_mut(&mut self.attachments).push(attachment); | ||||||
|
@@ -304,4 +331,26 @@ impl Scope { | |||||
session.update_from_event(event); | ||||||
} | ||||||
} | ||||||
|
||||||
pub(crate) fn add_breadcrumb(&mut self, breadcrumb: Breadcrumb, max_breadcrumbs: usize) { | ||||||
self.notify_scope_listeners(|| ScopeUpdate::AddBreadcrumb(breadcrumb.clone())); | ||||||
|
||||||
let breadcrumbs = Arc::make_mut(&mut self.breadcrumbs); | ||||||
breadcrumbs.push_back(breadcrumb); | ||||||
|
||||||
while breadcrumbs.len() > max_breadcrumbs { | ||||||
breadcrumbs.pop_front(); | ||||||
} | ||||||
} | ||||||
|
||||||
fn notify_scope_listeners<F: Fn() -> ScopeUpdate>(&mut self, update_fn: F) { | ||||||
if self.scope_listeners.is_empty() { | ||||||
return; | ||||||
} | ||||||
|
||||||
let update = update_fn(); | ||||||
for listener in self.scope_listeners.as_ref() { | ||||||
listener(&update); | ||||||
} | ||||||
} | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.