diff --git a/CHANGELOG.md b/CHANGELOG.md index 45c91970d6..961faa2de0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,10 @@ Increment the: schedule delay. [#4741](https://github.com/open-telemetry/opentelemetry-cpp/pull/4741) +* [SDK] Clamp `BatchLogRecordProcessor` max_export_batch_size to non-zero + max_queue_size, with a warning, matching `BatchSpanProcessor`. + [#4768](https://github.com/open-telemetry/opentelemetry-cpp/pull/4768) + ## [1.30.0] 2026-10-22 This release contains: diff --git a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h index 8533527ff5..8da9d8c2ff 100644 --- a/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h +++ b/sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h @@ -75,8 +75,8 @@ struct OPENTELEMETRY_EXPORT BatchLogRecordProcessorOptions batch_log_record_processor_options_env::GetExportTimeoutFromEnv(); /** - * The maximum batch size of every export. It must be smaller or - * equal to max_queue_size. + * The maximum batch size of every export. Values larger than a non-zero + * max_queue_size are clamped to it, with a warning. */ size_t max_export_batch_size = batch_log_record_processor_options_env::GetMaxExportBatchSizeFromEnv(); diff --git a/sdk/src/logs/batch_log_record_processor.cc b/sdk/src/logs/batch_log_record_processor.cc index 4bb5c6affa..e810a4bc48 100644 --- a/sdk/src/logs/batch_log_record_processor.cc +++ b/sdk/src/logs/batch_log_record_processor.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -18,6 +19,7 @@ #include "opentelemetry/sdk/common/atomic_unique_ptr.h" #include "opentelemetry/sdk/common/circular_buffer.h" #include "opentelemetry/sdk/common/circular_buffer_range.h" +#include "opentelemetry/sdk/common/global_log_handler.h" #include "opentelemetry/sdk/logs/batch_log_record_processor.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" @@ -37,6 +39,24 @@ namespace sdk { namespace logs { +namespace +{ + +size_t ClampMaxExportBatchSize(size_t max_export_batch_size, size_t max_queue_size) +{ + // Clamping to a queue of 0 would give a batch of 0, which makes the worker spin. + if (max_queue_size > 0 && max_export_batch_size > max_queue_size) + { + OTEL_INTERNAL_LOG_WARN("[BatchLogRecordProcessor] max_export_batch_size (" + << max_export_batch_size << ") is larger than max_queue_size (" + << max_queue_size << "), using " << max_queue_size << "."); + return max_queue_size; + } + return max_export_batch_size; +} + +} // namespace + BatchLogRecordProcessor::BatchLogRecordProcessor( std::unique_ptr &&exporter, const size_t max_queue_size, @@ -45,7 +65,7 @@ BatchLogRecordProcessor::BatchLogRecordProcessor( : exporter_(std::move(exporter)), max_queue_size_(max_queue_size), scheduled_delay_millis_(scheduled_delay_millis), - max_export_batch_size_(max_export_batch_size), + max_export_batch_size_(ClampMaxExportBatchSize(max_export_batch_size, max_queue_size)), buffer_(max_queue_size_), synchronization_data_(std::make_shared()), worker_thread_instrumentation_(nullptr), @@ -60,7 +80,8 @@ BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr()), worker_thread_instrumentation_(nullptr), @@ -77,7 +98,8 @@ BatchLogRecordProcessor::BatchLogRecordProcessor( : exporter_(std::move(exporter)), max_queue_size_(options.max_queue_size), scheduled_delay_millis_(options.schedule_delay_millis), - max_export_batch_size_(options.max_export_batch_size), + max_export_batch_size_( + ClampMaxExportBatchSize(options.max_export_batch_size, options.max_queue_size)), buffer_(options.max_queue_size), synchronization_data_(std::make_shared()), worker_thread_instrumentation_(runtime_options.thread_instrumentation), diff --git a/sdk/test/logs/BUILD b/sdk/test/logs/BUILD index 7cc06ae988..3ecbfd4c83 100644 --- a/sdk/test/logs/BUILD +++ b/sdk/test/logs/BUILD @@ -124,6 +124,7 @@ cc_test( ], deps = [ "//sdk/src/logs", + "//test_common:headers", "@com_google_googletest//:gtest_main", ], ) diff --git a/sdk/test/logs/CMakeLists.txt b/sdk/test/logs/CMakeLists.txt index f6ae31d076..26840570dc 100644 --- a/sdk/test/logs/CMakeLists.txt +++ b/sdk/test/logs/CMakeLists.txt @@ -17,7 +17,8 @@ foreach( target_link_libraries( ${testname} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} opentelemetry_logs opentelemetry_trace) - if(testname STREQUAL "logger_provider_sdk_test") + if(testname STREQUAL "logger_provider_sdk_test" + OR testname STREQUAL "batch_log_record_processor_test") target_link_libraries(${testname} opentelemetry_test_common) endif() gtest_add_tests( diff --git a/sdk/test/logs/batch_log_record_processor_test.cc b/sdk/test/logs/batch_log_record_processor_test.cc index fb804459f4..f3ee3481b4 100644 --- a/sdk/test/logs/batch_log_record_processor_test.cc +++ b/sdk/test/logs/batch_log_record_processor_test.cc @@ -2,12 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -22,11 +24,14 @@ #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/nostd/variant.h" #include "opentelemetry/sdk/common/exporter_utils.h" +#include "opentelemetry/sdk/common/global_log_handler.h" #include "opentelemetry/sdk/logs/batch_log_record_processor.h" #include "opentelemetry/sdk/logs/batch_log_record_processor_options.h" +#include "opentelemetry/sdk/logs/batch_log_record_processor_runtime_options.h" #include "opentelemetry/sdk/logs/exporter.h" #include "opentelemetry/sdk/logs/processor.h" #include "opentelemetry/sdk/logs/recordable.h" +#include "opentelemetry/test_common/sdk/common/scoped_test_log_handler.h" #if defined(_MSC_VER) # include "opentelemetry/sdk/common/env_variables.h" @@ -316,6 +321,80 @@ class BatchLogRecordProcessorTest : public testing::Test // ::testing::Test } }; +#if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_WARN +class BatchLogRecordProcessorClampTest : public testing::TestWithParam +{ +protected: + std::unique_ptr MakeProcessor(size_t max_queue_size, + size_t max_export_batch_size) + { + auto exporter = std::make_unique( + std::make_shared>(), std::make_shared>(0), + std::make_shared>(false)); + + BatchLogRecordProcessorOptions options; + options.max_queue_size = max_queue_size; + options.max_export_batch_size = max_export_batch_size; + options.schedule_delay_millis = std::chrono::milliseconds(10000); + + switch (GetParam()) + { + case 0: + return std::make_unique(std::move(exporter), max_queue_size, + options.schedule_delay_millis, + max_export_batch_size); + case 1: + return std::make_unique(std::move(exporter), options); + default: + return std::make_unique(std::move(exporter), options, + BatchLogRecordProcessorRuntimeOptions()); + } + } +}; + +TEST_P(BatchLogRecordProcessorClampTest, TestMaxExportBatchSizeClampLogsWarningOnce) +{ + opentelemetry::test_common::ScopedTestLogHandler log_handler(internal_log::LogLevel::Warning); + auto batch_processor = MakeProcessor(256, 512); + EXPECT_TRUE(batch_processor->Shutdown()); + + const auto entries = log_handler.Drain(); + EXPECT_EQ(1u, entries.size()); + EXPECT_EQ(1, + std::count_if(entries.begin(), entries.end(), + [](const opentelemetry::test_common::ScopedTestLogHandler::Entry &entry) { + return entry.level == internal_log::LogLevel::Warning && + entry.msg == + "[BatchLogRecordProcessor] max_export_batch_size (512) is " + "larger than max_queue_size (256), using 256."; + })); +} + +TEST_P(BatchLogRecordProcessorClampTest, TestValidMaxExportBatchSizeDoesNotLogWarning) +{ + for (size_t batch_size : {128u, 256u}) + { + SCOPED_TRACE(batch_size); + opentelemetry::test_common::ScopedTestLogHandler log_handler(internal_log::LogLevel::Warning); + auto batch_processor = MakeProcessor(256, batch_size); + EXPECT_TRUE(batch_processor->Shutdown()); + EXPECT_TRUE(log_handler.Drain().empty()); + } +} + +TEST_P(BatchLogRecordProcessorClampTest, TestZeroMaxQueueSizeDoesNotLogWarning) +{ + opentelemetry::test_common::ScopedTestLogHandler log_handler(internal_log::LogLevel::Warning); + auto batch_processor = MakeProcessor(0, 512); + EXPECT_TRUE(batch_processor->Shutdown()); + EXPECT_TRUE(log_handler.Drain().empty()); +} + +INSTANTIATE_TEST_SUITE_P(ConstructorOverloads, + BatchLogRecordProcessorClampTest, + testing::Values(0, 1, 2)); +#endif + TEST_F(BatchLogRecordProcessorTest, TestShutdown) { // initialize a batch log processor with the test exporter