Skip to content
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

fix: Propagate User updates to the active Session #292

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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-core/src/scope/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ impl Scope {

/// Sets the user for the current scope.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update this doc comment.

What happens when there is a session? When will the session not see this update?

pub fn set_user(&mut self, user: Option<User>) {
if let Some(session) = self.session.lock().unwrap().as_mut() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a method on a scope is supposed to affect the session?

session.update_user(user.as_ref());
}
self.user = user.map(Arc::new);
}

Expand Down
95 changes: 82 additions & 13 deletions sentry-core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::{Duration, Instant};

use crate::client::TransportArc;
use crate::protocol::{
EnvelopeItem, Event, Level, SessionAttributes, SessionStatus, SessionUpdate,
EnvelopeItem, Event, Level, SessionAttributes, SessionStatus, SessionUpdate, User,
};
use crate::scope::StackLayer;
use crate::types::{Utc, Uuid};
Expand All @@ -35,20 +35,12 @@ impl Session {
pub fn from_stack(stack: &StackLayer) -> Option<Self> {
let client = stack.client.as_ref()?;
let options = client.options();
let user = stack.scope.user.as_ref();
let distinct_id = user
.and_then(|user| {
user.id
.as_ref()
.or_else(|| user.email.as_ref())
.or_else(|| user.username.as_ref())
})
.cloned();
Some(Self {

let mut session = Self {
client: client.clone(),
session_update: SessionUpdate {
session_id: Uuid::new_v4(),
distinct_id,
distinct_id: None,
sequence: None,
timestamp: None,
started: Utc::now(),
Expand All @@ -65,7 +57,10 @@ impl Session {
},
started: Instant::now(),
dirty: true,
})
};
session.update_user(stack.scope.user.as_deref());

Some(session)
}

pub(crate) fn update_from_event(&mut self, event: &Event<'static>) {
Expand Down Expand Up @@ -116,6 +111,21 @@ impl Session {
}
None
}

pub(crate) fn update_user(&mut self, user: Option<&User>) {
if !self.session_update.init {
// once a session update was sent, it becomes immutable
return;
}
self.session_update.distinct_id = user
.and_then(|user| {
user.id
.as_ref()
.or_else(|| user.email.as_ref())
.or_else(|| user.username.as_ref())
})
.cloned();
}
}

// as defined here: https://develop.sentry.dev/sdk/envelopes/#size-limits
Expand Down Expand Up @@ -350,6 +360,65 @@ mod tests {
}
assert_eq!(items.next(), None);
}

#[test]
fn test_session_update_user() {
let envelopes = capture_envelopes(|| {
sentry::start_session();
// capturing this error means the session will be sent out to the
// server, becoming immutable.
let err = "NaN".parse::<usize>().unwrap_err();
sentry::capture_error(&err);

sentry::configure_scope(|scope| {
scope.set_user(Some(sentry::User {
id: Some("foo".into()), // we won’t see this
..Default::default()
}))
});

sentry::start_session();
sentry::configure_scope(|scope| {
scope.set_user(Some(sentry::User {
id: Some("foo-bar".into()),
..Default::default()
}))
});
});
assert_eq!(envelopes.len(), 2);

let mut items = envelopes[0].items();
assert!(matches!(items.next(), Some(EnvelopeItem::Event(_))));
if let Some(EnvelopeItem::SessionUpdate(session)) = items.next() {
assert_eq!(session.status, SessionStatus::Ok);
assert_eq!(session.errors, 1);
assert_eq!(session.distinct_id, None);
assert_eq!(session.init, true);
} else {
panic!("expected session");
}
assert_eq!(items.next(), None);

let mut items = envelopes[1].items();
if let Some(EnvelopeItem::SessionUpdate(session)) = items.next() {
assert_eq!(session.status, SessionStatus::Exited);
assert_eq!(session.errors, 1);
assert_eq!(session.distinct_id, None);
assert_eq!(session.init, false);
} else {
panic!("expected session");
}
if let Some(EnvelopeItem::SessionUpdate(session)) = items.next() {
assert_eq!(session.status, SessionStatus::Exited);
assert_eq!(session.errors, 0);
assert_eq!(session.distinct_id, Some("foo-bar".into()));
assert_eq!(session.init, true);
} else {
panic!("expected session");
}
assert_eq!(items.next(), None);
}

#[test]
fn test_session_sampled_errors() {
let mut envelopes = crate::test::with_captured_envelopes_options(
Expand Down