Skip to content

Commit 7062d8d

Browse files
authored
ref(session): Change session update logic to follow the spec (#477)
1 parent 0112a4e commit 7062d8d

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

Diff for: sentry-core/src/client.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,6 @@ impl Client {
158158
mut event: Event<'static>,
159159
scope: Option<&Scope>,
160160
) -> Option<Event<'static>> {
161-
if let Some(scope) = scope {
162-
scope.update_session_from_event(&event);
163-
}
164-
165-
if !self.sample_should_send() {
166-
return None;
167-
}
168-
169161
// event_id and sdk_info are set before the processors run so that the
170162
// processors can poke around in that data.
171163
if event.event_id.is_nil() {
@@ -211,10 +203,20 @@ impl Client {
211203
if let Some(ref func) = self.options.before_send {
212204
sentry_debug!("invoking before_send callback");
213205
let id = event.event_id;
214-
func(event).or_else(move || {
206+
if let Some(processed_event) = func(event) {
207+
event = processed_event;
208+
} else {
215209
sentry_debug!("before_send dropped event {:?}", id);
216-
None
217-
})
210+
return None;
211+
}
212+
}
213+
214+
if let Some(scope) = scope {
215+
scope.update_session_from_event(&event);
216+
}
217+
218+
if !self.sample_should_send() {
219+
None
218220
} else {
219221
Some(event)
220222
}

Diff for: sentry-core/src/session.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,9 @@ mod tests {
439439
sentry::start_session();
440440
}
441441

442-
// this error will be discarded because of the event processor,
443-
// but the session will still be updated accordingly.
442+
// This error will be discarded because of the event processor,
443+
// and session will not be updated.
444+
// Only events dropped due to sampling should update the session.
444445
let err = "NaN".parse::<usize>().unwrap_err();
445446
sentry::capture_error(&err);
446447
},
@@ -469,11 +470,10 @@ mod tests {
469470

470471
assert_eq!(aggregates[0].distinct_id, None);
471472
assert_eq!(aggregates[0].exited, 50);
472-
assert_eq!(aggregates[1].errored, 1);
473473

474+
assert_eq!(aggregates[1].errored, 0);
474475
assert_eq!(aggregates[1].distinct_id, Some("foo-bar".into()));
475-
assert_eq!(aggregates[1].exited, 49);
476-
assert_eq!(aggregates[1].errored, 1);
476+
assert_eq!(aggregates[1].exited, 50);
477477
} else {
478478
panic!("expected session");
479479
}

0 commit comments

Comments
 (0)