Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 25 additions & 3 deletions sdk/src/logs/batch_log_record_processor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cstdint>
#include <memory>
#include <mutex>
#include <ostream>
#include <thread>
#include <utility>
#include <vector>
Expand All @@ -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"
Expand All @@ -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<LogRecordExporter> &&exporter,
const size_t max_queue_size,
Expand All @@ -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<SynchronizationData>()),
worker_thread_instrumentation_(nullptr),
Expand All @@ -60,7 +80,8 @@ BatchLogRecordProcessor::BatchLogRecordProcessor(std::unique_ptr<LogRecordExport
: 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<SynchronizationData>()),
worker_thread_instrumentation_(nullptr),
Expand All @@ -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<SynchronizationData>()),
worker_thread_instrumentation_(runtime_options.thread_instrumentation),
Expand Down
1 change: 1 addition & 0 deletions sdk/test/logs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ cc_test(
],
deps = [
"//sdk/src/logs",
"//test_common:headers",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
3 changes: 2 additions & 1 deletion sdk/test/logs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
79 changes: 79 additions & 0 deletions sdk/test/logs/batch_log_record_processor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// SPDX-License-Identifier: Apache-2.0

#include <gtest/gtest.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <initializer_list>
#include <memory>
#include <mutex>
#include <new>
Expand All @@ -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"
Expand Down Expand Up @@ -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<int>
{
protected:
std::unique_ptr<BatchLogRecordProcessor> MakeProcessor(size_t max_queue_size,
size_t max_export_batch_size)
{
auto exporter = std::make_unique<BlockingMockLogExporter>(
std::make_shared<std::vector<std::size_t>>(), std::make_shared<std::atomic<std::size_t>>(0),
std::make_shared<std::atomic<bool>>(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<BatchLogRecordProcessor>(std::move(exporter), max_queue_size,
options.schedule_delay_millis,
max_export_batch_size);
case 1:
return std::make_unique<BatchLogRecordProcessor>(std::move(exporter), options);
default:
return std::make_unique<BatchLogRecordProcessor>(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
Expand Down
Loading