From 1a0443c3a53086c29a73d9babb9ad50799a31b2a Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 12 Aug 2026 12:17:05 +0200 Subject: [PATCH 1/3] chore: deprecate enable_logs/metrics --- CHANGELOG.md | 7 ++ examples/example.c | 90 ++++++++++-------------- include/sentry.h | 14 +++- src/sentry_logs.c | 25 +++---- src/sentry_metrics.c | 76 +++++++++++--------- src/sentry_options.c | 14 ++-- src/sentry_options.h | 2 - src/sentry_telemetry.c | 12 +--- tests/test_integration_client_reports.py | 1 - tests/test_integration_metrics.py | 17 ----- tests/unit/test_logger.c | 21 +++--- tests/unit/test_logs.c | 41 ++--------- tests/unit/test_metrics.c | 24 +------ tests/unit/tests.inc | 5 +- 14 files changed, 140 insertions(+), 209 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36cd328005..72a6d3ed4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +**Breaking / Important behavior changes**: + +- Deprecate `sentry_options_get/set_enable_logs` and `sentry_options_get/set_enable_metrics`. The setters no longer affect SDK behavior and the getters always return `true`. ([#1980](https://github.com/getsentry/sentry-native/pull/1980)) + > Structured logs and metrics have been enabled by default since `0.13`. + > + > We recognize that this change may inconvenience applications that rely on the opt-out. Use `sentry_options_set_before_send_log` or `sentry_options_set_before_send_metric` to filter logs or metrics. We made this tradeoff deliberately because consistent behavior across SDK integrations will help most users successfully adopt these features. + **Features**: - 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)) diff --git a/examples/example.c b/examples/example.c index 005a8f36dd..e36b9c8587 100644 --- a/examples/example.c +++ b/examples/example.c @@ -857,10 +857,6 @@ main(int argc, char **argv) sentry_options_set_logger_enabled_when_crashed(options, 1); } - if (has_arg(argc, argv, "disable-logs")) { - sentry_options_set_enable_logs(options, false); - } - if (has_arg(argc, argv, "crash-reporter")) { #ifdef SENTRY_PLATFORM_WINDOWS sentry_options_set_external_crash_reporter_pathw( @@ -895,10 +891,6 @@ main(int argc, char **argv) sentry_options_set_http_retry(options, false); } - if (has_arg(argc, argv, "disable-metrics")) { - sentry_options_set_enable_metrics(options, false); - } - if (has_arg(argc, argv, "before-send-metric")) { sentry_options_set_before_send_metric( options, before_send_metric_callback, NULL); @@ -1062,54 +1054,48 @@ main(int argc, char **argv) } } - if (sentry_options_get_enable_logs(options)) { - if (has_arg(argc, argv, "capture-log")) { - sentry_log_debug("I'm a log message!"); - } - if (has_arg(argc, argv, "logs-timer")) { - for (int i = 0; i < 10; i++) { - sentry_log_info("Informational log nr.%d", i); - } - // sleep >5s to trigger logs timer - sleep_s(6); - // we should see two envelopes make its way to Sentry - sentry_log_debug("post-sleep log"); - } - if (has_arg(argc, argv, "logs-threads")) { - run_threads(log_thread_func); + if (has_arg(argc, argv, "capture-log")) { + sentry_log_debug("I'm a log message!"); + } + if (has_arg(argc, argv, "logs-timer")) { + for (int i = 0; i < 10; i++) { + sentry_log_info("Informational log nr.%d", i); } + // sleep >5s to trigger logs timer + sleep_s(6); + // we should see two envelopes make its way to Sentry + sentry_log_debug("post-sleep log"); + } + if (has_arg(argc, argv, "logs-threads")) { + run_threads(log_thread_func); } - if (sentry_options_get_enable_metrics(options)) { - if (has_arg(argc, argv, "capture-metric")) { - sentry_metrics_count("test.counter", 1, sentry_value_new_null()); - } - if (has_arg(argc, argv, "capture-metric-all-types")) { - sentry_metrics_count("test.counter", 1, sentry_value_new_null()); - sentry_metrics_gauge("test.gauge", 42.5, SENTRY_UNIT_PERCENT, - sentry_value_new_null()); - sentry_metrics_distribution("test.distribution", 123.456, - SENTRY_UNIT_MILLISECOND, sentry_value_new_null()); - } - if (has_arg(argc, argv, "metric-with-attributes")) { - sentry_value_t attributes = sentry_value_new_object(); - sentry_value_t attr = sentry_value_new_attribute( - sentry_value_new_string("my_value"), NULL); - sentry_value_set_by_key(attributes, "my.custom.attribute", attr); - sentry_metrics_count("test.counter.with.attributes", 1, attributes); - } - if (has_arg(argc, argv, "metrics-timer")) { - for (int i = 0; i < 10; i++) { - sentry_metrics_count( - "batch.counter", 1, sentry_value_new_null()); - } - sleep_s(6); - sentry_metrics_count( - "post.sleep.counter", 1, sentry_value_new_null()); - } - if (has_arg(argc, argv, "metrics-threads")) { - run_threads(metric_thread_func); + if (has_arg(argc, argv, "capture-metric")) { + sentry_metrics_count("test.counter", 1, sentry_value_new_null()); + } + if (has_arg(argc, argv, "capture-metric-all-types")) { + sentry_metrics_count("test.counter", 1, sentry_value_new_null()); + sentry_metrics_gauge( + "test.gauge", 42.5, SENTRY_UNIT_PERCENT, sentry_value_new_null()); + sentry_metrics_distribution("test.distribution", 123.456, + SENTRY_UNIT_MILLISECOND, sentry_value_new_null()); + } + if (has_arg(argc, argv, "metric-with-attributes")) { + sentry_value_t attributes = sentry_value_new_object(); + sentry_value_t attr = sentry_value_new_attribute( + sentry_value_new_string("my_value"), NULL); + sentry_value_set_by_key(attributes, "my.custom.attribute", attr); + sentry_metrics_count("test.counter.with.attributes", 1, attributes); + } + if (has_arg(argc, argv, "metrics-timer")) { + for (int i = 0; i < 10; i++) { + sentry_metrics_count("batch.counter", 1, sentry_value_new_null()); } + sleep_s(6); + sentry_metrics_count("post.sleep.counter", 1, sentry_value_new_null()); + } + if (has_arg(argc, argv, "metrics-threads")) { + run_threads(metric_thread_func); } if (!has_arg(argc, argv, "no-setup")) { diff --git a/include/sentry.h b/include/sentry.h index 35da3abcd7..0ee8ef05ff 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -2620,8 +2620,13 @@ SENTRY_EXPERIMENTAL_API int sentry_options_get_strict_trace_continuation( * * Enabled by default. */ +SENTRY_DEPRECATED( + "This function does nothing. It will be removed in a future release.") SENTRY_EXPERIMENTAL_API void sentry_options_set_enable_logs( sentry_options_t *opts, int enable_logs); +SENTRY_DEPRECATED( + "This function always returns true. It will be removed in a future " + "release.") SENTRY_EXPERIMENTAL_API int sentry_options_get_enable_logs( const sentry_options_t *opts); @@ -2696,7 +2701,7 @@ SENTRY_API int sentry_options_get_send_client_reports( * - Success means a log was enqueued * - Discard means the `before_send_log` function discarded the log * - Failed means the log wasn't enqueued. This happens if the buffers are full - * - Disabled means the option `enable_logs` was false. + * - Disabled means the SDK was not initialized */ typedef enum { SENTRY_LOG_RETURN_SUCCESS = 0, @@ -2804,8 +2809,13 @@ SENTRY_EXPERIMENTAL_API void sentry_options_set_before_send_log( * * Enabled by default. */ +SENTRY_DEPRECATED( + "This function does nothing. It will be removed in a future release.") SENTRY_EXPERIMENTAL_API void sentry_options_set_enable_metrics( sentry_options_t *opts, int enable_metrics); +SENTRY_DEPRECATED( + "This function always returns true. It will be removed in a future " + "release.") SENTRY_EXPERIMENTAL_API int sentry_options_get_enable_metrics( const sentry_options_t *opts); @@ -2891,7 +2901,7 @@ SENTRY_EXPERIMENTAL_API void sentry_options_set_before_send_metric( * - Success means the metric was enqueued * - Discard means the `before_send_metric` callback discarded the metric * - Failed means the metric wasn't enqueued (buffers are full) - * - Disabled means metrics are disabled + * - Disabled means the SDK was not initialized */ typedef enum { SENTRY_METRICS_RESULT_SUCCESS = 0, diff --git a/src/sentry_logs.c b/src/sentry_logs.c index 9549b9f792..9b63f7b171 100644 --- a/src/sentry_logs.c +++ b/src/sentry_logs.c @@ -14,6 +14,17 @@ static sentry_batcher_ref_t g_batcher = SENTRY_BATCHER_REF_INIT; +static bool +sdk_is_initialized(void) +{ + bool initialized = false; + SENTRY_WITH_OPTIONS (options) { + (void)options; + initialized = true; + } + return initialized; +} + typedef enum { PRINTF_LENGTH_NONE, PRINTF_LENGTH_CHAR, @@ -519,12 +530,7 @@ send_log(sentry_level_t level, sentry_value_t log) log_return_value_t sentry__logs_log(sentry_level_t level, const char *message, va_list args) { - bool enable_logs = false; - SENTRY_WITH_OPTIONS (options) { - if (options->enable_logs) - enable_logs = true; - } - if (!enable_logs) { + if (!sdk_is_initialized()) { return SENTRY_LOG_RETURN_DISABLED; } return send_log(level, construct_log(level, message, args)); @@ -607,12 +613,7 @@ log_return_value_t sentry_scope_capture_log(sentry_scope_t *scope, sentry_level_t level, const char *body, sentry_value_t custom_attributes) { - bool enable_logs = false; - SENTRY_WITH_OPTIONS (options) { - if (options->enable_logs) - enable_logs = true; - } - if (!enable_logs) { + if (!sdk_is_initialized()) { sentry_value_decref(custom_attributes); sentry__scope_free_one_shot(scope); return SENTRY_LOG_RETURN_DISABLED; diff --git a/src/sentry_metrics.c b/src/sentry_metrics.c index 0f6f05f0ce..946c7ed817 100644 --- a/src/sentry_metrics.c +++ b/src/sentry_metrics.c @@ -10,6 +10,17 @@ static sentry_batcher_ref_t g_batcher = SENTRY_BATCHER_REF_INIT; +static bool +sdk_is_initialized(void) +{ + bool initialized = false; + SENTRY_WITH_OPTIONS (options) { + (void)options; + initialized = true; + } + return initialized; +} + static const char * metric_type_string(sentry_metric_type_t type) { @@ -65,46 +76,41 @@ sentry_scope_capture_metric(sentry_scope_t *scope, sentry_metric_type_t type, const char *name, sentry_value_t value, const char *unit, sentry_value_t attributes) { - bool enable_metrics = false; - SENTRY_WITH_OPTIONS (options) { - if (options->enable_metrics) - enable_metrics = true; - } - if (enable_metrics) { - bool discarded = false; - sentry_value_t metric - = construct_metric(scope, type, name, value, unit, attributes); + if (!sdk_is_initialized()) { + sentry_value_decref(value); + sentry_value_decref(attributes); sentry__scope_free_one_shot(scope); - SENTRY_WITH_OPTIONS (options) { - if (options->before_send_metric_func) { - metric = options->before_send_metric_func( - metric, options->before_send_metric_data); - if (sentry_value_is_null(metric)) { - SENTRY_DEBUG("metric was discarded by the " - "`before_send_metric` hook"); - sentry__client_report_discard( - SENTRY_DISCARD_REASON_BEFORE_SEND, - SENTRY_DATA_CATEGORY_TRACE_METRIC, 1); - discarded = true; - } + return SENTRY_METRICS_RESULT_DISABLED; + } + + bool discarded = false; + sentry_value_t metric + = construct_metric(scope, type, name, value, unit, attributes); + sentry__scope_free_one_shot(scope); + SENTRY_WITH_OPTIONS (options) { + if (options->before_send_metric_func) { + metric = options->before_send_metric_func( + metric, options->before_send_metric_data); + if (sentry_value_is_null(metric)) { + SENTRY_DEBUG("metric was discarded by the " + "`before_send_metric` hook"); + sentry__client_report_discard(SENTRY_DISCARD_REASON_BEFORE_SEND, + SENTRY_DATA_CATEGORY_TRACE_METRIC, 1); + discarded = true; } } - if (discarded) { - return SENTRY_METRICS_RESULT_DISCARD; - } - sentry_batcher_t *batcher = sentry__batcher_acquire(&g_batcher); - if (!batcher || !sentry__batcher_enqueue(batcher, metric)) { - sentry__batcher_release(batcher); - sentry_value_decref(metric); - return SENTRY_METRICS_RESULT_FAILED; - } + } + if (discarded) { + return SENTRY_METRICS_RESULT_DISCARD; + } + sentry_batcher_t *batcher = sentry__batcher_acquire(&g_batcher); + if (!batcher || !sentry__batcher_enqueue(batcher, metric)) { sentry__batcher_release(batcher); - return SENTRY_METRICS_RESULT_SUCCESS; + sentry_value_decref(metric); + return SENTRY_METRICS_RESULT_FAILED; } - sentry_value_decref(value); - sentry_value_decref(attributes); - sentry__scope_free_one_shot(scope); - return SENTRY_METRICS_RESULT_DISABLED; + sentry__batcher_release(batcher); + return SENTRY_METRICS_RESULT_SUCCESS; } sentry_metrics_result_t diff --git a/src/sentry_options.c b/src/sentry_options.c index 1710ca6e1e..dad8aa13dd 100644 --- a/src/sentry_options.c +++ b/src/sentry_options.c @@ -90,8 +90,6 @@ sentry_options_new(void) opts->propagate_traceparent = false; opts->strict_trace_continuation = false; opts->crashpad_limit_stack_capture_to_sp = false; - opts->enable_metrics = true; - opts->enable_logs = true; opts->cache_keep = SENTRY_CACHE_KEEP_NONE; opts->cache_max_age = 0; opts->cache_max_size = 0; @@ -1020,13 +1018,15 @@ sentry__options_has_integration(const sentry_options_t *opts, const char *name) void sentry_options_set_enable_logs(sentry_options_t *opts, int enable_logs) { - opts->enable_logs = !!enable_logs; + (void)opts; + (void)enable_logs; } int sentry_options_get_enable_logs(const sentry_options_t *opts) { - return opts->enable_logs; + (void)opts; + return 1; } void @@ -1045,13 +1045,15 @@ sentry_options_get_logs_with_attributes(const sentry_options_t *opts) void sentry_options_set_enable_metrics(sentry_options_t *opts, int enable_metrics) { - opts->enable_metrics = !!enable_metrics; + (void)opts; + (void)enable_metrics; } int sentry_options_get_enable_metrics(const sentry_options_t *opts) { - return opts->enable_metrics; + (void)opts; + return 1; } void diff --git a/src/sentry_options.h b/src/sentry_options.h index dcdfeec729..9b98a27c5e 100644 --- a/src/sentry_options.h +++ b/src/sentry_options.h @@ -81,11 +81,9 @@ struct sentry_options_s { void *traces_sampler_data; char *org_id; size_t max_spans; - bool enable_logs; // takes the first varg as a `sentry_value_t` object containing attributes // if no custom attributes are to be passed, use `sentry_value_new_object()` bool logs_with_attributes; - bool enable_metrics; sentry_before_send_metric_function_t before_send_metric_func; void *before_send_metric_data; bool enable_app_hang_tracking; diff --git a/src/sentry_telemetry.c b/src/sentry_telemetry.c index 8e0268fac0..fe3d49b499 100644 --- a/src/sentry_telemetry.c +++ b/src/sentry_telemetry.c @@ -7,21 +7,13 @@ void sentry__telemetry_startup(const sentry_options_t *options) { - if (options->enable_logs) { - sentry__logs_startup(options); - } - if (options->enable_metrics) { - sentry__metrics_startup(options); - } + sentry__logs_startup(options); + sentry__metrics_startup(options); } void sentry__telemetry_shutdown(const sentry_options_t *options) { - if (!options->enable_logs && !options->enable_metrics) { - return; - } - SENTRY_DEBUG("shutting down telemetry"); sentry__logs_shutdown(options->shutdown_timeout); sentry__metrics_shutdown(options->shutdown_timeout); diff --git a/tests/test_integration_client_reports.py b/tests/test_integration_client_reports.py index 5c741a8776..f9e7bea8c3 100644 --- a/tests/test_integration_client_reports.py +++ b/tests/test_integration_client_reports.py @@ -212,7 +212,6 @@ def test_client_report_before_send_metric(cmake, httpserver): "sentry_example", [ "log", - "enable-metrics", "discarding-before-send-metric", "capture-metric", "capture-event", diff --git a/tests/test_integration_metrics.py b/tests/test_integration_metrics.py index 91d789163f..185f99db03 100644 --- a/tests/test_integration_metrics.py +++ b/tests/test_integration_metrics.py @@ -222,23 +222,6 @@ def test_before_send_metric_discard(cmake, httpserver): assert len(httpserver.log) == 0 -def test_metrics_disabled(cmake, httpserver): - tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"}) - - httpserver.expect_request("/api/123456/envelope/").respond_with_data("OK") - env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver)) - - run( - tmp_path, - "sentry_example", - ["log", "disable-metrics", "capture-metric"], - env=env, - ) - - # No metrics should be sent when feature is disabled - assert len(httpserver.log) == 0 - - def test_metrics_event(cmake, httpserver): tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"}) diff --git a/tests/unit/test_logger.c b/tests/unit/test_logger.c index 44360a1268..c372a341eb 100644 --- a/tests/unit/test_logger.c +++ b/tests/unit/test_logger.c @@ -1,5 +1,7 @@ #include "sentry_core.h" #include "sentry_logger.h" +#include "sentry_logs.h" +#include "sentry_metrics.h" #include "sentry_sync.h" #include "sentry_testsupport.h" @@ -14,6 +16,13 @@ typedef struct { // To blacklist a test, add to the respective list of `test_unit_transport` // in the `tests/test_unit.py` unit-test runner. +static void +wait_for_telemetry_threads(void) +{ + sentry__logs_wait_for_thread_startup(); + sentry__metrics_wait_for_thread_startup(); +} + static void test_logger( sentry_level_t level, const char *message, va_list args, void *_data) @@ -39,10 +48,8 @@ SENTRY_TEST(custom_logger) SENTRY_TEST_OPTIONS_NEW(options); sentry_options_set_debug(options, true); sentry_options_set_logger(options, test_logger, &data); - sentry_options_set_enable_metrics(options, false); - sentry_options_set_enable_logs(options, false); - sentry_init(options); + wait_for_telemetry_threads(); data.assert_now = true; SENTRY_WARNF("Oh this is %s", "bad"); @@ -68,10 +75,8 @@ SENTRY_TEST(logger_enable_disable_functionality) SENTRY_TEST_OPTIONS_NEW(options); sentry_options_set_debug(options, true); sentry_options_set_logger(options, test_logger, &data); - sentry_options_set_enable_metrics(options, false); - sentry_options_set_enable_logs(options, false); - sentry_init(options); + wait_for_telemetry_threads(); // Test logging is enabled by default data.called = 0; @@ -139,10 +144,8 @@ SENTRY_TEST(logger_level) sentry_options_set_debug(options, true); sentry_options_set_logger_level(options, test_cases[i].level); sentry_options_set_logger(options, test_log_level, &data); - sentry_options_set_enable_metrics(options, false); - sentry_options_set_enable_logs(options, false); - sentry_init(options); + wait_for_telemetry_threads(); data.assert_now = true; // Test all 5 levels in order from most to least verbose diff --git a/tests/unit/test_logs.c b/tests/unit/test_logs.c index 1e7cfcb945..20d3db4b6f 100644 --- a/tests/unit/test_logs.c +++ b/tests/unit/test_logs.c @@ -53,7 +53,7 @@ SENTRY_TEST(basic_logging_functionality) sentry_init(options); sentry__logs_wait_for_thread_startup(); - // These should not crash and should respect the enable_logs option + // These should not crash. TEST_CHECK_INT_EQUAL(sentry_log_trace("Trace message"), 0); TEST_CHECK_INT_EQUAL(sentry_log_debug("Debug message"), 0); TEST_CHECK_INT_EQUAL(sentry_log_info("Info message"), 0); @@ -75,31 +75,6 @@ SENTRY_TEST(basic_logging_functionality) TEST_CHECK_INT_EQUAL(validation_data.called_count, 2); } -SENTRY_TEST(logs_disabled) -{ - transport_validation_data_t validation_data = { 0, false }; - - SENTRY_TEST_OPTIONS_NEW(options); - sentry_options_set_dsn(options, "https://foo@sentry.invalid/42"); - sentry_options_set_enable_logs(options, false); - - sentry_transport_t *transport - = sentry_transport_new(validate_logs_envelope); - sentry_transport_set_state(transport, &validation_data); - sentry_options_set_transport(options, transport); - - sentry_init(options); - - // Should return DISABLED since logs were explicitly disabled - TEST_CHECK_INT_EQUAL(sentry_log_info("This should not be sent"), 3); - - sentry_close(); - - // Transport should not be called since logs were explicitly disabled - TEST_CHECK(!validation_data.has_validation_error); - TEST_CHECK_INT_EQUAL(validation_data.called_count, 0); -} - SENTRY_TEST(formatted_log_messages) { transport_validation_data_t validation_data = { 0, false }; @@ -338,7 +313,7 @@ SENTRY_TEST(logs_force_flush) sentry_init(options); sentry__logs_wait_for_thread_startup(); - // These should not crash and should respect the enable_logs option + // These should not crash. TEST_CHECK_INT_EQUAL(sentry_log_trace("Trace message"), 0); sentry_flush(5000); TEST_CHECK_INT_EQUAL(sentry_log_debug("Debug message"), 0); @@ -556,22 +531,14 @@ SENTRY_TEST(logs_span_trace_attributes) sentry_close(); } -SENTRY_TEST(logs_plain_string_disabled) +SENTRY_TEST(logs_uninitialized) { - SENTRY_TEST_OPTIONS_NEW(options); - sentry_options_set_dsn(options, "https://foo@sentry.invalid/42"); - sentry_options_set_enable_logs(options, false); - - sentry_init(options); - - // Should not leak the attributes value sentry_value_t attrs = sentry_value_new_object(); sentry_value_set_by_key(attrs, "key", sentry_value_new_attribute(sentry_value_new_string("val"), NULL)); + TEST_CHECK_INT_EQUAL(sentry_log(SENTRY_LEVEL_INFO, "test", attrs), SENTRY_LOG_RETURN_DISABLED); - - sentry_close(); } SENTRY_TEST(logs_global_attribute_no_field_leak) diff --git a/tests/unit/test_metrics.c b/tests/unit/test_metrics.c index 7115d07348..a7a32225db 100644 --- a/tests/unit/test_metrics.c +++ b/tests/unit/test_metrics.c @@ -262,22 +262,8 @@ SENTRY_TEST(metrics_before_send_modify) TEST_CHECK_INT_EQUAL(validation_data.called_count, 1); } -SENTRY_TEST(metrics_disabled) +SENTRY_TEST(metrics_uninitialized) { - transport_validation_data_t validation_data = { 0, false }; - - SENTRY_TEST_OPTIONS_NEW(options); - sentry_options_set_dsn(options, "https://foo@sentry.invalid/42"); - sentry_options_set_enable_metrics(options, false); - - sentry_transport_t *transport - = sentry_transport_new(validate_metrics_envelope); - sentry_transport_set_state(transport, &validation_data); - sentry_options_set_transport(options, transport); - - sentry_init(options); - - // These should return DISABLED since metrics were explicitly disabled TEST_CHECK_INT_EQUAL( sentry_metrics_count("test.counter", 1, sentry_value_new_null()), SENTRY_METRICS_RESULT_DISABLED); @@ -287,12 +273,6 @@ SENTRY_TEST(metrics_disabled) TEST_CHECK_INT_EQUAL(sentry_metrics_distribution("test.distribution", 123.0, NULL, sentry_value_new_null()), SENTRY_METRICS_RESULT_DISABLED); - - sentry_close(); - - // Transport should not be called since metrics were explicitly disabled - TEST_CHECK(!validation_data.has_validation_error); - TEST_CHECK_INT_EQUAL(validation_data.called_count, 0); } SENTRY_TEST(metrics_force_flush) @@ -591,8 +571,6 @@ SENTRY_TEST(metrics_reinit) // This will deadlock if sentry__batcher_flush holds g_options_lock. SENTRY_TEST_OPTIONS_NEW(options2); sentry_options_set_dsn(options2, "https://foo@sentry.invalid/42"); - sentry_options_set_enable_metrics(options2, true); - sentry_init(options2); sentry_close(); } diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 78827091a6..ab3edc2027 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -195,7 +195,6 @@ XX(logger_enable_disable_functionality) XX(logger_level) XX(logs_custom_attributes_not_modified) XX(logs_custom_attributes_with_format_strings) -XX(logs_disabled) XX(logs_force_flush) XX(logs_global_attribute_no_field_leak) XX(logs_param_conversion) @@ -203,10 +202,10 @@ XX(logs_param_sign) XX(logs_param_types) XX(logs_param_width) XX(logs_plain_string) -XX(logs_plain_string_disabled) XX(logs_reinit) XX(logs_reinit_stress) XX(logs_span_trace_attributes) +XX(logs_uninitialized) XX(m128a_size) XX(message_with_null_text_is_valid) XX(metrics_batch) @@ -214,13 +213,13 @@ XX(metrics_before_send_discard) XX(metrics_before_send_modify) XX(metrics_count) XX(metrics_default_attributes) -XX(metrics_disabled) XX(metrics_distribution) XX(metrics_force_flush) XX(metrics_gauge) XX(metrics_global_attribute_no_field_leak) XX(metrics_reinit) XX(metrics_reinit_stress) +XX(metrics_uninitialized) XX(metrics_with_attributes) XX(minidump_context_flags) XX(minidump_context_sizes) From 59dd36cc904561c91d5081ce100db8285414d2f1 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Tue, 18 Aug 2026 15:59:09 +0200 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72a6d3ed4a..c731c883be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ **Breaking / Important behavior changes**: -- Deprecate `sentry_options_get/set_enable_logs` and `sentry_options_get/set_enable_metrics`. The setters no longer affect SDK behavior and the getters always return `true`. ([#1980](https://github.com/getsentry/sentry-native/pull/1980)) +- Deprecate `sentry_options_get/set_enable_logs` and `sentry_options_get/set_enable_metrics`. The setters no longer affect SDK behavior and the getters always return `true`. ([#2000](https://github.com/getsentry/sentry-native/pull/2000)) > Structured logs and metrics have been enabled by default since `0.13`. > > We recognize that this change may inconvenience applications that rely on the opt-out. Use `sentry_options_set_before_send_log` or `sentry_options_set_before_send_metric` to filter logs or metrics. We made this tradeoff deliberately because consistent behavior across SDK integrations will help most users successfully adopt these features. From fdc7b2d7368026bf048b040f968dc81429057981 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 19 Aug 2026 09:27:46 +0200 Subject: [PATCH 3/3] take a step back --- CHANGELOG.md | 11 +-- examples/example.c | 96 ++++++++++++++---------- include/sentry.h | 18 ++--- ndk/lib/src/main/jni/sentry.c | 2 + src/sentry_logs.c | 25 +++--- src/sentry_metrics.c | 76 +++++++++---------- src/sentry_options.c | 14 ++-- src/sentry_options.h | 2 + src/sentry_telemetry.c | 12 ++- tests/test_integration_client_reports.py | 1 + tests/test_integration_metrics.py | 17 +++++ tests/unit/test_logger.c | 24 +++--- tests/unit/test_logs.c | 41 +++++++++- tests/unit/test_metrics.c | 24 +++++- tests/unit/tests.inc | 5 +- 15 files changed, 228 insertions(+), 140 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c731c883be..8ed4a1ad66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,13 +2,6 @@ ## Unreleased -**Breaking / Important behavior changes**: - -- Deprecate `sentry_options_get/set_enable_logs` and `sentry_options_get/set_enable_metrics`. The setters no longer affect SDK behavior and the getters always return `true`. ([#2000](https://github.com/getsentry/sentry-native/pull/2000)) - > Structured logs and metrics have been enabled by default since `0.13`. - > - > We recognize that this change may inconvenience applications that rely on the opt-out. Use `sentry_options_set_before_send_log` or `sentry_options_set_before_send_metric` to filter logs or metrics. We made this tradeoff deliberately because consistent behavior across SDK integrations will help most users successfully adopt these features. - **Features**: - 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)) @@ -16,6 +9,10 @@ - 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)) - Native/Unix: The native crash daemon now loads `libcurl` dynamically at runtime by default when `SENTRY_LINK_CURL=AUTO`, avoiding `libcurl` linker work during process startup and significantly speeding up startup time. Explicitly set `SENTRY_LINK_CURL=ON` to link it directly. ([#1955](https://github.com/getsentry/sentry-native/pull/1955)) +**Deprecations**: + +- Deprecate `sentry_options_get/set_enable_logs` and `sentry_options_get/set_enable_metrics`. ([#2000](https://github.com/getsentry/sentry-native/pull/2000)) + **Fixes**: - Native: store daemon logs, minidumps, crash envelopes, and scratch files in `.run` directories so they are cleaned up with the run instead of accumulating in the database root. Minidumps can still be retained with `cache_keep`, which stores `.dmp` sidecars alongside cached envelopes. ([#1976](https://github.com/getsentry/sentry-native/pull/1976)) diff --git a/examples/example.c b/examples/example.c index e36b9c8587..0770d7cb19 100644 --- a/examples/example.c +++ b/examples/example.c @@ -857,6 +857,12 @@ main(int argc, char **argv) sentry_options_set_logger_enabled_when_crashed(options, 1); } + SENTRY_SUPPRESS_DEPRECATED + if (has_arg(argc, argv, "disable-logs")) { + sentry_options_set_enable_logs(options, false); + } + SENTRY_RESTORE_DEPRECATED + if (has_arg(argc, argv, "crash-reporter")) { #ifdef SENTRY_PLATFORM_WINDOWS sentry_options_set_external_crash_reporter_pathw( @@ -891,6 +897,12 @@ main(int argc, char **argv) sentry_options_set_http_retry(options, false); } + SENTRY_SUPPRESS_DEPRECATED + if (has_arg(argc, argv, "disable-metrics")) { + sentry_options_set_enable_metrics(options, false); + } + SENTRY_RESTORE_DEPRECATED + if (has_arg(argc, argv, "before-send-metric")) { sentry_options_set_before_send_metric( options, before_send_metric_callback, NULL); @@ -1054,49 +1066,57 @@ main(int argc, char **argv) } } - if (has_arg(argc, argv, "capture-log")) { - sentry_log_debug("I'm a log message!"); - } - if (has_arg(argc, argv, "logs-timer")) { - for (int i = 0; i < 10; i++) { - sentry_log_info("Informational log nr.%d", i); + SENTRY_SUPPRESS_DEPRECATED + if (sentry_options_get_enable_logs(options)) { + if (has_arg(argc, argv, "capture-log")) { + sentry_log_debug("I'm a log message!"); + } + if (has_arg(argc, argv, "logs-timer")) { + for (int i = 0; i < 10; i++) { + sentry_log_info("Informational log nr.%d", i); + } + // sleep >5s to trigger logs timer + sleep_s(6); + // we should see two envelopes make its way to Sentry + sentry_log_debug("post-sleep log"); + } + if (has_arg(argc, argv, "logs-threads")) { + run_threads(log_thread_func); } - // sleep >5s to trigger logs timer - sleep_s(6); - // we should see two envelopes make its way to Sentry - sentry_log_debug("post-sleep log"); - } - if (has_arg(argc, argv, "logs-threads")) { - run_threads(log_thread_func); } - if (has_arg(argc, argv, "capture-metric")) { - sentry_metrics_count("test.counter", 1, sentry_value_new_null()); - } - if (has_arg(argc, argv, "capture-metric-all-types")) { - sentry_metrics_count("test.counter", 1, sentry_value_new_null()); - sentry_metrics_gauge( - "test.gauge", 42.5, SENTRY_UNIT_PERCENT, sentry_value_new_null()); - sentry_metrics_distribution("test.distribution", 123.456, - SENTRY_UNIT_MILLISECOND, sentry_value_new_null()); - } - if (has_arg(argc, argv, "metric-with-attributes")) { - sentry_value_t attributes = sentry_value_new_object(); - sentry_value_t attr = sentry_value_new_attribute( - sentry_value_new_string("my_value"), NULL); - sentry_value_set_by_key(attributes, "my.custom.attribute", attr); - sentry_metrics_count("test.counter.with.attributes", 1, attributes); - } - if (has_arg(argc, argv, "metrics-timer")) { - for (int i = 0; i < 10; i++) { - sentry_metrics_count("batch.counter", 1, sentry_value_new_null()); + if (sentry_options_get_enable_metrics(options)) { + if (has_arg(argc, argv, "capture-metric")) { + sentry_metrics_count("test.counter", 1, sentry_value_new_null()); + } + if (has_arg(argc, argv, "capture-metric-all-types")) { + sentry_metrics_count("test.counter", 1, sentry_value_new_null()); + sentry_metrics_gauge("test.gauge", 42.5, SENTRY_UNIT_PERCENT, + sentry_value_new_null()); + sentry_metrics_distribution("test.distribution", 123.456, + SENTRY_UNIT_MILLISECOND, sentry_value_new_null()); + } + if (has_arg(argc, argv, "metric-with-attributes")) { + sentry_value_t attributes = sentry_value_new_object(); + sentry_value_t attr = sentry_value_new_attribute( + sentry_value_new_string("my_value"), NULL); + sentry_value_set_by_key(attributes, "my.custom.attribute", attr); + sentry_metrics_count("test.counter.with.attributes", 1, attributes); + } + if (has_arg(argc, argv, "metrics-timer")) { + for (int i = 0; i < 10; i++) { + sentry_metrics_count( + "batch.counter", 1, sentry_value_new_null()); + } + sleep_s(6); + sentry_metrics_count( + "post.sleep.counter", 1, sentry_value_new_null()); + } + if (has_arg(argc, argv, "metrics-threads")) { + run_threads(metric_thread_func); } - sleep_s(6); - sentry_metrics_count("post.sleep.counter", 1, sentry_value_new_null()); - } - if (has_arg(argc, argv, "metrics-threads")) { - run_threads(metric_thread_func); } + SENTRY_RESTORE_DEPRECATED if (!has_arg(argc, argv, "no-setup")) { sentry_set_transaction("test-transaction"); diff --git a/include/sentry.h b/include/sentry.h index 0ee8ef05ff..4eef281640 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -2620,13 +2620,10 @@ SENTRY_EXPERIMENTAL_API int sentry_options_get_strict_trace_continuation( * * Enabled by default. */ -SENTRY_DEPRECATED( - "This function does nothing. It will be removed in a future release.") +SENTRY_DEPRECATED("This function will be removed in a future release.") SENTRY_EXPERIMENTAL_API void sentry_options_set_enable_logs( sentry_options_t *opts, int enable_logs); -SENTRY_DEPRECATED( - "This function always returns true. It will be removed in a future " - "release.") +SENTRY_DEPRECATED("This function will be removed in a future release.") SENTRY_EXPERIMENTAL_API int sentry_options_get_enable_logs( const sentry_options_t *opts); @@ -2701,7 +2698,7 @@ SENTRY_API int sentry_options_get_send_client_reports( * - Success means a log was enqueued * - Discard means the `before_send_log` function discarded the log * - Failed means the log wasn't enqueued. This happens if the buffers are full - * - Disabled means the SDK was not initialized + * - Disabled means the option `enable_logs` was false. */ typedef enum { SENTRY_LOG_RETURN_SUCCESS = 0, @@ -2809,13 +2806,10 @@ SENTRY_EXPERIMENTAL_API void sentry_options_set_before_send_log( * * Enabled by default. */ -SENTRY_DEPRECATED( - "This function does nothing. It will be removed in a future release.") +SENTRY_DEPRECATED("This function will be removed in a future release.") SENTRY_EXPERIMENTAL_API void sentry_options_set_enable_metrics( sentry_options_t *opts, int enable_metrics); -SENTRY_DEPRECATED( - "This function always returns true. It will be removed in a future " - "release.") +SENTRY_DEPRECATED("This function will be removed in a future release.") SENTRY_EXPERIMENTAL_API int sentry_options_get_enable_metrics( const sentry_options_t *opts); @@ -2901,7 +2895,7 @@ SENTRY_EXPERIMENTAL_API void sentry_options_set_before_send_metric( * - Success means the metric was enqueued * - Discard means the `before_send_metric` callback discarded the metric * - Failed means the metric wasn't enqueued (buffers are full) - * - Disabled means the SDK was not initialized + * - Disabled means metrics are disabled */ typedef enum { SENTRY_METRICS_RESULT_SUCCESS = 0, diff --git a/ndk/lib/src/main/jni/sentry.c b/ndk/lib/src/main/jni/sentry.c index 36516b9c8e..30ecb6d55f 100644 --- a/ndk/lib/src/main/jni/sentry.c +++ b/ndk/lib/src/main/jni/sentry.c @@ -527,7 +527,9 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative( jboolean enable_logs = (jboolean)(*env)->CallBooleanMethod( env, sentry_ndk_options, enable_logs_mid); + SENTRY_SUPPRESS_DEPRECATED sentry_options_set_enable_logs(options, enable_logs); + SENTRY_RESTORE_DEPRECATED int rv = sentry_init(options); return (jint)rv; diff --git a/src/sentry_logs.c b/src/sentry_logs.c index 9b63f7b171..9549b9f792 100644 --- a/src/sentry_logs.c +++ b/src/sentry_logs.c @@ -14,17 +14,6 @@ static sentry_batcher_ref_t g_batcher = SENTRY_BATCHER_REF_INIT; -static bool -sdk_is_initialized(void) -{ - bool initialized = false; - SENTRY_WITH_OPTIONS (options) { - (void)options; - initialized = true; - } - return initialized; -} - typedef enum { PRINTF_LENGTH_NONE, PRINTF_LENGTH_CHAR, @@ -530,7 +519,12 @@ send_log(sentry_level_t level, sentry_value_t log) log_return_value_t sentry__logs_log(sentry_level_t level, const char *message, va_list args) { - if (!sdk_is_initialized()) { + bool enable_logs = false; + SENTRY_WITH_OPTIONS (options) { + if (options->enable_logs) + enable_logs = true; + } + if (!enable_logs) { return SENTRY_LOG_RETURN_DISABLED; } return send_log(level, construct_log(level, message, args)); @@ -613,7 +607,12 @@ log_return_value_t sentry_scope_capture_log(sentry_scope_t *scope, sentry_level_t level, const char *body, sentry_value_t custom_attributes) { - if (!sdk_is_initialized()) { + bool enable_logs = false; + SENTRY_WITH_OPTIONS (options) { + if (options->enable_logs) + enable_logs = true; + } + if (!enable_logs) { sentry_value_decref(custom_attributes); sentry__scope_free_one_shot(scope); return SENTRY_LOG_RETURN_DISABLED; diff --git a/src/sentry_metrics.c b/src/sentry_metrics.c index 946c7ed817..0f6f05f0ce 100644 --- a/src/sentry_metrics.c +++ b/src/sentry_metrics.c @@ -10,17 +10,6 @@ static sentry_batcher_ref_t g_batcher = SENTRY_BATCHER_REF_INIT; -static bool -sdk_is_initialized(void) -{ - bool initialized = false; - SENTRY_WITH_OPTIONS (options) { - (void)options; - initialized = true; - } - return initialized; -} - static const char * metric_type_string(sentry_metric_type_t type) { @@ -76,41 +65,46 @@ sentry_scope_capture_metric(sentry_scope_t *scope, sentry_metric_type_t type, const char *name, sentry_value_t value, const char *unit, sentry_value_t attributes) { - if (!sdk_is_initialized()) { - sentry_value_decref(value); - sentry_value_decref(attributes); - sentry__scope_free_one_shot(scope); - return SENTRY_METRICS_RESULT_DISABLED; - } - - bool discarded = false; - sentry_value_t metric - = construct_metric(scope, type, name, value, unit, attributes); - sentry__scope_free_one_shot(scope); + bool enable_metrics = false; SENTRY_WITH_OPTIONS (options) { - if (options->before_send_metric_func) { - metric = options->before_send_metric_func( - metric, options->before_send_metric_data); - if (sentry_value_is_null(metric)) { - SENTRY_DEBUG("metric was discarded by the " - "`before_send_metric` hook"); - sentry__client_report_discard(SENTRY_DISCARD_REASON_BEFORE_SEND, - SENTRY_DATA_CATEGORY_TRACE_METRIC, 1); - discarded = true; + if (options->enable_metrics) + enable_metrics = true; + } + if (enable_metrics) { + bool discarded = false; + sentry_value_t metric + = construct_metric(scope, type, name, value, unit, attributes); + sentry__scope_free_one_shot(scope); + SENTRY_WITH_OPTIONS (options) { + if (options->before_send_metric_func) { + metric = options->before_send_metric_func( + metric, options->before_send_metric_data); + if (sentry_value_is_null(metric)) { + SENTRY_DEBUG("metric was discarded by the " + "`before_send_metric` hook"); + sentry__client_report_discard( + SENTRY_DISCARD_REASON_BEFORE_SEND, + SENTRY_DATA_CATEGORY_TRACE_METRIC, 1); + discarded = true; + } } } - } - if (discarded) { - return SENTRY_METRICS_RESULT_DISCARD; - } - sentry_batcher_t *batcher = sentry__batcher_acquire(&g_batcher); - if (!batcher || !sentry__batcher_enqueue(batcher, metric)) { + if (discarded) { + return SENTRY_METRICS_RESULT_DISCARD; + } + sentry_batcher_t *batcher = sentry__batcher_acquire(&g_batcher); + if (!batcher || !sentry__batcher_enqueue(batcher, metric)) { + sentry__batcher_release(batcher); + sentry_value_decref(metric); + return SENTRY_METRICS_RESULT_FAILED; + } sentry__batcher_release(batcher); - sentry_value_decref(metric); - return SENTRY_METRICS_RESULT_FAILED; + return SENTRY_METRICS_RESULT_SUCCESS; } - sentry__batcher_release(batcher); - return SENTRY_METRICS_RESULT_SUCCESS; + sentry_value_decref(value); + sentry_value_decref(attributes); + sentry__scope_free_one_shot(scope); + return SENTRY_METRICS_RESULT_DISABLED; } sentry_metrics_result_t diff --git a/src/sentry_options.c b/src/sentry_options.c index dad8aa13dd..1710ca6e1e 100644 --- a/src/sentry_options.c +++ b/src/sentry_options.c @@ -90,6 +90,8 @@ sentry_options_new(void) opts->propagate_traceparent = false; opts->strict_trace_continuation = false; opts->crashpad_limit_stack_capture_to_sp = false; + opts->enable_metrics = true; + opts->enable_logs = true; opts->cache_keep = SENTRY_CACHE_KEEP_NONE; opts->cache_max_age = 0; opts->cache_max_size = 0; @@ -1018,15 +1020,13 @@ sentry__options_has_integration(const sentry_options_t *opts, const char *name) void sentry_options_set_enable_logs(sentry_options_t *opts, int enable_logs) { - (void)opts; - (void)enable_logs; + opts->enable_logs = !!enable_logs; } int sentry_options_get_enable_logs(const sentry_options_t *opts) { - (void)opts; - return 1; + return opts->enable_logs; } void @@ -1045,15 +1045,13 @@ sentry_options_get_logs_with_attributes(const sentry_options_t *opts) void sentry_options_set_enable_metrics(sentry_options_t *opts, int enable_metrics) { - (void)opts; - (void)enable_metrics; + opts->enable_metrics = !!enable_metrics; } int sentry_options_get_enable_metrics(const sentry_options_t *opts) { - (void)opts; - return 1; + return opts->enable_metrics; } void diff --git a/src/sentry_options.h b/src/sentry_options.h index 9b98a27c5e..dcdfeec729 100644 --- a/src/sentry_options.h +++ b/src/sentry_options.h @@ -81,9 +81,11 @@ struct sentry_options_s { void *traces_sampler_data; char *org_id; size_t max_spans; + bool enable_logs; // takes the first varg as a `sentry_value_t` object containing attributes // if no custom attributes are to be passed, use `sentry_value_new_object()` bool logs_with_attributes; + bool enable_metrics; sentry_before_send_metric_function_t before_send_metric_func; void *before_send_metric_data; bool enable_app_hang_tracking; diff --git a/src/sentry_telemetry.c b/src/sentry_telemetry.c index fe3d49b499..8e0268fac0 100644 --- a/src/sentry_telemetry.c +++ b/src/sentry_telemetry.c @@ -7,13 +7,21 @@ void sentry__telemetry_startup(const sentry_options_t *options) { - sentry__logs_startup(options); - sentry__metrics_startup(options); + if (options->enable_logs) { + sentry__logs_startup(options); + } + if (options->enable_metrics) { + sentry__metrics_startup(options); + } } void sentry__telemetry_shutdown(const sentry_options_t *options) { + if (!options->enable_logs && !options->enable_metrics) { + return; + } + SENTRY_DEBUG("shutting down telemetry"); sentry__logs_shutdown(options->shutdown_timeout); sentry__metrics_shutdown(options->shutdown_timeout); diff --git a/tests/test_integration_client_reports.py b/tests/test_integration_client_reports.py index f9e7bea8c3..5c741a8776 100644 --- a/tests/test_integration_client_reports.py +++ b/tests/test_integration_client_reports.py @@ -212,6 +212,7 @@ def test_client_report_before_send_metric(cmake, httpserver): "sentry_example", [ "log", + "enable-metrics", "discarding-before-send-metric", "capture-metric", "capture-event", diff --git a/tests/test_integration_metrics.py b/tests/test_integration_metrics.py index 185f99db03..91d789163f 100644 --- a/tests/test_integration_metrics.py +++ b/tests/test_integration_metrics.py @@ -222,6 +222,23 @@ def test_before_send_metric_discard(cmake, httpserver): assert len(httpserver.log) == 0 +def test_metrics_disabled(cmake, httpserver): + tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"}) + + httpserver.expect_request("/api/123456/envelope/").respond_with_data("OK") + env = dict(os.environ, SENTRY_DSN=make_dsn(httpserver)) + + run( + tmp_path, + "sentry_example", + ["log", "disable-metrics", "capture-metric"], + env=env, + ) + + # No metrics should be sent when feature is disabled + assert len(httpserver.log) == 0 + + def test_metrics_event(cmake, httpserver): tmp_path = cmake(["sentry_example"], {"SENTRY_BACKEND": "none"}) diff --git a/tests/unit/test_logger.c b/tests/unit/test_logger.c index c372a341eb..b81813bdd8 100644 --- a/tests/unit/test_logger.c +++ b/tests/unit/test_logger.c @@ -1,7 +1,5 @@ #include "sentry_core.h" #include "sentry_logger.h" -#include "sentry_logs.h" -#include "sentry_metrics.h" #include "sentry_sync.h" #include "sentry_testsupport.h" @@ -16,13 +14,6 @@ typedef struct { // To blacklist a test, add to the respective list of `test_unit_transport` // in the `tests/test_unit.py` unit-test runner. -static void -wait_for_telemetry_threads(void) -{ - sentry__logs_wait_for_thread_startup(); - sentry__metrics_wait_for_thread_startup(); -} - static void test_logger( sentry_level_t level, const char *message, va_list args, void *_data) @@ -48,8 +39,11 @@ SENTRY_TEST(custom_logger) SENTRY_TEST_OPTIONS_NEW(options); sentry_options_set_debug(options, true); sentry_options_set_logger(options, test_logger, &data); + SENTRY_TEST_DEPRECATED( + sentry_options_set_enable_metrics(options, false)); + SENTRY_TEST_DEPRECATED(sentry_options_set_enable_logs(options, false)); + sentry_init(options); - wait_for_telemetry_threads(); data.assert_now = true; SENTRY_WARNF("Oh this is %s", "bad"); @@ -75,8 +69,10 @@ SENTRY_TEST(logger_enable_disable_functionality) SENTRY_TEST_OPTIONS_NEW(options); sentry_options_set_debug(options, true); sentry_options_set_logger(options, test_logger, &data); + SENTRY_TEST_DEPRECATED(sentry_options_set_enable_metrics(options, false)); + SENTRY_TEST_DEPRECATED(sentry_options_set_enable_logs(options, false)); + sentry_init(options); - wait_for_telemetry_threads(); // Test logging is enabled by default data.called = 0; @@ -144,8 +140,12 @@ SENTRY_TEST(logger_level) sentry_options_set_debug(options, true); sentry_options_set_logger_level(options, test_cases[i].level); sentry_options_set_logger(options, test_log_level, &data); + SENTRY_TEST_DEPRECATED( + sentry_options_set_enable_metrics(options, false)); + SENTRY_TEST_DEPRECATED( + sentry_options_set_enable_logs(options, false)); + sentry_init(options); - wait_for_telemetry_threads(); data.assert_now = true; // Test all 5 levels in order from most to least verbose diff --git a/tests/unit/test_logs.c b/tests/unit/test_logs.c index 20d3db4b6f..0c122bffce 100644 --- a/tests/unit/test_logs.c +++ b/tests/unit/test_logs.c @@ -53,7 +53,7 @@ SENTRY_TEST(basic_logging_functionality) sentry_init(options); sentry__logs_wait_for_thread_startup(); - // These should not crash. + // These should not crash and should respect the enable_logs option TEST_CHECK_INT_EQUAL(sentry_log_trace("Trace message"), 0); TEST_CHECK_INT_EQUAL(sentry_log_debug("Debug message"), 0); TEST_CHECK_INT_EQUAL(sentry_log_info("Info message"), 0); @@ -75,6 +75,31 @@ SENTRY_TEST(basic_logging_functionality) TEST_CHECK_INT_EQUAL(validation_data.called_count, 2); } +SENTRY_TEST(logs_disabled) +{ + transport_validation_data_t validation_data = { 0, false }; + + SENTRY_TEST_OPTIONS_NEW(options); + sentry_options_set_dsn(options, "https://foo@sentry.invalid/42"); + SENTRY_TEST_DEPRECATED(sentry_options_set_enable_logs(options, false)); + + sentry_transport_t *transport + = sentry_transport_new(validate_logs_envelope); + sentry_transport_set_state(transport, &validation_data); + sentry_options_set_transport(options, transport); + + sentry_init(options); + + // Should return DISABLED since logs were explicitly disabled + TEST_CHECK_INT_EQUAL(sentry_log_info("This should not be sent"), 3); + + sentry_close(); + + // Transport should not be called since logs were explicitly disabled + TEST_CHECK(!validation_data.has_validation_error); + TEST_CHECK_INT_EQUAL(validation_data.called_count, 0); +} + SENTRY_TEST(formatted_log_messages) { transport_validation_data_t validation_data = { 0, false }; @@ -313,7 +338,7 @@ SENTRY_TEST(logs_force_flush) sentry_init(options); sentry__logs_wait_for_thread_startup(); - // These should not crash. + // These should not crash and should respect the enable_logs option TEST_CHECK_INT_EQUAL(sentry_log_trace("Trace message"), 0); sentry_flush(5000); TEST_CHECK_INT_EQUAL(sentry_log_debug("Debug message"), 0); @@ -531,14 +556,22 @@ SENTRY_TEST(logs_span_trace_attributes) sentry_close(); } -SENTRY_TEST(logs_uninitialized) +SENTRY_TEST(logs_plain_string_disabled) { + SENTRY_TEST_OPTIONS_NEW(options); + sentry_options_set_dsn(options, "https://foo@sentry.invalid/42"); + SENTRY_TEST_DEPRECATED(sentry_options_set_enable_logs(options, false)); + + sentry_init(options); + + // Should not leak the attributes value sentry_value_t attrs = sentry_value_new_object(); sentry_value_set_by_key(attrs, "key", sentry_value_new_attribute(sentry_value_new_string("val"), NULL)); - TEST_CHECK_INT_EQUAL(sentry_log(SENTRY_LEVEL_INFO, "test", attrs), SENTRY_LOG_RETURN_DISABLED); + + sentry_close(); } SENTRY_TEST(logs_global_attribute_no_field_leak) diff --git a/tests/unit/test_metrics.c b/tests/unit/test_metrics.c index a7a32225db..2cd625c4a1 100644 --- a/tests/unit/test_metrics.c +++ b/tests/unit/test_metrics.c @@ -262,8 +262,22 @@ SENTRY_TEST(metrics_before_send_modify) TEST_CHECK_INT_EQUAL(validation_data.called_count, 1); } -SENTRY_TEST(metrics_uninitialized) +SENTRY_TEST(metrics_disabled) { + transport_validation_data_t validation_data = { 0, false }; + + SENTRY_TEST_OPTIONS_NEW(options); + sentry_options_set_dsn(options, "https://foo@sentry.invalid/42"); + SENTRY_TEST_DEPRECATED(sentry_options_set_enable_metrics(options, false)); + + sentry_transport_t *transport + = sentry_transport_new(validate_metrics_envelope); + sentry_transport_set_state(transport, &validation_data); + sentry_options_set_transport(options, transport); + + sentry_init(options); + + // These should return DISABLED since metrics were explicitly disabled TEST_CHECK_INT_EQUAL( sentry_metrics_count("test.counter", 1, sentry_value_new_null()), SENTRY_METRICS_RESULT_DISABLED); @@ -273,6 +287,12 @@ SENTRY_TEST(metrics_uninitialized) TEST_CHECK_INT_EQUAL(sentry_metrics_distribution("test.distribution", 123.0, NULL, sentry_value_new_null()), SENTRY_METRICS_RESULT_DISABLED); + + sentry_close(); + + // Transport should not be called since metrics were explicitly disabled + TEST_CHECK(!validation_data.has_validation_error); + TEST_CHECK_INT_EQUAL(validation_data.called_count, 0); } SENTRY_TEST(metrics_force_flush) @@ -571,6 +591,8 @@ SENTRY_TEST(metrics_reinit) // This will deadlock if sentry__batcher_flush holds g_options_lock. SENTRY_TEST_OPTIONS_NEW(options2); sentry_options_set_dsn(options2, "https://foo@sentry.invalid/42"); + SENTRY_TEST_DEPRECATED(sentry_options_set_enable_metrics(options2, true)); + sentry_init(options2); sentry_close(); } diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index ab3edc2027..78827091a6 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -195,6 +195,7 @@ XX(logger_enable_disable_functionality) XX(logger_level) XX(logs_custom_attributes_not_modified) XX(logs_custom_attributes_with_format_strings) +XX(logs_disabled) XX(logs_force_flush) XX(logs_global_attribute_no_field_leak) XX(logs_param_conversion) @@ -202,10 +203,10 @@ XX(logs_param_sign) XX(logs_param_types) XX(logs_param_width) XX(logs_plain_string) +XX(logs_plain_string_disabled) XX(logs_reinit) XX(logs_reinit_stress) XX(logs_span_trace_attributes) -XX(logs_uninitialized) XX(m128a_size) XX(message_with_null_text_is_valid) XX(metrics_batch) @@ -213,13 +214,13 @@ XX(metrics_before_send_discard) XX(metrics_before_send_modify) XX(metrics_count) XX(metrics_default_attributes) +XX(metrics_disabled) XX(metrics_distribution) XX(metrics_force_flush) XX(metrics_gauge) XX(metrics_global_attribute_no_field_leak) XX(metrics_reinit) XX(metrics_reinit_stress) -XX(metrics_uninitialized) XX(metrics_with_attributes) XX(minidump_context_flags) XX(minidump_context_sizes)