From a11a10739130d777b3529287414ddec550c52cb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Lup=C4=8D=C3=ADk?= Date: Thu, 2 Feb 2023 13:41:00 +0100 Subject: [PATCH] feat: Allow ending a session with a different status code (#801) --- CHANGELOG.md | 6 ++++++ include/sentry.h | 35 +++++++++++++++++++++++++---------- src/sentry_session.c | 26 ++++++++++++++++++++++---- src/sentry_session.h | 7 ------- tests/unit/test_session.c | 7 +++++-- 5 files changed, 58 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ca80e744..d77bf17dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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**: diff --git a/include/sentry.h b/include/sentry.h index 3e5f16a87..e7610ea1f 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -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. @@ -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 -- */ /** diff --git a/src/sentry_session.c b/src/sentry_session.c index 1aa7dc796..e782d385f 100644 --- a/src/sentry_session.c +++ b/src/sentry_session.c @@ -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) { @@ -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 diff --git a/src/sentry_session.h b/src/sentry_session.h index 5ba036cbd..2a18edb4c 100644 --- a/src/sentry_session.h +++ b/src/sentry_session.h @@ -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. diff --git a/tests/unit/test_session.c b/tests/unit/test_session.c index 59fa8774c..fbe586a02 100644 --- a/tests/unit/test_session.c +++ b/tests/unit/test_session.c @@ -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"); @@ -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 {