Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Native/Windows: capture WER report ID and expose as `contexts.wer.report_id` in crash events when the WER integration is enabled. ([#1970](https://github.com/getsentry/sentry-native/pull/1970))
- Add `sentry_set_tags` and `sentry_scope_set_tags` for updating multiple tags with a single scope flush, improving bulk-update performance. ([#1993](https://github.com/getsentry/sentry-native/pull/1993))
- Add `sentry_get_last_event_id` and `sentry_scope_get_last_event_id` for retrieving the last event ID captured with the global or given scope, respectively. ([#1992](https://github.com/getsentry/sentry-native/pull/1992))

**Fixes**:

Expand Down
16 changes: 16 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,22 @@ SENTRY_API sentry_scope_t *sentry_scope_clone(const sentry_scope_t *scope);
*/
SENTRY_API void sentry_scope_clear(sentry_scope_t *scope);

/**
* Returns the ID of the last event sent with the global scope.
*
* Returns a nil UUID if no event has been sent.
*/
SENTRY_API sentry_uuid_t sentry_get_last_event_id(void);

/**
* Returns the ID of the last event sent with `scope`.
*
* Returns a nil UUID if no event has been sent with the scope or if `scope` is
* NULL.
*/
SENTRY_API sentry_uuid_t sentry_scope_get_last_event_id(
const sentry_scope_t *scope);

/**
* Sends a sentry event.
*
Expand Down
4 changes: 2 additions & 2 deletions src/backends/sentry_backend_breakpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,11 @@ breakpad_backend_callback(const google_breakpad::MinidumpDescriptor &descriptor,
= sentry__prepare_transaction(
options, transaction, nullptr);
if (tx_envelope) {
sentry__capture_envelope(
sentry__submit_envelope(
options->transport, tx_envelope, options);
}
}
sentry__capture_envelope(options->transport, envelope, options);
sentry__submit_envelope(options->transport, envelope, options);
} else {
sentry_value_decref(transaction);
}
Expand Down
4 changes: 2 additions & 2 deletions src/backends/sentry_backend_inproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1140,11 +1140,11 @@ process_ucontext_deferred(const sentry_ucontext_t *uctx,
= sentry__prepare_transaction(
options, transaction, NULL);
if (tx_envelope) {
sentry__capture_envelope(
sentry__submit_envelope(
options->transport, tx_envelope, options);
}
}
sentry__capture_envelope(options->transport, envelope, options);
sentry__submit_envelope(options->transport, envelope, options);
} else {
sentry_value_decref(transaction);
}
Expand Down
53 changes: 41 additions & 12 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ sentry_user_consent_is_required(void)
}

void
sentry__capture_envelope(sentry_transport_t *transport,
sentry__submit_envelope(sentry_transport_t *transport,
sentry_envelope_t *envelope, const sentry_options_t *options)
{
if (!sentry__run_should_skip_upload(options->run)) {
Expand All @@ -511,6 +511,20 @@ sentry__capture_envelope(sentry_transport_t *transport,
sentry_envelope_free(envelope);
}

void
sentry__capture_envelope(sentry_transport_t *transport,
sentry_envelope_t *envelope, const sentry_options_t *options)
{
sentry_uuid_t event_id = sentry__envelope_get_event_id(envelope);
if (!sentry_uuid_is_nil(&event_id)) {
SENTRY_WITH_SCOPE_MUT_NO_FLUSH (scope) {
scope->last_event_id = event_id;
}
}

sentry__submit_envelope(transport, envelope, options);
}

void
sentry_capture_envelope(sentry_envelope_t *envelope)
{
Expand Down Expand Up @@ -633,14 +647,21 @@ sentry__capture_event(sentry_value_t event, sentry_scope_t *local_scope)
SENTRY_DATA_CATEGORY_ERROR, 1);
sentry_envelope_free(envelope);
} else {
sentry__capture_envelope(options->transport, envelope, options);
if (local_scope) {
sentry__scope_capture_envelope(
local_scope, options->transport, envelope, options);
} else {
sentry__capture_envelope(
options->transport, envelope, options);
}
was_sent = true;
}
}
}
if (!was_captured) {
sentry_value_decref(event);
}
sentry__scope_free_one_shot(local_scope);
return was_sent ? event_id : sentry_uuid_nil();
}

Expand Down Expand Up @@ -721,7 +742,6 @@ sentry__prepare_event(const sentry_options_t *options, sentry_value_t event,
SENTRY_DEBUG("event was discarded by the `before_send` hook");
sentry__client_report_discard(SENTRY_DISCARD_REASON_BEFORE_SEND,
SENTRY_DATA_CATEGORY_ERROR, 1);
sentry__scope_free_one_shot(local_scope);
return NULL;
}
}
Expand Down Expand Up @@ -750,14 +770,11 @@ sentry__prepare_event(const sentry_options_t *options, sentry_value_t event,
}

sentry__attachments_free(all_attachments);
sentry__scope_free_one_shot(local_scope);

return envelope;

fail:
sentry_envelope_free(envelope);
sentry_value_decref(event);
sentry__scope_free_one_shot(local_scope);
return NULL;
}

Expand Down Expand Up @@ -855,7 +872,6 @@ prepare_user_feedback(const sentry_options_t *options,
"feedback was discarded by the `before_send_feedback` hook");
sentry__client_report_discard(SENTRY_DISCARD_REASON_BEFORE_SEND,
SENTRY_DATA_CATEGORY_FEEDBACK, 1);
sentry__scope_free_one_shot(local_scope);
return NULL;
}
}
Expand Down Expand Up @@ -889,15 +905,12 @@ prepare_user_feedback(const sentry_options_t *options,
}

sentry__attachments_free(all_attachments);
sentry__scope_free_one_shot(local_scope);

return envelope;

fail:
SENTRY_WARN("dropping user feedback");
sentry_envelope_free(envelope);
sentry_value_decref(event);
sentry__scope_free_one_shot(local_scope);
return NULL;
}

Expand Down Expand Up @@ -1856,17 +1869,23 @@ capture_feedback(sentry_value_t user_feedback, sentry_hint_t *hint,
sentry_envelope_t *envelope = prepare_user_feedback(
options, user_feedback, hint, local_scope, &event_id);
if (envelope) {
sentry__capture_envelope(options->transport, envelope, options);
if (local_scope) {
sentry__scope_capture_envelope(
local_scope, options->transport, envelope, options);
} else {
sentry__capture_envelope(options->transport, envelope, options);
}
was_sent = true;
}
}

if (!was_captured) {
// The SDK is not initialized, most likely.
sentry_value_decref(user_feedback);
sentry__scope_free_one_shot(local_scope);
}

sentry__scope_free_one_shot(local_scope);

if (hint) {
sentry__hint_free(hint);
}
Expand Down Expand Up @@ -2201,3 +2220,13 @@ sentry_capture_minidumpw_n(const wchar_t *path, size_t path_len)
return capture_minidump(dump_path);
}
#endif

sentry_uuid_t
sentry_get_last_event_id(void)
{
sentry_uuid_t event_id = sentry_uuid_nil();
SENTRY_WITH_SCOPE (scope) {
event_id = sentry_scope_get_last_event_id(scope);
}
return event_id;
}
10 changes: 10 additions & 0 deletions src/sentry_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ sentry_value_t sentry__transaction_finish_value(
* This function will submit the `envelope` to the given `transport`, first
* checking for consent.
*/
void sentry__submit_envelope(sentry_transport_t *transport,
sentry_envelope_t *envelope, const sentry_options_t *options);

/**
* Captures the `envelope` on the global scope, recording the last sent event
* ID.
*
* Note: This is not safe to call from crash handlers; use
* `sentry__submit_envelope` directly instead.
*/
void sentry__capture_envelope(sentry_transport_t *transport,
sentry_envelope_t *envelope, const sentry_options_t *options);

Expand Down
3 changes: 3 additions & 0 deletions src/sentry_envelope.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ sentry__envelope_from_path(const sentry_path_t *path)
sentry_uuid_t
sentry__envelope_get_event_id(const sentry_envelope_t *envelope)
{
if (!envelope) {
return sentry_uuid_nil();
}
if (envelope->is_raw) {
const char *payload = envelope->contents.raw.payload;
size_t payload_len = envelope->contents.raw.payload_len;
Expand Down
4 changes: 2 additions & 2 deletions src/sentry_envelope.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ sentry_envelope_t *sentry__envelope_from_path(const sentry_path_t *path);

/**
* This returns the UUID of the event associated with this envelope.
* If there is no event inside this envelope, the empty nil UUID will be
* returned.
* If `envelope` is NULL or there is no event inside it, the empty nil UUID will
* be returned.
*/
sentry_uuid_t sentry__envelope_get_event_id(const sentry_envelope_t *envelope);

Expand Down
23 changes: 23 additions & 0 deletions src/sentry_scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
#include "sentry_backend.h"
#include "sentry_core.h"
#include "sentry_database.h"
#include "sentry_envelope.h"
#include "sentry_options.h"
#include "sentry_os.h"
#include "sentry_ringbuffer.h"
#include "sentry_string.h"
#include "sentry_symbolizer.h"
#include "sentry_sync.h"
#include "sentry_tracing.h"
#include "sentry_transport.h"
#include "sentry_value.h"

#include <stdlib.h>
Expand Down Expand Up @@ -83,6 +85,7 @@ init_scope(sentry_scope_t *scope)
scope->breadcrumbs = sentry__ringbuffer_new(SENTRY_BREADCRUMBS_MAX);
scope->dynamic_sampling_context = sentry_value_new_object();
scope->level = SENTRY_LEVEL_ERROR;
scope->last_event_id = sentry_uuid_nil();
scope->client_sdk = sentry_value_new_null();
scope->attachments = NULL;
scope->transaction_object = NULL;
Expand Down Expand Up @@ -413,6 +416,7 @@ sentry_scope_clone(const sentry_scope_t *scope)
sentry_value_freeze(clone->dynamic_sampling_context);
}
clone->level = scope->level;
clone->last_event_id = scope->last_event_id;
clone->client_sdk = sentry__value_clone(scope->client_sdk);
sentry__attachments_extend(&clone->attachments, scope->attachments);

Expand Down Expand Up @@ -1190,3 +1194,22 @@ sentry__scope_apply_to_telemetry(const sentry_scope_t *scope,
"sentry.release");
}
}

sentry_uuid_t
sentry_scope_get_last_event_id(const sentry_scope_t *scope)
{
return scope ? scope->last_event_id : sentry_uuid_nil();
}

void
sentry__scope_capture_envelope(sentry_scope_t *scope,
sentry_transport_t *transport, sentry_envelope_t *envelope,
const sentry_options_t *options)
{
sentry_uuid_t event_id = sentry__envelope_get_event_id(envelope);
if (!sentry_uuid_is_nil(&event_id)) {
scope->last_event_id = event_id;
}

sentry__submit_envelope(transport, envelope, options);
}
8 changes: 8 additions & 0 deletions src/sentry_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct sentry_scope_s {
sentry_ringbuffer_t *breadcrumbs;
sentry_value_t dynamic_sampling_context;
sentry_level_t level;
sentry_uuid_t last_event_id;
sentry_value_t client_sdk;
sentry_attachment_t *attachments;

Expand Down Expand Up @@ -233,6 +234,13 @@ void sentry__scope_freeze_dsc(sentry_scope_t *scope, sentry_value_t incoming);
void sentry__scope_apply_to_telemetry(const sentry_scope_t *scope,
sentry_value_t telemetry, sentry_value_t attributes);

/**
* Captures the `envelope` on `scope`, recording the last sent event ID.
*/
void sentry__scope_capture_envelope(sentry_scope_t *scope,
sentry_transport_t *transport, sentry_envelope_t *envelope,
const sentry_options_t *options);

#endif

// this is only used in unit tests
Expand Down
2 changes: 1 addition & 1 deletion src/session_replay/sentry_session_replay.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ sentry__session_replay_flush_pending(const sentry_options_t *options,
sentry_envelope_t *envelope = build_replay_envelope(
options, meta, mp4_path, end_sec, scope_source);
if (envelope) {
sentry__capture_envelope(transport, envelope, options);
sentry__submit_envelope(transport, envelope, options);
}
sentry_value_decref(meta);

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ SENTRY_TEST(capture_minidump_basic)

const sentry_uuid_t event_id = sentry_capture_minidump(minidump_path->path);
TEST_CHECK(!sentry_uuid_is_nil(&event_id));
sentry_uuid_t last_event_id = sentry_get_last_event_id();
TEST_CHECK(memcmp(&last_event_id, &event_id, sizeof(sentry_uuid_t)) == 0);

sentry__path_free(minidump_path);
sentry__path_free(dir);
Expand All @@ -293,6 +295,8 @@ SENTRY_TEST(capture_minidump_wide)
const sentry_uuid_t event_id
= sentry_capture_minidumpw(minidump_path->path_w);
TEST_CHECK(!sentry_uuid_is_nil(&event_id));
sentry_uuid_t last_event_id = sentry_get_last_event_id();
TEST_CHECK(memcmp(&last_event_id, &event_id, sizeof(sentry_uuid_t)) == 0);

sentry__path_free(minidump_path);
sentry__path_free(dir);
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_envelopes.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ SENTRY_TEST(write_raw_envelope_to_file)

SENTRY_TEST(raw_envelope_event_id)
{
sentry_uuid_t event_id = sentry__envelope_get_event_id(NULL);
TEST_CHECK(sentry_uuid_is_nil(&event_id));

sentry_envelope_t *envelope = create_test_envelope();
const char *test_file_str = SENTRY_TEST_PATH_PREFIX "sentry_test_envelope";
sentry_path_t *test_file_path = sentry__path_from_str(test_file_str);
Expand All @@ -406,7 +409,7 @@ SENTRY_TEST(raw_envelope_event_id)
= sentry__envelope_from_path(test_file_path);
TEST_CHECK(!!raw_envelope);

sentry_uuid_t event_id = sentry__envelope_get_event_id(raw_envelope);
event_id = sentry__envelope_get_event_id(raw_envelope);
char event_id_str[37];
sentry_uuid_as_string(&event_id, event_id_str);
TEST_CHECK_STRING_EQUAL(
Expand Down
Loading
Loading