Skip to content

Commit

Permalink
feat: Allow ending a session with a different status code (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawnCZek authored Feb 2, 2023
1 parent 19df84a commit a11a107
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

**Features**:

- A session may be ended with a different status code. ([#801](https://github.com/getsentry/sentry-native/pull/801))

## 0.5.4

**Fixes**:
Expand Down
35 changes: 25 additions & 10 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1346,16 +1346,6 @@ SENTRY_API void sentry_set_transaction(const char *transaction);
*/
SENTRY_API void sentry_set_level(sentry_level_t level);

/**
* Starts a new session.
*/
SENTRY_API void sentry_start_session(void);

/**
* Ends a session.
*/
SENTRY_API void sentry_end_session(void);

/**
* Sets the maximum number of spans that can be attached to a
* transaction.
Expand Down Expand Up @@ -1384,6 +1374,31 @@ SENTRY_EXPERIMENTAL_API void sentry_options_set_traces_sample_rate(
SENTRY_EXPERIMENTAL_API double sentry_options_get_traces_sample_rate(
sentry_options_t *opts);

/* -- Session APIs -- */

typedef enum {
SENTRY_SESSION_STATUS_OK,
SENTRY_SESSION_STATUS_CRASHED,
SENTRY_SESSION_STATUS_ABNORMAL,
SENTRY_SESSION_STATUS_EXITED,
} sentry_session_status_t;

/**
* Starts a new session.
*/
SENTRY_API void sentry_start_session(void);

/**
* Ends a session.
*/
SENTRY_API void sentry_end_session(void);

/**
* Ends a session with an explicit `status` code.
*/
SENTRY_EXPERIMENTAL_API void sentry_end_session_with_status(
sentry_session_status_t status);

/* -- Performance Monitoring/Tracing APIs -- */

/**
Expand Down
26 changes: 22 additions & 4 deletions src/sentry_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,17 @@ sentry__end_current_session_with_status(sentry_session_status_t status)
return session;
}

static void
sentry__capture_session(sentry_session_t *session)
{
sentry_envelope_t *envelope = sentry__envelope_new();
sentry__envelope_add_session(envelope, session);

SENTRY_WITH_OPTIONS (options) {
sentry__capture_envelope(options->transport, envelope);
}
}

void
sentry_end_session(void)
{
Expand All @@ -273,13 +284,20 @@ sentry_end_session(void)
return;
}

sentry_envelope_t *envelope = sentry__envelope_new();
sentry__envelope_add_session(envelope, session);
sentry__capture_session(session);
sentry__session_free(session);
}

SENTRY_WITH_OPTIONS (options) {
sentry__capture_envelope(options->transport, envelope);
void
sentry_end_session_with_status(sentry_session_status_t status)
{
sentry_session_t *session = sentry__end_current_session_with_status(status);
if (!session) {
return;
}

sentry__capture_session(session);
sentry__session_free(session);
}

void
Expand Down
7 changes: 0 additions & 7 deletions src/sentry_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@

struct sentry_jsonwriter_s;

typedef enum {
SENTRY_SESSION_STATUS_OK,
SENTRY_SESSION_STATUS_CRASHED,
SENTRY_SESSION_STATUS_ABNORMAL,
SENTRY_SESSION_STATUS_EXITED,
} sentry_session_status_t;

/**
* This represents a session, with the number of errors, a status and other
* metadata.
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/test_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ send_envelope(const sentry_envelope_t *envelope, void *data)
SENTRY_VALUE_TYPE_STRING);
TEST_CHECK_STRING_EQUAL(
sentry_value_as_string(sentry_value_get_by_key(session, "status")),
"exited");
*called == 2 ? "crashed" : "exited");
TEST_CHECK_STRING_EQUAL(
sentry_value_as_string(sentry_value_get_by_key(session, "did")),
*called == 1 ? "[email protected]" : "swatinem");
Expand Down Expand Up @@ -83,9 +83,12 @@ SENTRY_TEST(session_basics)
user, "username", sentry_value_new_string("swatinem"));
sentry_set_user(user);

sentry_end_session_with_status(SENTRY_SESSION_STATUS_CRASHED);
sentry_start_session();

sentry_close();

TEST_CHECK_INT_EQUAL(called, 2);
TEST_CHECK_INT_EQUAL(called, 3);
}

typedef struct {
Expand Down

0 comments on commit a11a107

Please sign in to comment.