From aebe67f8b515e5263aac42f1d6cb34a35aa500ec Mon Sep 17 00:00:00 2001 From: syedowais312 Date: Sat, 7 Nov 2026 00:16:40 +0530 Subject: [PATCH 1/3] fix(logs): clamp export batch size to queue size --- CHANGELOG.md | 4 + .../logs/batch_log_record_processor_options.h | 4 +- sdk/src/logs/batch_log_record_processor.cc | 28 ++++++- sdk/test/logs/BUILD | 1 + sdk/test/logs/CMakeLists.txt | 3 +- .../logs/batch_log_record_processor_test.cc | 78 +++++++++++++++++++ 6 files changed, 112 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0bde603a7..ce215b2a93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ Increment the: ## [Unreleased] +* [SDK] Clamp `BatchLogRecordProcessor` max_export_batch_size to non-zero + max_queue_size, with a warning, matching `BatchSpanProcessor`. + [#4750](https://github.com/open-telemetry/opentelemetry-cpp/issues/4750) + * [API/SDK] Add `Enabled()` to synchronous metric instruments (ABI v2). It returns `false` when the Meter is disabled or every matching view uses the Drop aggregation 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..7d0519f8f5 100644 --- a/sdk/test/logs/batch_log_record_processor_test.cc +++ b/sdk/test/logs/batch_log_record_processor_test.cc @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include +#include #include #include #include @@ -22,11 +23,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 +320,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 From 5626cd3075c905b705d3b1c7802168afe36202ce Mon Sep 17 00:00:00 2001 From: syedowais312 Date: Sat, 7 Nov 2026 02:08:18 +0530 Subject: [PATCH 2/3] fix(logs): include initializer_list in batch processor tests --- sdk/test/logs/batch_log_record_processor_test.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/test/logs/batch_log_record_processor_test.cc b/sdk/test/logs/batch_log_record_processor_test.cc index 7d0519f8f5..f3ee3481b4 100644 --- a/sdk/test/logs/batch_log_record_processor_test.cc +++ b/sdk/test/logs/batch_log_record_processor_test.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include From d94b0c19fe5ccc9f9281608361c1d8acec0b7d07 Mon Sep 17 00:00:00 2001 From: syedowais312 Date: Sat, 7 Nov 2026 12:56:44 +0530 Subject: [PATCH 3/3] docs: link changelog entry to PR #4768 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce215b2a93..3c2ade0eb9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ Increment the: * [SDK] Clamp `BatchLogRecordProcessor` max_export_batch_size to non-zero max_queue_size, with a warning, matching `BatchSpanProcessor`. - [#4750](https://github.com/open-telemetry/opentelemetry-cpp/issues/4750) + [#4768](https://github.com/open-telemetry/opentelemetry-cpp/pull/4768) * [API/SDK] Add `Enabled()` to synchronous metric instruments (ABI v2). It returns `false` when the Meter is disabled or every matching view uses the