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

feat: Allow ending a session with a different status code #801

Merged
merged 2 commits into from
Feb 2, 2023
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
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 ? "foo@blabla.invalid" : "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