Skip to content

feat: End sessions with explicit status #289

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

Merged
merged 2 commits into from
Nov 11, 2020
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
- The `log` and `slog` integrations were re-designed, they now offer types that wrap a `log::Log` or `slog::Drain` and forward log events to the currently active sentry `Hub` based on an optional filter and an optional mapper.
- The new `log` integration will not implicitly call `log::set_max_level_filter` anymore, and users need to do so manually.

**Features**:

- Add the new `end_session_with_status` global and Hub functions which allow ending a Release Health Session with an explicit `SessionStatus`.

**Deprecations**:

- The `error-chain` and `failure` integration was officially deprecated and will be removed soon.
Expand Down
15 changes: 14 additions & 1 deletion sentry-core/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use sentry_types::protocol::v7::SessionStatus;

use crate::protocol::{Event, Level};
use crate::types::Uuid;
use crate::{Hub, Integration, IntoBreadcrumbs, Scope};
Expand Down Expand Up @@ -283,5 +285,16 @@ pub fn start_session() {

/// End the current Release Health Session.
pub fn end_session() {
Hub::with_active(|hub| hub.end_session())
end_session_with_status(SessionStatus::Exited)
}

/// End the current Release Health Session with the given [`SessionStatus`].
///
/// By default, the SDK will only consider the `Exited` and `Crashed` status
/// based on the type of events that were captured during the session.
///
/// When an `Abnormal` session should be captured, it has to be done explicitly
/// using this function.
pub fn end_session_with_status(status: SessionStatus) {
Hub::with_active(|hub| hub.end_session_with_status(status))
}
6 changes: 0 additions & 6 deletions sentry-core/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,6 @@ impl Client {
self.session_flusher.enqueue(session_update)
}

pub(crate) fn capture_envelope(&self, envelope: Envelope) {
if let Some(ref transport) = *self.transport.read().unwrap() {
transport.send_envelope(envelope);
}
}

/// Drains all pending events and shuts down the transport behind the
/// client. After shutting down the transport is removed.
///
Expand Down
19 changes: 10 additions & 9 deletions sentry-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,21 +331,22 @@ impl Hub {

/// End the current Release Health Session.
///
/// See the global [`end_session`](fn.end_session.html)
/// See the global [`end_session`](crate::end_session_with)
/// for more documentation.
pub fn end_session(&self) {
self.end_session_with_status(SessionStatus::Exited)
}
/// End the current Release Health Session with the given [`SessionStatus`].
///
/// See the global [`end_session_with_status`](crate::end_session_with_status)
/// for more documentation.
pub fn end_session_with_status(&self, status: SessionStatus) {
with_client_impl! {{
self.inner.with_mut(|stack| {
let top = stack.top_mut();
// drop will close and enqueue the session
if let Some(mut session) = top.scope.session.lock().unwrap().take() {
session.close();
if let Some(item) = session.create_envelope_item() {
let mut envelope = Envelope::new();
envelope.add_item(item);
if let Some(ref client) = top.client {
client.capture_envelope(envelope);
}
}
session.close(status);
}
})
}}
Expand Down
27 changes: 24 additions & 3 deletions sentry-core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Session {

impl Drop for Session {
fn drop(&mut self) {
self.close();
self.close(SessionStatus::Exited);
if self.dirty {
self.client.enqueue_session(self.session_update.clone());
}
Expand Down Expand Up @@ -95,10 +95,14 @@ impl Session {
}
}

pub(crate) fn close(&mut self) {
pub(crate) fn close(&mut self, status: SessionStatus) {
if self.session_update.status == SessionStatus::Ok {
let status = match status {
SessionStatus::Ok => SessionStatus::Exited,
s => s,
};
self.session_update.duration = Some(self.started.elapsed().as_secs_f64());
self.session_update.status = SessionStatus::Exited;
self.session_update.status = status;
self.dirty = true;
}
}
Expand Down Expand Up @@ -329,6 +333,23 @@ mod tests {
assert_eq!(items.next(), None);
}

#[test]
fn test_session_abnormal() {
let envelopes = capture_envelopes(|| {
sentry::start_session();
sentry::end_session_with_status(SessionStatus::Abnormal);
});
assert_eq!(envelopes.len(), 1);

let mut items = envelopes[0].items();
if let Some(EnvelopeItem::SessionUpdate(session)) = items.next() {
assert_eq!(session.status, SessionStatus::Abnormal);
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