diff --git a/external/crashpad b/external/crashpad index 6a25bbac1..44d4b1639 160000 --- a/external/crashpad +++ b/external/crashpad @@ -1 +1 @@ -Subproject commit 6a25bbac1cc8c7a49a37599a24d18eea214cff99 +Subproject commit 44d4b1639fb854d10f1c0a71e3f6c701212233a7 diff --git a/src/backends/sentry_backend_crashpad.cpp b/src/backends/sentry_backend_crashpad.cpp index bdfd50a9c..4eb4b04f3 100644 --- a/src/backends/sentry_backend_crashpad.cpp +++ b/src/backends/sentry_backend_crashpad.cpp @@ -128,10 +128,9 @@ typedef struct { crashpad::CrashReportDatabase *db; crashpad::CrashpadClient *client; sentry_path_t *event_path; - sentry_path_t *breadcrumb1_path; - sentry_path_t *breadcrumb2_path; sentry_path_t *external_report_path; - size_t num_breadcrumbs; + size_t max_breadcrumbs; + sentry_scope_observer_t *scope_observer; std::atomic crashed; std::atomic scope_flush; sentry_uuid_t crash_event_id; @@ -249,6 +248,10 @@ flush_external_crash_report( return; } sentry__envelope_set_event_id(envelope, crash_event_id); + if (options->dsn && options->dsn->is_valid) { + sentry__envelope_set_header(envelope, "dsn", + sentry_value_new_string(sentry_options_get_dsn(options))); + } if (options->session) { sentry__envelope_add_session(envelope, options->session); } @@ -261,27 +264,49 @@ flush_external_crash_report( sentry_envelope_free(envelope); } -// This function is necessary for macOS since it has no `FirstChanceHandler`. -// but it is also necessary on Windows if the WER handler is enabled. -// This means we have to continuously flush the scope on -// every change so that `__sentry_event` is ready to upload when the crash -// happens. With platforms that have a `FirstChanceHandler` we can do that -// once in the handler. No need to share event- or crashpad-state mutation. +static bool +begin_scope_flush(crashpad_state_t *data) +{ + if (data->crashed.load(std::memory_order_relaxed)) { + return false; + } + + bool expected = false; + while (!data->scope_flush.compare_exchange_strong( + expected, true, std::memory_order_acquire)) { + if (data->crashed.load(std::memory_order_relaxed)) { + return false; + } + expected = false; + sentry__cpu_relax(); + } + + if (data->crashed.load(std::memory_order_relaxed)) { + data->scope_flush.store(false, std::memory_order_release); + return false; + } + + return true; +} + static void -crashpad_backend_flush_scope( +end_scope_flush(crashpad_state_t *data) +{ + data->scope_flush.store(false, std::memory_order_release); +} + +// This hook seeds __sentry-event after sentry_init() has initialized derived +// scope state. macOS has no FirstChanceHandler, and Windows WER can capture +// outside the normal first-chance path, so the attachment must already contain +// static event data. Mutable scope changes are sent to Crashpad via IPC and +// merged when the report is written. +static void +crashpad_backend_post_init( sentry_backend_t *backend, const sentry_options_t *options) { -#if defined(SENTRY_PLATFORM_LINUX) - (void)backend; - (void)options; -#else auto *data = static_cast(backend->data); - bool expected = false; - // - if (!data->event_path || data->crashed.load(std::memory_order_relaxed) - || !data->scope_flush.compare_exchange_strong( - expected, true, std::memory_order_acquire)) { + if (!data->event_path || data->crashed.load(std::memory_order_relaxed)) { return; } @@ -292,13 +317,53 @@ crashpad_backend_flush_scope( // event fatal. sentry_value_set_by_key( event, "level", sentry__value_new_level(SENTRY_LEVEL_FATAL)); + if (options->release) { + sentry_value_set_by_key( + event, "release", sentry_value_new_string(options->release)); + } + if (options->dist) { + sentry_value_set_by_key( + event, "dist", sentry_value_new_string(options->dist)); + } + if (options->environment) { + sentry_value_set_by_key(event, "environment", + sentry_value_new_string(options->environment)); + } + + // Seed __sentry-event with event data that is not sent as scope updates, + // such as the event ID and option-level metadata. Existing breadcrumbs + // are included so they survive backend reinstall; later mutable scope + // state is sent via IPC and merged when Crashpad writes the report. + SENTRY_WITH_SCOPE (scope) { + // we want the scope without any modules + sentry__scope_apply_to_event( + scope, options, event, SENTRY_SCOPE_BREADCRUMBS); + } + + if (!begin_scope_flush(data)) { + sentry_value_decref(event); + return; + } + + size_t mpack_size; + char *mpack = sentry_value_to_msgpack(event, &mpack_size); + sentry_value_decref(event); + if (!mpack) { + end_scope_flush(data); + return; + } + + int rv = sentry__path_write_buffer(data->event_path, mpack, mpack_size); + sentry_free(mpack); + + if (rv != 0) { + SENTRY_WARN("flushing scope to msgpack failed"); + } + end_scope_flush(data); - flush_scope_to_event(data->event_path, options, event); if (data->external_report_path) { flush_external_crash_report(options, &data->crash_event_id); } - data->scope_flush.store(false, std::memory_order_release); -#endif } #if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_WINDOWS) @@ -308,10 +373,10 @@ flush_scope_from_handler( { auto state = static_cast(options->backend->data); - // this blocks any further calls to `crashpad_backend_flush_scope` + // this blocks any further calls to `send_scope_update` state->crashed.store(true, std::memory_order_relaxed); - // busy-wait until any in-progress scope flushes are finished + // busy-wait until any in-progress scope updates are finished bool expected = false; while (!state->scope_flush.compare_exchange_strong( expected, true, std::memory_order_acquire)) { @@ -538,9 +603,13 @@ report_to_envelope(const crashpad::CrashReportDatabase::Report &report, } } if (envelope) { - sentry_value_set_by_key(event, "breadcrumbs", - sentry__value_merge_breadcrumbs( - breadcrumbs1, breadcrumbs2, options->max_breadcrumbs)); + sentry_value_t breadcrumbs = sentry__value_merge_breadcrumbs( + breadcrumbs1, breadcrumbs2, options->max_breadcrumbs); + if (!sentry_value_is_null(breadcrumbs)) { + sentry_value_set_by_key(event, "breadcrumbs", breadcrumbs); + } else { + sentry_value_decref(breadcrumbs); + } sentry__attachments_add_path(&attachments, minidump_path, SENTRY_ATTACHMENT_TYPE_MINIDUMP, nullptr); @@ -609,6 +678,315 @@ process_completed_reports( } } +#if defined(SENTRY_PLATFORM_WINDOWS) || defined(SENTRY_PLATFORM_LINUX) \ + || defined(SENTRY_PLATFORM_MACOS) + +static void +send_scope_update(crashpad_state_t *data, sentry_value_t update) +{ + if (!data || !data->client + || data->crashed.load(std::memory_order_relaxed)) { + sentry_value_decref(update); + return; + } + + if (!begin_scope_flush(data)) { + sentry_value_decref(update); + return; + } + + size_t mpack_size = 0; + char *mpack = sentry_value_to_msgpack(update, &mpack_size); + sentry_value_decref(update); + if (!mpack) { + end_scope_flush(data); + return; + } + + if (!data->crashed.load(std::memory_order_relaxed)) { + data->client->UpdateScope(std::string(mpack, mpack_size)); + } + sentry_free(mpack); + end_scope_flush(data); +} + +static sentry_value_t +scope_update_new(const char *op, const char *field) +{ + sentry_value_t update = sentry_value_new_object(); + sentry_value_set_by_key(update, "op", sentry_value_new_string(op)); + sentry_value_set_by_key(update, "field", sentry_value_new_string(field)); + return update; +} + +static void +send_scope_value_update(void *state, const char *field, sentry_value_t value) +{ + sentry_value_t update = scope_update_new("set", field); + sentry_value_set_by_key(update, "value", sentry__value_clone(value)); + send_scope_update(static_cast(state), update); +} + +static void +send_scope_string_update( + void *state, const char *field, const char *value, size_t value_len) +{ + sentry_value_t update = scope_update_new("set", field); + sentry_value_set_by_key( + update, "value", sentry_value_new_string_n(value, value_len)); + send_scope_update(static_cast(state), update); +} + +static void +send_scope_item_update(void *state, const char *field, const char *key, + size_t key_len, sentry_value_t value) +{ + sentry_value_t update = scope_update_new("set_item", field); + sentry_value_set_by_key( + update, "key", sentry_value_new_string_n(key, key_len)); + sentry_value_set_by_key(update, "value", sentry__value_clone(value)); + send_scope_update(static_cast(state), update); +} + +static void +send_scope_remove_item_update( + void *state, const char *field, const char *key, size_t key_len) +{ + sentry_value_t update = scope_update_new("remove_item", field); + sentry_value_set_by_key( + update, "key", sentry_value_new_string_n(key, key_len)); + send_scope_update(static_cast(state), update); +} + +static void +set_release(void *state, const char *release, size_t release_len) +{ + send_scope_string_update(state, "release", release, release_len); +} + +static void +set_environment(void *state, const char *environment, size_t environment_len) +{ + send_scope_string_update( + state, "environment", environment, environment_len); +} + +static void +set_transaction(void *state, const char *transaction, size_t transaction_len) +{ + send_scope_string_update( + state, "transaction", transaction, transaction_len); +} + +static void +set_fingerprint(void *state, sentry_value_t fingerprint) +{ + send_scope_value_update(state, "fingerprint", fingerprint); +} + +static void +set_level(void *state, sentry_level_t level) +{ + sentry_value_t update = scope_update_new("set", "level"); + sentry_value_set_by_key(update, "value", sentry__value_new_level(level)); + send_scope_update(static_cast(state), update); +} + +static void +set_user(void *state, sentry_value_t user) +{ + sentry_value_t update = scope_update_new("set", "user"); + sentry_value_t value = sentry__value_clone(user); + if (sentry_value_get_type(value) == SENTRY_VALUE_TYPE_OBJECT + && sentry_value_is_null(sentry_value_get_by_key(value, "id"))) { + SENTRY_WITH_OPTIONS (options) { + if (options->run && options->run->installation_id) { + sentry_value_set_by_key(value, "id", + sentry_value_new_string(options->run->installation_id)); + } + } + } + sentry_value_set_by_key(update, "value", value); + send_scope_update(static_cast(state), update); +} + +static void +set_tag(void *state, const char *key, size_t key_len, const char *value, + size_t value_len) +{ + sentry_value_t update = scope_update_new("set_item", "tags"); + sentry_value_set_by_key( + update, "key", sentry_value_new_string_n(key, key_len)); + sentry_value_set_by_key( + update, "value", sentry_value_new_string_n(value, value_len)); + send_scope_update(static_cast(state), update); +} + +static void +remove_tag(void *state, const char *key, size_t key_len) +{ + send_scope_remove_item_update(state, "tags", key, key_len); +} + +static void +set_extra(void *state, const char *key, size_t key_len, sentry_value_t value) +{ + send_scope_item_update(state, "extra", key, key_len, value); +} + +static void +remove_extra(void *state, const char *key, size_t key_len) +{ + send_scope_remove_item_update(state, "extra", key, key_len); +} + +static void +set_context(void *state, const char *key, size_t key_len, sentry_value_t value) +{ + send_scope_item_update(state, "contexts", key, key_len, value); +} + +static void +remove_context(void *state, const char *key, size_t key_len) +{ + send_scope_remove_item_update(state, "contexts", key, key_len); +} + +static void +set_trace_context(void *state, sentry_value_t trace_context) +{ + sentry_value_t update; + if (sentry_value_is_null(trace_context)) { + update = scope_update_new("remove", "trace_context"); + } else { + update = scope_update_new("set", "trace_context"); + sentry_value_set_by_key( + update, "value", sentry__value_clone(trace_context)); + } + send_scope_update(static_cast(state), update); +} + +static void +add_breadcrumb(void *state, sentry_value_t breadcrumb) +{ + auto *data = static_cast(state); + if (!data->max_breadcrumbs) { + return; + } + + sentry_value_t update = scope_update_new("add_breadcrumb", "breadcrumbs"); + sentry_value_set_by_key(update, "value", sentry__value_clone(breadcrumb)); + sentry_value_set_by_key(update, "max", + sentry_value_new_uint64(static_cast(data->max_breadcrumbs))); + send_scope_update(data, update); +} + +static bool +ensure_unique_path(sentry_attachment_t *attachment) +{ + sentry_path_t *path = nullptr; + SENTRY_WITH_OPTIONS (options) { + path = sentry__path_unique(options->run->run_path, + sentry__path_filename(attachment->filename)); + } + if (!path) { + return false; + } + + sentry__path_free(attachment->path); + attachment->path = path; + return true; +} + +static void +add_attachment(void *state, sentry_attachment_t *attachment) +{ + auto *data = static_cast(state); + if (!data || !data->client) { + return; + } + + if (attachment->buf) { + if (!ensure_unique_path(attachment) + || sentry__path_write_buffer( + attachment->path, attachment->buf, attachment->buf_len) + != 0) { + SENTRY_WARNF("failed to write crashpad attachment \"%s\"", + attachment->path->path); + } + } + + data->client->AddAttachment( + base::FilePath(SENTRY_PATH_PLATFORM_STR(attachment->path))); +} + +static void +remove_attachment(void *state, sentry_attachment_t *attachment) +{ + auto *data = static_cast(state); + if (!data || !data->client) { + return; + } + data->client->RemoveAttachment( + base::FilePath(SENTRY_PATH_PLATFORM_STR(attachment->path))); + + if (attachment->buf && sentry__path_remove(attachment->path) != 0) { + SENTRY_WARNF("failed to remove crashpad attachment \"%s\"", + attachment->path->path); + } +} + +static void +add_scope_observer(crashpad_state_t *data) +{ + if (data->scope_observer) { + return; + } + + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + if (!observer) { + return; + } + + observer->data = data; + observer->set_release = set_release; + observer->set_environment = set_environment; + observer->set_transaction = set_transaction; + observer->set_fingerprint = set_fingerprint; + observer->set_level = set_level; + observer->set_user = set_user; + observer->set_tag = set_tag; + observer->remove_tag = remove_tag; + observer->set_extra = set_extra; + observer->remove_extra = remove_extra; + observer->set_context = set_context; + observer->remove_context = remove_context; + observer->set_trace_context = set_trace_context; + observer->add_breadcrumb = add_breadcrumb; + observer->add_attachment = add_attachment; + observer->remove_attachment = remove_attachment; + + SENTRY_WITH_SCOPE_MUT_NO_FLUSH (scope) { + if (sentry__scope_add_observer(scope, observer)) { + data->scope_observer = observer; + } + } +} + +static void +remove_scope_observer(crashpad_state_t *data) +{ + if (!data->scope_observer) { + return; + } + + SENTRY_WITH_SCOPE_MUT_NO_FLUSH (scope) { + sentry__scope_remove_observer(scope, data->scope_observer); + } + data->scope_observer = nullptr; +} +#endif + static int crashpad_backend_startup( sentry_backend_t *backend, const sentry_options_t *options) @@ -655,6 +1033,7 @@ crashpad_backend_startup( absolute_handler_path->path); sentry_path_t *current_run_folder = options->run->run_path; auto *data = static_cast(backend->data); + data->max_breadcrumbs = options->max_breadcrumbs; // pre-generate event ID for a potential future crash to be able to // associate feedback with the crash event. @@ -672,23 +1051,14 @@ crashpad_backend_startup( attachments.emplace_back(SENTRY_PATH_PLATFORM_STR(attachment->path)); } - // and add the serialized event, and two rotating breadcrumb files - // as attachments and make sure the files exist + // and add the serialized event as an attachment and make sure it exists data->event_path = sentry__path_join_str(current_run_folder, "__sentry-event"); - data->breadcrumb1_path - = sentry__path_join_str(current_run_folder, "__sentry-breadcrumb1"); - data->breadcrumb2_path - = sentry__path_join_str(current_run_folder, "__sentry-breadcrumb2"); sentry__path_touch(data->event_path); - sentry__path_touch(data->breadcrumb1_path); - sentry__path_touch(data->breadcrumb2_path); - attachments.insert(attachments.end(), - { base::FilePath(SENTRY_PATH_PLATFORM_STR(data->event_path)), - base::FilePath(SENTRY_PATH_PLATFORM_STR(data->breadcrumb1_path)), - base::FilePath(SENTRY_PATH_PLATFORM_STR(data->breadcrumb2_path)) }); + attachments.push_back( + base::FilePath(SENTRY_PATH_PLATFORM_STR(data->event_path))); base::FilePath screenshot; if (options->attach_screenshot) { @@ -828,6 +1198,11 @@ crashpad_backend_startup( crashpad::TriState::kEnabled); } +#if defined(SENTRY_PLATFORM_WINDOWS) || defined(SENTRY_PLATFORM_LINUX) \ + || defined(SENTRY_PLATFORM_MACOS) + add_scope_observer(data); +#endif + return 0; } @@ -853,51 +1228,15 @@ crashpad_backend_shutdown(sentry_backend_t *backend) #endif } -static void -crashpad_backend_add_breadcrumb(sentry_backend_t *backend, - sentry_value_t breadcrumb, const sentry_options_t *options) -{ - auto *data = static_cast(backend->data); - - size_t max_breadcrumbs = options->max_breadcrumbs; - if (!max_breadcrumbs) { - return; - } - - bool first_breadcrumb = data->num_breadcrumbs % max_breadcrumbs == 0; - - const sentry_path_t *breadcrumb_file - = data->num_breadcrumbs % (max_breadcrumbs * 2) < max_breadcrumbs - ? data->breadcrumb1_path - : data->breadcrumb2_path; - data->num_breadcrumbs++; - if (!breadcrumb_file) { - return; - } - - size_t mpack_size; - char *mpack = sentry_value_to_msgpack(breadcrumb, &mpack_size); - if (!mpack) { - return; - } - - int rv = first_breadcrumb - ? sentry__path_write_buffer(breadcrumb_file, mpack, mpack_size) - : sentry__path_append_buffer(breadcrumb_file, mpack, mpack_size); - sentry_free(mpack); - - if (rv != 0) { - SENTRY_WARN("flushing breadcrumb to msgpack failed"); - } -} - static void crashpad_backend_free(sentry_backend_t *backend) { auto *data = static_cast(backend->data); +#if defined(SENTRY_PLATFORM_WINDOWS) || defined(SENTRY_PLATFORM_LINUX) \ + || defined(SENTRY_PLATFORM_MACOS) + remove_scope_observer(data); +#endif sentry__path_free(data->event_path); - sentry__path_free(data->breadcrumb1_path); - sentry__path_free(data->breadcrumb2_path); sentry__path_free(data->external_report_path); delete data; } @@ -1016,66 +1355,6 @@ crashpad_backend_prune_database(sentry_backend_t *backend) crashpad::PruneCrashReportDatabase(data->db, &condition); } -#if defined(SENTRY_PLATFORM_WINDOWS) || defined(SENTRY_PLATFORM_LINUX) \ - || defined(SENTRY_PLATFORM_MACOS) -static bool -ensure_unique_path(sentry_attachment_t *attachment) -{ - sentry_path_t *path = nullptr; - SENTRY_WITH_OPTIONS (options) { - path = sentry__path_unique(options->run->run_path, - sentry__path_filename(attachment->filename)); - } - if (!path) { - return false; - } - - sentry__path_free(attachment->path); - attachment->path = path; - return true; -} - -static void -crashpad_backend_add_attachment( - sentry_backend_t *backend, sentry_attachment_t *attachment) -{ - auto *data = static_cast(backend->data); - if (!data || !data->client) { - return; - } - - if (attachment->buf) { - if (!ensure_unique_path(attachment) - || sentry__path_write_buffer( - attachment->path, attachment->buf, attachment->buf_len) - != 0) { - SENTRY_WARNF("failed to write crashpad attachment \"%s\"", - attachment->path->path); - } - } - - data->client->AddAttachment( - base::FilePath(SENTRY_PATH_PLATFORM_STR(attachment->path))); -} - -static void -crashpad_backend_remove_attachment( - sentry_backend_t *backend, sentry_attachment_t *attachment) -{ - auto *data = static_cast(backend->data); - if (!data || !data->client) { - return; - } - data->client->RemoveAttachment( - base::FilePath(SENTRY_PATH_PLATFORM_STR(attachment->path))); - - if (attachment->buf && sentry__path_remove(attachment->path) != 0) { - SENTRY_WARNF("failed to remove crashpad attachment \"%s\"", - attachment->path->path); - } -} -#endif - void sentry__backend_preload(void) { @@ -1098,19 +1377,13 @@ sentry__backend_new(void) data->crashed = false; backend->startup_func = crashpad_backend_startup; + backend->post_init_func = crashpad_backend_post_init; backend->shutdown_func = crashpad_backend_shutdown; backend->except_func = crashpad_backend_except; backend->free_func = crashpad_backend_free; - backend->flush_scope_func = crashpad_backend_flush_scope; - backend->add_breadcrumb_func = crashpad_backend_add_breadcrumb; backend->user_consent_changed_func = crashpad_backend_user_consent_changed; backend->get_last_crash_func = crashpad_backend_last_crash; backend->prune_database_func = crashpad_backend_prune_database; -#if defined(SENTRY_PLATFORM_WINDOWS) || defined(SENTRY_PLATFORM_LINUX) \ - || defined(SENTRY_PLATFORM_MACOS) - backend->add_attachment_func = crashpad_backend_add_attachment; - backend->remove_attachment_func = crashpad_backend_remove_attachment; -#endif backend->data = data; backend->can_capture_after_shutdown = true; diff --git a/src/sentry_attachment.c b/src/sentry_attachment.c index 57d11bb4b..c337a02f8 100644 --- a/src/sentry_attachment.c +++ b/src/sentry_attachment.c @@ -262,12 +262,12 @@ sentry__attachments_add_path(sentry_attachment_t **attachments_ptr, return sentry__attachments_add(attachments_ptr, attachment); } -void +bool sentry__attachments_remove( sentry_attachment_t **attachments_ptr, sentry_attachment_t *attachment) { if (!attachment) { - return; + return false; } sentry_attachment_t **next_ptr = attachments_ptr; @@ -275,12 +275,14 @@ sentry__attachments_remove( for (sentry_attachment_t *it = *attachments_ptr; it; it = it->next) { if (it == attachment) { *next_ptr = it->next; - sentry__attachment_free(it); - return; + it->next = NULL; + return true; } next_ptr = &it->next; } + + return false; } static sentry_attachment_t * diff --git a/src/sentry_attachment.h b/src/sentry_attachment.h index 0004164e7..0ddf96f35 100644 --- a/src/sentry_attachment.h +++ b/src/sentry_attachment.h @@ -90,8 +90,9 @@ sentry_attachment_t *sentry__attachments_add_path( /** * Removes an attachment from the attachments list at `attachments_ptr`. + * Returns true if the attachment was found and removed. */ -void sentry__attachments_remove( +bool sentry__attachments_remove( sentry_attachment_t **attachments_ptr, sentry_attachment_t *attachment); /** diff --git a/src/sentry_backend.h b/src/sentry_backend.h index a55fc507a..1ec3f0173 100644 --- a/src/sentry_backend.h +++ b/src/sentry_backend.h @@ -13,6 +13,7 @@ */ struct sentry_backend_s { int (*startup_func)(sentry_backend_t *, const sentry_options_t *options); + void (*post_init_func)(sentry_backend_t *, const sentry_options_t *options); void (*shutdown_func)(sentry_backend_t *); void (*free_func)(sentry_backend_t *); void (*except_func)(sentry_backend_t *, const struct sentry_ucontext_s *); diff --git a/src/sentry_core.c b/src/sentry_core.c index e169f7308..a3fd43069 100644 --- a/src/sentry_core.c +++ b/src/sentry_core.c @@ -206,10 +206,9 @@ sentry_init(sentry_options_t *options) g_last_crash = sentry__has_crash_marker(options); g_options = options; - // *after* setting the global options, trigger a scope and consent flush, - // since at least crashpad needs that. At this point we also freeze the - // `client_sdk` in the `scope` because some downstream SDKs want to override - // it at runtime via the options interface. + // *after* setting the global options, initialize derived scope state. At + // this point we also freeze the `client_sdk` in the `scope` because some + // downstream SDKs want to override it at runtime via the options interface. SENTRY_WITH_SCOPE_MUT (scope) { if (options->sdk_name) { sentry_value_t sdk_name @@ -228,6 +227,9 @@ sentry_init(sentry_options_t *options) sentry__scope_update_dsc(scope, options); } + if (backend && backend->post_init_func) { + backend->post_init_func(backend, options); + } if (backend && backend->user_consent_changed_func) { backend->user_consent_changed_func(backend); } @@ -392,6 +394,8 @@ sentry_reinstall_backend(void) if (backend && backend->startup_func) { if (backend->startup_func(backend, options)) { rv = 1; + } else if (backend->post_init_func) { + backend->post_init_func(backend, options); } } } @@ -899,6 +903,7 @@ sentry_set_release_n(const char *release, size_t release_len) scope->release = sentry__string_clone_n(release, release_len); sentry_value_set_by_key(scope->dynamic_sampling_context, "release", sentry_value_new_string(scope->release)); + SENTRY_SCOPE_NOTIFY(scope, set_release, release, release_len); } } @@ -917,6 +922,8 @@ sentry_set_environment_n(const char *environment, size_t environment_len) = sentry__string_clone_n(environment, environment_len); sentry_value_set_by_key(scope->dynamic_sampling_context, "environment", sentry_value_new_string(scope->environment)); + SENTRY_SCOPE_NOTIFY( + scope, set_environment, environment, environment_len); } } @@ -981,9 +988,7 @@ sentry_set_tag_n( void sentry_remove_tag(const char *key) { - SENTRY_WITH_SCOPE_MUT (scope) { - sentry_value_remove_by_key(scope->tags, key); - } + sentry_remove_tag_n(key, sentry__guarded_strlen(key)); } void @@ -991,6 +996,7 @@ sentry_remove_tag_n(const char *key, size_t key_len) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key_n(scope->tags, key, key_len); + SENTRY_SCOPE_NOTIFY(scope, remove_tag, key, key_len); } } @@ -1015,6 +1021,8 @@ sentry_remove_extra(const char *key) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key(scope->extra, key); + SENTRY_SCOPE_NOTIFY( + scope, remove_extra, key, sentry__guarded_strlen(key)); } } @@ -1023,6 +1031,7 @@ sentry_remove_extra_n(const char *key, size_t key_len) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key_n(scope->extra, key, key_len); + SENTRY_SCOPE_NOTIFY(scope, remove_extra, key, key_len); } } @@ -1096,6 +1105,8 @@ sentry__set_propagation_context(const char *key, sentry_value_t value) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_set_by_key(scope->propagation_context, key, value); + SENTRY_SCOPE_NOTIFY( + scope, set_context, key, sentry__guarded_strlen(key), value); } } @@ -1130,6 +1141,8 @@ sentry_remove_context(const char *key) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key(scope->contexts, key); + SENTRY_SCOPE_NOTIFY( + scope, remove_context, key, sentry__guarded_strlen(key)); } } @@ -1138,6 +1151,7 @@ sentry_remove_context_n(const char *key, size_t key_len) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key_n(scope->contexts, key, key_len); + SENTRY_SCOPE_NOTIFY(scope, remove_context, key, key_len); } } @@ -1174,6 +1188,7 @@ sentry_remove_fingerprint(void) SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_decref(scope->fingerprint); scope->fingerprint = sentry_value_new_null(); + SENTRY_SCOPE_NOTIFY(scope, set_fingerprint, scope->fingerprint); } } @@ -1228,6 +1243,9 @@ sentry_regenerate_trace(void) generate_propagation_context(scope->propagation_context); scope->trace_managed = false; sentry__scope_update_dsc(scope, options); + SENTRY_SCOPE_NOTIFY(scope, set_context, "trace", + sentry__guarded_strlen("trace"), + sentry_value_get_by_key(scope->propagation_context, "trace")); } } } @@ -1242,6 +1260,8 @@ sentry_set_transaction(const char *transaction) if (scope->transaction_object) { sentry_transaction_set_name(scope->transaction_object, transaction); } + SENTRY_SCOPE_NOTIFY(scope, set_transaction, transaction, + sentry__guarded_strlen(transaction)); } } @@ -1257,6 +1277,8 @@ sentry_set_transaction_n(const char *transaction, size_t transaction_len) sentry_transaction_set_name_n( scope->transaction_object, transaction, transaction_len); } + SENTRY_SCOPE_NOTIFY( + scope, set_transaction, transaction, transaction_len); } } @@ -1329,6 +1351,10 @@ sentry_transaction_start_ts(sentry_transaction_context_t *opaque_tx_ctx, sentry_value_remove_by_key(tx, "parent_span_id"); sentry_value_remove_by_key(tx, "sampled"); sentry__scope_update_dsc(scope, options); + SENTRY_SCOPE_NOTIFY(scope, set_context, "trace", + sentry__guarded_strlen("trace"), + sentry_value_get_by_key( + scope->propagation_context, "trace")); } } } @@ -1399,6 +1425,9 @@ sentry__transaction_finish_value( if (sentry__string_eq(tx_id, scope_tx_id)) { sentry__transaction_decref(scope->transaction_object); scope->transaction_object = NULL; + if (!scope->trace_managed) { + sentry__scope_notify_trace_context(scope); + } } } // if the SDK manages the trace (rather than the user or a downstream @@ -1411,6 +1440,7 @@ sentry__transaction_finish_value( sentry_value_set_by_key( sentry_value_get_by_key(scope->propagation_context, "trace"), "trace_id", txn_trace_id); + sentry__scope_notify_trace_context(scope); } } // The sampling decision should already be made for transactions @@ -1475,6 +1505,7 @@ sentry_set_transaction_object(sentry_transaction_t *tx) sentry__transaction_decref(scope->transaction_object); sentry__transaction_incref(tx); scope->transaction_object = tx; + sentry__scope_notify_trace_context(scope); } } @@ -1487,6 +1518,7 @@ sentry_set_span(sentry_span_t *span) sentry__span_decref(scope->span); sentry__span_incref(span); scope->span = span; + sentry__scope_notify_trace_context(scope); } } @@ -1648,6 +1680,7 @@ sentry_span_finish_ts(sentry_span_t *opaque_span, uint64_t timestamp) if (sentry__string_eq(span_id, scope_span_id)) { sentry__span_decref(scope->span); scope->span = NULL; + sentry__scope_notify_trace_context(scope); } } } @@ -1903,13 +1936,17 @@ sentry_capture_minidump_n(const char *path, size_t path_len) static sentry_attachment_t * add_attachment(sentry_attachment_t *attachment) { + if (!attachment) { + return NULL; + } + SENTRY_WITH_OPTIONS (options) { if (options->backend && options->backend->add_attachment_func) { options->backend->add_attachment_func(options->backend, attachment); } } SENTRY_WITH_SCOPE_MUT (scope) { - attachment = sentry__attachments_add(&scope->attachments, attachment); + attachment = sentry__scope_add_attachment(scope, attachment); } return attachment; } @@ -1947,15 +1984,17 @@ sentry_clear_attachments(void) { SENTRY_WITH_OPTIONS (options) { SENTRY_WITH_SCOPE_MUT (scope) { - if (options->backend && options->backend->remove_attachment_func) { - for (sentry_attachment_t *it = scope->attachments; it; - it = it->next) { + sentry_attachment_t *attachments = scope->attachments; + scope->attachments = NULL; + for (sentry_attachment_t *it = attachments; it; it = it->next) { + if (options->backend + && options->backend->remove_attachment_func) { options->backend->remove_attachment_func( options->backend, it); } + SENTRY_SCOPE_NOTIFY(scope, remove_attachment, it); } - sentry__attachments_free(scope->attachments); - scope->attachments = NULL; + sentry__attachments_free(attachments); } } } @@ -1963,15 +2002,23 @@ sentry_clear_attachments(void) void sentry_remove_attachment(sentry_attachment_t *attachment) { + if (!attachment) { + return; + } + SENTRY_WITH_OPTIONS (options) { - if (options->backend && options->backend->remove_attachment_func) { - options->backend->remove_attachment_func( - options->backend, attachment); + SENTRY_WITH_SCOPE_MUT (scope) { + if (sentry__attachments_remove(&scope->attachments, attachment)) { + if (options->backend + && options->backend->remove_attachment_func) { + options->backend->remove_attachment_func( + options->backend, attachment); + } + SENTRY_SCOPE_NOTIFY(scope, remove_attachment, attachment); + sentry__attachment_free(attachment); + } } } - SENTRY_WITH_SCOPE_MUT (scope) { - sentry__attachments_remove(&scope->attachments, attachment); - } } #ifdef SENTRY_PLATFORM_WINDOWS diff --git a/src/sentry_scope.c b/src/sentry_scope.c index 1a6f54a55..b197455ba 100644 --- a/src/sentry_scope.c +++ b/src/sentry_scope.c @@ -88,6 +88,9 @@ init_scope(sentry_scope_t *scope) scope->transaction_object = NULL; scope->span = NULL; scope->trace_managed = true; + scope->observers = NULL; + scope->num_observers = 0; + scope->is_notifying = 0; } static sentry_scope_t * @@ -127,6 +130,12 @@ cleanup_scope(sentry_scope_t *scope) sentry__attachments_free(scope->attachments); sentry__transaction_decref(scope->transaction_object); sentry__span_decref(scope->span); + for (size_t i = 0; i < scope->num_observers; i++) { + sentry_free(scope->observers[i]); + } + sentry_free(scope->observers); + scope->observers = NULL; + scope->num_observers = 0; } void @@ -159,7 +168,11 @@ sentry__scope_unlock(void) void sentry__scope_flush_unlock(void) { + bool was_notifying = g_scope.is_notifying > 0; sentry__scope_unlock(); + if (was_notifying) { + return; + } SENTRY_WITH_OPTIONS (options) { // we try to unlock the scope as soon as possible. The // backend will do its own `WITH_SCOPE` internally. @@ -169,6 +182,101 @@ sentry__scope_flush_unlock(void) } } +sentry_scope_observer_t * +sentry__scope_observer_new(void) +{ + return SENTRY_MAKE(sentry_scope_observer_t); +} + +bool +sentry__scope_add_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer) +{ + if (!observer) { + return false; + } + + size_t new_count = scope->num_observers + 1; + sentry_scope_observer_t **new_array + = sentry__calloc(new_count, sizeof(sentry_scope_observer_t *)); + if (!new_array) { + sentry_free(observer); + return false; + } + if (scope->observers) { + memcpy(new_array, scope->observers, + scope->num_observers * sizeof(sentry_scope_observer_t *)); + sentry_free(scope->observers); + } + new_array[scope->num_observers] = observer; + scope->observers = new_array; + scope->num_observers = new_count; + return true; +} + +void +sentry__scope_remove_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer) +{ + if (!observer || !scope->observers) { + return; + } + + for (size_t i = 0; i < scope->num_observers; i++) { + if (scope->observers[i] != observer) { + continue; + } + + sentry_free(observer); + if (scope->is_notifying) { + // avoid shifting the array while SENTRY_SCOPE_NOTIFY is iterating + scope->observers[i] = NULL; + return; + } + for (size_t j = i + 1; j < scope->num_observers; j++) { + scope->observers[j - 1] = scope->observers[j]; + } + scope->num_observers--; + if (scope->num_observers == 0) { + sentry_free(scope->observers); + scope->observers = NULL; + } + return; + } +} + +size_t +sentry__scope_begin_notify(sentry_scope_t *scope) +{ + scope->is_notifying++; + return scope->num_observers; +} + +void +sentry__scope_end_notify(sentry_scope_t *scope) +{ + if (--scope->is_notifying > 0) { + return; + } + if (!scope->observers) { + return; + } + + // removals during notification leave tombstones to avoid shifting the array + // while it is being iterated + size_t j = 0; + for (size_t i = 0; i < scope->num_observers; i++) { + if (scope->observers[i]) { + scope->observers[j++] = scope->observers[i]; + } + } + scope->num_observers = j; + if (scope->num_observers == 0) { + sentry_free(scope->observers); + scope->observers = NULL; + } +} + sentry_scope_t * sentry_local_scope_new(void) { @@ -350,6 +458,39 @@ get_span_or_transaction(const sentry_scope_t *scope) } } +static sentry_value_t +get_scope_trace_context(const sentry_scope_t *scope) +{ + sentry_value_t scoped_txn_or_span = get_span_or_transaction(scope); + sentry_value_t trace_context + = sentry__value_get_trace_context(scoped_txn_or_span); + if (sentry_value_is_null(trace_context)) { + return trace_context; + } + + sentry_value_t data = sentry_value_get_by_key(scoped_txn_or_span, "data"); + if (!sentry_value_is_null(data)) { + sentry_value_incref(data); + sentry_value_set_by_key(trace_context, "data", data); + } + return trace_context; +} + +void +sentry__scope_notify_trace_context(sentry_scope_t *scope) +{ + sentry_value_t trace_context = get_scope_trace_context(scope); + if (sentry_value_is_null(trace_context)) { + // Fall back to the propagation trace so observers see the same trace + // context as an event with scope applied. + trace_context + = sentry_value_get_by_key(scope->propagation_context, "trace"); + sentry_value_incref(trace_context); + } + SENTRY_SCOPE_NOTIFY(scope, set_trace_context, trace_context); + sentry_value_decref(trace_context); +} + #ifdef SENTRY_UNITTEST sentry_value_t sentry__scope_get_span_or_transaction(void) @@ -446,23 +587,14 @@ sentry__scope_apply_to_event(const sentry_scope_t *scope, // prep contexts sourced from scope; data about transaction on scope needs // to be extracted and inserted - sentry_value_t scoped_txn_or_span = sentry_value_new_null(); sentry_value_t scope_trace = sentry_value_new_null(); if (!is_transaction) { - scoped_txn_or_span = get_span_or_transaction(scope); - scope_trace = sentry__value_get_trace_context(scoped_txn_or_span); + scope_trace = get_scope_trace_context(scope); } if (!sentry_value_is_null(scope_trace)) { if (sentry_value_is_null(contexts)) { contexts = sentry_value_new_object(); } - sentry_value_t scoped_txn_or_span_data - = sentry_value_get_by_key(scoped_txn_or_span, "data"); - if (!sentry_value_is_null(scoped_txn_or_span_data)) { - sentry_value_incref(scoped_txn_or_span_data); - sentry_value_set_by_key( - scope_trace, "data", scoped_txn_or_span_data); - } sentry_value_set_by_key(contexts, "trace", scope_trace); } @@ -515,7 +647,9 @@ sentry__scope_apply_to_event(const sentry_scope_t *scope, void sentry_scope_add_breadcrumb(sentry_scope_t *scope, sentry_value_t breadcrumb) { - sentry__ringbuffer_append(scope->breadcrumbs, breadcrumb); + if (sentry__ringbuffer_append(scope->breadcrumbs, breadcrumb) == 0) { + SENTRY_SCOPE_NOTIFY(scope, add_breadcrumb, breadcrumb); + } } void @@ -523,12 +657,14 @@ sentry_scope_set_user(sentry_scope_t *scope, sentry_value_t user) { sentry_value_decref(scope->user); scope->user = user; + SENTRY_SCOPE_NOTIFY(scope, set_user, user); } void sentry_scope_set_tag(sentry_scope_t *scope, const char *key, const char *value) { - sentry_value_set_by_key(scope->tags, key, sentry_value_new_string(value)); + sentry_scope_set_tag_n(scope, key, sentry__guarded_strlen(key), value, + sentry__guarded_strlen(value)); } void @@ -537,13 +673,14 @@ sentry_scope_set_tag_n(sentry_scope_t *scope, const char *key, size_t key_len, { sentry_value_set_by_key_n( scope->tags, key, key_len, sentry_value_new_string_n(value, value_len)); + SENTRY_SCOPE_NOTIFY(scope, set_tag, key, key_len, value, value_len); } void sentry_scope_set_extra( sentry_scope_t *scope, const char *key, sentry_value_t value) { - sentry_value_set_by_key(scope->extra, key, value); + sentry_scope_set_extra_n(scope, key, sentry__guarded_strlen(key), value); } void @@ -551,6 +688,7 @@ sentry_scope_set_extra_n(sentry_scope_t *scope, const char *key, size_t key_len, sentry_value_t value) { sentry_value_set_by_key_n(scope->extra, key, key_len, value); + SENTRY_SCOPE_NOTIFY(scope, set_extra, key, key_len, value); } void @@ -590,6 +728,8 @@ sentry_scope_set_context( sentry_scope_t *scope, const char *key, sentry_value_t value) { sentry_value_set_by_key(scope->contexts, key, value); + SENTRY_SCOPE_NOTIFY( + scope, set_context, key, sentry__guarded_strlen(key), value); } void @@ -597,6 +737,7 @@ sentry_scope_set_context_n(sentry_scope_t *scope, const char *key, size_t key_len, sentry_value_t value) { sentry_value_set_by_key_n(scope->contexts, key, key_len, value); + SENTRY_SCOPE_NOTIFY(scope, set_context, key, key_len, value); } void @@ -619,6 +760,7 @@ sentry_scope_update_context_n(sentry_scope_t *scope, const char *key, sentry__value_merge_objects(value, context); sentry_value_set_by_key_n(scope->contexts, key, key_len, value); } + SENTRY_SCOPE_NOTIFY(scope, set_context, key, key_len, value); } void @@ -633,6 +775,7 @@ sentry__scope_set_fingerprint_va( sentry_value_decref(scope->fingerprint); scope->fingerprint = fingerprint_value; + SENTRY_SCOPE_NOTIFY(scope, set_fingerprint, fingerprint_value); } void @@ -687,12 +830,30 @@ sentry_scope_set_fingerprints( sentry_value_decref(scope->fingerprint); scope->fingerprint = fingerprints; + SENTRY_SCOPE_NOTIFY(scope, set_fingerprint, fingerprints); } void sentry_scope_set_level(sentry_scope_t *scope, sentry_level_t level) { scope->level = level; + SENTRY_SCOPE_NOTIFY(scope, set_level, level); +} + +sentry_attachment_t * +sentry__scope_add_attachment( + sentry_scope_t *scope, sentry_attachment_t *attachment) +{ + if (!attachment) { + return NULL; + } + + sentry_attachment_t *added + = sentry__attachments_add(&scope->attachments, attachment); + if (added == attachment) { + SENTRY_SCOPE_NOTIFY(scope, add_attachment, attachment); + } + return added; } sentry_attachment_t * @@ -706,8 +867,8 @@ sentry_attachment_t * sentry_scope_attach_file_n( sentry_scope_t *scope, const char *path, size_t path_len) { - return sentry__attachments_add_path(&scope->attachments, - sentry__path_from_str_n(path, path_len), NULL, NULL); + return sentry__scope_add_attachment(scope, + sentry__attachment_from_path(sentry__path_from_str_n(path, path_len))); } sentry_attachment_t * @@ -722,7 +883,7 @@ sentry_attachment_t * sentry_scope_attach_bytes_n(sentry_scope_t *scope, const char *buf, size_t buf_len, const char *filename, size_t filename_len) { - return sentry__attachments_add(&scope->attachments, + return sentry__scope_add_attachment(scope, sentry__attachment_from_buffer( buf, buf_len, sentry__path_from_str_n(filename, filename_len))); } @@ -739,8 +900,8 @@ sentry_attachment_t * sentry_scope_attach_filew_n( sentry_scope_t *scope, const wchar_t *path, size_t path_len) { - return sentry__attachments_add_path(&scope->attachments, - sentry__path_from_wstr_n(path, path_len), NULL, NULL); + return sentry__scope_add_attachment(scope, + sentry__attachment_from_path(sentry__path_from_wstr_n(path, path_len))); } sentry_attachment_t * @@ -756,7 +917,7 @@ sentry_attachment_t * sentry_scope_attach_bytesw_n(sentry_scope_t *scope, const char *buf, size_t buf_len, const wchar_t *filename, size_t filename_len) { - return sentry__attachments_add(&scope->attachments, + return sentry__scope_add_attachment(scope, sentry__attachment_from_buffer( buf, buf_len, sentry__path_from_wstr_n(filename, filename_len))); } diff --git a/src/sentry_scope.h b/src/sentry_scope.h index 6047fa803..9d1f6b3d8 100644 --- a/src/sentry_scope.h +++ b/src/sentry_scope.h @@ -8,6 +8,46 @@ #include "sentry_session.h" #include "sentry_value.h" +/** + * Scope observer — one callback per scope property. + * + * Implementors set the function pointers they care about. NULL pointers are + * skipped. Callbacks are invoked while the scope lock is held. + * + * Ownership: the scope takes ownership of the observer pointer on + * registration; the caller must not free it after that point. + */ +typedef struct sentry_scope_observer_s { + void *data; + + void (*set_release)(void *data, const char *release, size_t release_len); + void (*set_environment)( + void *data, const char *environment, size_t environment_len); + void (*set_transaction)( + void *data, const char *transaction, size_t transaction_len); + void (*set_fingerprint)(void *data, sentry_value_t fingerprint); + void (*set_level)(void *data, sentry_level_t level); + void (*set_user)(void *data, sentry_value_t user); + + void (*add_breadcrumb)(void *data, sentry_value_t breadcrumb); + + void (*set_tag)(void *data, const char *key, size_t key_len, + const char *value, size_t value_len); + void (*remove_tag)(void *data, const char *key, size_t key_len); + + void (*set_extra)( + void *data, const char *key, size_t key_len, sentry_value_t value); + void (*remove_extra)(void *data, const char *key, size_t key_len); + + void (*set_context)( + void *data, const char *key, size_t key_len, sentry_value_t value); + void (*remove_context)(void *data, const char *key, size_t key_len); + void (*set_trace_context)(void *data, sentry_value_t value); + + void (*add_attachment)(void *data, sentry_attachment_t *attachment); + void (*remove_attachment)(void *data, sentry_attachment_t *attachment); +} sentry_scope_observer_t; + /** * This represents the current scope. */ @@ -39,6 +79,10 @@ struct sentry_scope_s { sentry_transaction_t *transaction_object; sentry_span_t *span; bool trace_managed; + + sentry_scope_observer_t **observers; + size_t num_observers; + size_t is_notifying; }; /** @@ -114,6 +158,9 @@ void sentry__scope_remove_attribute(sentry_scope_t *scope, const char *key); void sentry__scope_remove_attribute_n( sentry_scope_t *scope, const char *key, size_t key_len); +sentry_attachment_t *sentry__scope_add_attachment( + sentry_scope_t *scope, sentry_attachment_t *attachment); + /** * These are convenience macros to automatically lock/unlock the global scope * inside a code block. @@ -128,6 +175,50 @@ void sentry__scope_remove_attribute_n( for (sentry_scope_t *Scope = sentry__scope_lock(); Scope; \ sentry__scope_unlock(), Scope = NULL) +/** + * Allocate and zero-initialize a scope observer. + * + * Returns NULL on allocation failure. The caller sets whichever callback + * function pointers and the `data` pointer they need, then registers the + * observer with `sentry__scope_add_observer`, which takes ownership. + */ +sentry_scope_observer_t *sentry__scope_observer_new(void); + +/** + * Register a scope observer. + * + * Takes ownership of `observer`; the caller must not free it after this call. + * Must be called while holding the scope lock. Registration order is respected + * — observers are notified in registration order. + */ +bool sentry__scope_add_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer); + +/** + * Remove a scope observer. + * + * Frees `observer` if it is registered. Must be called while holding the scope + * lock. Does nothing if `observer` is NULL or not registered. + */ +void sentry__scope_remove_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer); + +size_t sentry__scope_begin_notify(sentry_scope_t *scope); +void sentry__scope_end_notify(sentry_scope_t *scope); + +/** Notify observers registered before this notification started. */ +#define SENTRY_SCOPE_NOTIFY(scope, callback, ...) \ + do { \ + size_t _end = sentry__scope_begin_notify(scope); \ + for (size_t _i = 0; _i < _end && _i < (scope)->num_observers; _i++) { \ + sentry_scope_observer_t *_observer = (scope)->observers[_i]; \ + if (_observer && _observer->callback) { \ + _observer->callback(_observer->data, __VA_ARGS__); \ + } \ + } \ + sentry__scope_end_notify(scope); \ + } while (0) + /** * Rebuilds the scope's dynamic sampling context (DSC) from the SDK options * and the current propagation context. The previous DSC is discarded. @@ -135,6 +226,12 @@ void sentry__scope_remove_attribute_n( void sentry__scope_update_dsc( sentry_scope_t *scope, const sentry_options_t *options); +/** + * Notifies observers with the trace context derived from the active scoped span + * or transaction, falling back to the propagation context when neither is set. + */ +void sentry__scope_notify_trace_context(sentry_scope_t *scope); + /** * Replaces the scope's dynamic sampling context (DSC) with a verbatim copy * of the incoming object. Used when continuing an upstream trace: per the diff --git a/tests/test_integration_crashpad.py b/tests/test_integration_crashpad.py index b73f5e73e..0c56f4d8a 100644 --- a/tests/test_integration_crashpad.py +++ b/tests/test_integration_crashpad.py @@ -434,6 +434,37 @@ def test_crashpad_dumping_crash(cmake, httpserver, run_args, build_args): assert wait_for_file(minidump) +@pytest.mark.skipif( + sys.platform == "darwin", + reason="crashpad doesn't provide SetFirstChanceExceptionHandler on macOS", +) +def test_crashpad_crash_before_send(cmake, httpserver): + tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "crashpad"}) + + env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver)) + httpserver.expect_oneshot_request("/api/123456/minidump/").respond_with_data("OK") + + with httpserver.wait(timeout=10) as waiting: + run( + tmp_path, + "sentry_example", + [ + "log", + "overflow-breadcrumbs", + "crashpad-wait-for-upload", + "crash", + "before-send", + ], + expect_failure=True, + env=env, + ) + + assert waiting.result + + attachments = assert_crashpad_upload(httpserver.log[0][0]) + assert attachments.event["adapted_by"] == "before_send" + + @pytest.mark.parametrize( "stack_size", [ diff --git a/tests/unit/test_scope.c b/tests/unit/test_scope.c index 0ac3951ff..4d793fd03 100644 --- a/tests/unit/test_scope.c +++ b/tests/unit/test_scope.c @@ -1132,3 +1132,772 @@ SENTRY_TEST(scope_local_attributes) sentry_close(); } + +typedef struct { + sentry_value_t release; + sentry_value_t environment; + sentry_value_t transaction; + sentry_value_t fingerprint; + sentry_level_t level; + sentry_value_t user; + sentry_value_t breadcrumbs; + sentry_value_t tags; + sentry_value_t extras; + sentry_value_t contexts; + sentry_value_t trace_contexts; + sentry_value_t attachments; + bool was_called; +} test_observer_data_t; + +typedef struct { + test_observer_data_t *self_data; + test_observer_data_t *nested_data; + sentry_scope_observer_t *self; + sentry_scope_observer_t *added; +} reentrant_observer_data_t; + +static void +observe_set_release(void *data, const char *release, size_t release_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->release = sentry_value_new_string_n(release, release_len); + d->was_called = true; +} + +static void +observe_set_environment( + void *data, const char *environment, size_t environment_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->environment = sentry_value_new_string_n(environment, environment_len); + d->was_called = true; +} + +static void +observe_set_transaction( + void *data, const char *transaction, size_t transaction_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->transaction = sentry_value_new_string_n(transaction, transaction_len); + d->was_called = true; +} + +static void +observe_set_fingerprint(void *data, sentry_value_t fingerprint) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (!sentry_value_is_null(d->fingerprint)) { + sentry_value_decref(d->fingerprint); + } + sentry_value_incref(fingerprint); + d->fingerprint = fingerprint; + d->was_called = true; +} + +static void +observe_set_level(void *data, sentry_level_t level) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->level = level; + d->was_called = true; +} + +static void +observe_add_attachment(void *data, sentry_attachment_t *attachment) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->attachments)) { + d->attachments = sentry_value_new_list(); + } + sentry_value_t obj = sentry_value_new_object(); + const char *filename = sentry__attachment_get_filename(attachment); + if (filename) { + sentry_value_set_by_key( + obj, "filename", sentry_value_new_string(filename)); + } + if (attachment->buf) { + sentry_value_set_by_key(obj, "buf", + sentry_value_new_string_n(attachment->buf, attachment->buf_len)); + } + sentry_value_append(d->attachments, obj); + d->was_called = true; +} + +static void +observe_remove_attachment(void *data, sentry_attachment_t *attachment) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->attachments)) { + d->attachments = sentry_value_new_list(); + } + sentry_value_t obj = sentry_value_new_object(); + const char *filename = sentry__attachment_get_filename(attachment); + if (filename) { + sentry_value_set_by_key( + obj, "filename", sentry_value_new_string(filename)); + } + sentry_value_set_by_key(obj, "removed", sentry_value_new_bool(true)); + sentry_value_append(d->attachments, obj); + d->was_called = true; +} + +static void +observe_set_user(void *data, sentry_value_t user) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->user = user; + d->was_called = true; +} + +static void +observe_add_breadcrumb(void *data, sentry_value_t breadcrumb) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->breadcrumbs)) { + d->breadcrumbs = sentry_value_new_list(); + } + sentry_value_incref(breadcrumb); + sentry_value_append(d->breadcrumbs, breadcrumb); + d->was_called = true; +} + +static void +observe_set_tag(void *data, const char *key, size_t key_len, const char *value, + size_t value_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->tags)) { + d->tags = sentry_value_new_object(); + } + sentry_value_set_by_key_n( + d->tags, key, key_len, sentry_value_new_string_n(value, value_len)); + d->was_called = true; +} + +static void +observe_set_tag_remove_self_and_add(void *data, const char *key, size_t key_len, + const char *value, size_t value_len) +{ + reentrant_observer_data_t *d = (reentrant_observer_data_t *)data; + observe_set_tag(d->self_data, key, key_len, value, value_len); + SENTRY_WITH_SCOPE_MUT_NO_FLUSH (scope) { + sentry__scope_remove_observer(scope, d->self); + TEST_CHECK(sentry__scope_add_observer(scope, d->added)); + } + d->nested_data->was_called = false; + sentry_set_extra("nested", sentry_value_new_string("notify")); + TEST_CHECK(d->nested_data->was_called); +} + +static void +observe_remove_tag(void *data, const char *key, size_t key_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->tags)) { + d->tags = sentry_value_new_object(); + } + sentry_value_set_by_key_n( + d->tags, key, key_len, sentry_value_new_string("(removed)")); + d->was_called = true; +} + +static void +observe_set_extra( + void *data, const char *key, size_t key_len, sentry_value_t value) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->extras)) { + d->extras = sentry_value_new_object(); + } + sentry_value_incref(value); + sentry_value_set_by_key_n(d->extras, key, key_len, value); + d->was_called = true; +} + +static void +observe_remove_extra(void *data, const char *key, size_t key_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->extras)) { + d->extras = sentry_value_new_object(); + } + sentry_value_set_by_key_n( + d->extras, key, key_len, sentry_value_new_string("(removed)")); + d->was_called = true; +} + +static void +observe_set_context( + void *data, const char *key, size_t key_len, sentry_value_t value) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->contexts)) { + d->contexts = sentry_value_new_object(); + } + sentry_value_incref(value); + sentry_value_set_by_key_n(d->contexts, key, key_len, value); + d->was_called = true; +} + +static void +observe_remove_context(void *data, const char *key, size_t key_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->contexts)) { + d->contexts = sentry_value_new_object(); + } + sentry_value_set_by_key_n( + d->contexts, key, key_len, sentry_value_new_string("(removed)")); + d->was_called = true; +} + +static void +observe_set_trace_context(void *data, sentry_value_t trace_context) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->trace_contexts)) { + d->trace_contexts = sentry_value_new_list(); + } + sentry_value_incref(trace_context); + sentry_value_append(d->trace_contexts, trace_context); + d->was_called = true; +} + +SENTRY_TEST(scope_observer_null) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .tags = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_tag = observe_set_tag; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_tag("my-tag", "my-value"); + TEST_CHECK(d.was_called); + + d.was_called = false; + sentry_remove_tag("my-tag"); + TEST_CHECK(!d.was_called); + + sentry_value_decref(d.tags); + + sentry_close(); +} + +SENTRY_TEST(scope_observer_multiple) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d1 = { .tags = sentry_value_new_null() }; + test_observer_data_t d2 = { .tags = sentry_value_new_null() }; + sentry_scope_observer_t *observer1 = sentry__scope_observer_new(); + observer1->data = &d1; + observer1->set_tag = observe_set_tag; + + sentry_scope_observer_t *observer2 = sentry__scope_observer_new(); + observer2->data = &d2; + observer2->set_tag = observe_set_tag; + + SENTRY_WITH_SCOPE_MUT (scope) { + TEST_CHECK(sentry__scope_add_observer(scope, observer1)); + TEST_CHECK(sentry__scope_add_observer(scope, observer2)); + } + + sentry_set_tag("multi", "test"); + TEST_CHECK(d1.was_called); + TEST_CHECK(d2.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d1.tags, "multi")), + "test"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d2.tags, "multi")), + "test"); + + d1.was_called = false; + d2.was_called = false; + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_remove_observer(scope, observer1); + } + + sentry_set_tag("multi", "again"); + TEST_CHECK(!d1.was_called); + TEST_CHECK(d2.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d2.tags, "multi")), + "again"); + + sentry_value_decref(d1.tags); + sentry_value_decref(d2.tags); + sentry_close(); +} + +SENTRY_TEST(scope_observer_mutate) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d1 = { .tags = sentry_value_new_null() }; + test_observer_data_t d2 = { .tags = sentry_value_new_null() }; + test_observer_data_t d3 = { .tags = sentry_value_new_null() }; + reentrant_observer_data_t reentrant + = { .self_data = &d1, .nested_data = &d2 }; + + sentry_scope_observer_t *observer1 = sentry__scope_observer_new(); + reentrant.self = observer1; + observer1->data = &reentrant; + observer1->set_tag = observe_set_tag_remove_self_and_add; + + sentry_scope_observer_t *observer2 = sentry__scope_observer_new(); + observer2->data = &d2; + observer2->set_tag = observe_set_tag; + observer2->set_extra = observe_set_extra; + + sentry_scope_observer_t *observer3 = sentry__scope_observer_new(); + reentrant.added = observer3; + observer3->data = &d3; + observer3->set_tag = observe_set_tag; + + SENTRY_WITH_SCOPE_MUT (scope) { + TEST_CHECK(sentry__scope_add_observer(scope, observer1)); + TEST_CHECK(sentry__scope_add_observer(scope, observer2)); + } + + sentry_set_tag("reentrant", "first"); + TEST_CHECK(d1.was_called); + TEST_CHECK(d2.was_called); + TEST_CHECK(!d3.was_called); + + d1.was_called = false; + d2.was_called = false; + sentry_set_tag("reentrant", "second"); + TEST_CHECK(!d1.was_called); + TEST_CHECK(d2.was_called); + TEST_CHECK(d3.was_called); + + sentry_value_decref(d1.tags); + sentry_value_decref(d2.tags); + sentry_value_decref(d3.tags); + sentry_close(); +} + +SENTRY_TEST(scope_observer_release) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_release = observe_set_release; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_release("my-release"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL(sentry_value_as_string(d.release), "my-release"); + + sentry_value_decref(d.release); + sentry_close(); +} + +SENTRY_TEST(scope_observer_environment) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_environment = observe_set_environment; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_environment("my-env"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL(sentry_value_as_string(d.environment), "my-env"); + + sentry_value_decref(d.environment); + sentry_close(); +} + +SENTRY_TEST(scope_observer_transaction) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_transaction = observe_set_transaction; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_transaction("my-transaction"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(d.transaction), "my-transaction"); + + sentry_value_decref(d.transaction); + sentry_close(); +} + +SENTRY_TEST(scope_observer_fingerprint) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_fingerprint = observe_set_fingerprint; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_fingerprint("my-fingerprint", NULL); + TEST_CHECK(d.was_called); + TEST_CHECK(!sentry_value_is_null(d.fingerprint)); + TEST_CHECK_JSON_VALUE(d.fingerprint, "[\"my-fingerprint\"]"); + + d.was_called = false; + sentry_remove_fingerprint(); + TEST_CHECK(d.was_called); + TEST_CHECK(sentry_value_is_null(d.fingerprint)); + + sentry_close(); +} + +SENTRY_TEST(scope_observer_level) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_level = observe_set_level; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_level(SENTRY_LEVEL_WARNING); + TEST_CHECK(d.was_called); + TEST_CHECK(d.level == SENTRY_LEVEL_WARNING); + + sentry_close(); +} + +SENTRY_TEST(scope_observer_user) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_user = observe_set_user; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_value_t user = sentry_value_new_object(); + sentry_value_set_by_key(user, "id", sentry_value_new_string("user123")); + sentry_set_user(user); + TEST_CHECK(d.was_called); + TEST_CHECK(!sentry_value_is_null(d.user)); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.user, "id")), + "user123"); + + d.was_called = false; + sentry_remove_user(); + TEST_CHECK(d.was_called); + TEST_CHECK(sentry_value_is_null(d.user)); + + sentry_close(); +} + +SENTRY_TEST(scope_observer_breadcrumbs) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .breadcrumbs = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->add_breadcrumb = observe_add_breadcrumb; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_add_breadcrumb( + sentry_value_new_breadcrumb(NULL, "first breadcrumb")); + TEST_CHECK(d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.breadcrumbs), 1); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key( + sentry_value_get_by_index(d.breadcrumbs, 0), "message")), + "first breadcrumb"); + + sentry_add_breadcrumb( + sentry_value_new_breadcrumb("warning", "second breadcrumb")); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.breadcrumbs), 2); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key( + sentry_value_get_by_index(d.breadcrumbs, 1), "message")), + "second breadcrumb"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key( + sentry_value_get_by_index(d.breadcrumbs, 1), "type")), + "warning"); + + sentry_value_decref(d.breadcrumbs); + sentry_close(); +} + +SENTRY_TEST(scope_observer_tags) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .tags = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_tag = observe_set_tag; + observer->remove_tag = observe_remove_tag; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_tag("my-tag", "my-value"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.tags, "my-tag")), + "my-value"); + TEST_CHECK_INT_EQUAL( + sentry_value_get_length(sentry_value_get_by_key(d.tags, "my-tag")), 8); + + d.was_called = false; + sentry_remove_tag("my-tag"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.tags, "my-tag")), + "(removed)"); + + sentry_value_decref(d.tags); + sentry_close(); +} + +SENTRY_TEST(scope_observer_extras) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .extras = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_extra = observe_set_extra; + observer->remove_extra = observe_remove_extra; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_value_t val = sentry_value_new_string("extra-value"); + sentry_set_extra("my-extra", val); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.extras, "my-extra")), + "extra-value"); + + d.was_called = false; + sentry_remove_extra("my-extra"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.extras, "my-extra")), + "(removed)"); + + sentry_value_decref(d.extras); + sentry_close(); +} + +SENTRY_TEST(scope_observer_contexts) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .contexts = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_context = observe_set_context; + observer->remove_context = observe_remove_context; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_value_t ctx = sentry_value_new_object(); + sentry_value_set_by_key(ctx, "type", sentry_value_new_string("device")); + sentry_set_context("my-context", ctx); + TEST_CHECK(d.was_called); + sentry_value_t received = sentry_value_get_by_key(d.contexts, "my-context"); + TEST_CHECK(!sentry_value_is_null(received)); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(received, "type")), + "device"); + + sentry_value_t update = sentry_value_new_object(); + sentry_value_set_by_key(update, "version", sentry_value_new_string("1.0")); + d.was_called = false; + sentry_update_context("my-context", update); + TEST_CHECK(d.was_called); + received = sentry_value_get_by_key(d.contexts, "my-context"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(received, "type")), + "device"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(received, "version")), + "1.0"); + + d.was_called = false; + sentry_remove_context("my-context"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL(sentry_value_as_string(sentry_value_get_by_key( + d.contexts, "my-context")), + "(removed)"); + + sentry_value_decref(d.contexts); + sentry_close(); +} + +SENTRY_TEST(scope_observer_trace_context) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_options_set_traces_sample_rate(options, 1.0); + sentry_init(options); + + test_observer_data_t d = { .trace_contexts = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_trace_context = observe_set_trace_context; + + SENTRY_WITH_SCOPE_MUT (scope) { + TEST_CHECK(sentry__scope_add_observer(scope, observer)); + } + + sentry_transaction_context_t *tx_ctx + = sentry_transaction_context_new("root", "op"); + sentry_transaction_t *tx + = sentry_transaction_start(tx_ctx, sentry_value_new_null()); + sentry_transaction_set_data(tx, "tx-data", sentry_value_new_string("root")); + sentry_set_transaction_object(tx); + + TEST_CHECK(d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.trace_contexts), 1); + sentry_value_t trace = sentry_value_get_by_index(d.trace_contexts, 0); + TEST_CHECK( + !sentry_value_is_null(sentry_value_get_by_key(trace, "trace_id"))); + TEST_CHECK( + !sentry_value_is_null(sentry_value_get_by_key(trace, "span_id"))); + sentry_value_t data = sentry_value_get_by_key(trace, "data"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(data, "tx-data")), + "root"); + + sentry_span_t *child = sentry_transaction_start_child(tx, "child", "desc"); + sentry_span_set_data(child, "span-data", sentry_value_new_string("child")); + sentry_set_span(child); + + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.trace_contexts), 2); + trace = sentry_value_get_by_index(d.trace_contexts, 1); + data = sentry_value_get_by_key(trace, "data"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(data, "span-data")), + "child"); + + sentry_span_finish(child); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.trace_contexts), 3); + + sentry_transaction_finish(tx); + sentry_value_decref(d.trace_contexts); + sentry_close(); +} + +SENTRY_TEST(scope_observer_attachments) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .attachments = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->add_attachment = observe_add_attachment; + observer->remove_attachment = observe_remove_attachment; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_attachment_t *attachment = sentry_attach_bytes("buf", 3, "test.txt"); + TEST_CHECK(d.was_called); + TEST_CHECK(attachment != NULL); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 1); + sentry_value_t added = sentry_value_get_by_index(d.attachments, 0); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(added, "buf")), "buf"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(added, "filename")), + "test.txt"); + + d.was_called = false; + sentry_remove_attachment(attachment); + TEST_CHECK(d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 2); + sentry_value_t removed = sentry_value_get_by_index(d.attachments, 1); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(removed, "filename")), + "test.txt"); + TEST_CHECK( + sentry_value_is_true(sentry_value_get_by_key(removed, "removed"))); + + attachment = sentry_attach_file("test.txt"); + TEST_CHECK(d.was_called); + TEST_CHECK(attachment != NULL); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 3); + + d.was_called = false; + sentry_attachment_t *duplicate = sentry_attach_file("test.txt"); + TEST_CHECK(duplicate == attachment); + TEST_CHECK(!d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 3); + + d.was_called = false; + sentry_remove_attachment(attachment); + TEST_CHECK(d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 4); + + d.was_called = false; + sentry_remove_attachment(duplicate); + TEST_CHECK(!d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 4); + + sentry_value_decref(d.attachments); + sentry_close(); +} diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 86addc6da..f16b745ae 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -285,14 +285,29 @@ XX(sampling_decision) XX(sampling_transaction) XX(scope_breadcrumbs) XX(scope_contexts) -XX(scope_update_context) XX(scope_extra) XX(scope_fingerprint) XX(scope_fingerprint_n) XX(scope_global_attributes) XX(scope_level) XX(scope_local_attributes) +XX(scope_observer_attachments) +XX(scope_observer_breadcrumbs) +XX(scope_observer_contexts) +XX(scope_observer_environment) +XX(scope_observer_extras) +XX(scope_observer_fingerprint) +XX(scope_observer_level) +XX(scope_observer_multiple) +XX(scope_observer_mutate) +XX(scope_observer_null) +XX(scope_observer_release) +XX(scope_observer_tags) +XX(scope_observer_trace_context) +XX(scope_observer_transaction) +XX(scope_observer_user) XX(scope_tags) +XX(scope_update_context) XX(scope_user) XX(scope_user_id) XX(scoped_txn)