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
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,30 @@ namespace logs
/**
* Struct to hold batch LogRecordProcessor options.
*/
struct BatchLogRecordProcessorOptions
struct OPENTELEMETRY_EXPORT BatchLogRecordProcessorOptions
{
BatchLogRecordProcessorOptions();
/**
* The maximum buffer/queue size. After the size is reached, spans are
* dropped.
*/
size_t max_queue_size = 2048;
size_t max_queue_size;

/* The time interval between two consecutive exports. */
std::chrono::milliseconds schedule_delay_millis = std::chrono::milliseconds(5000);
std::chrono::milliseconds schedule_delay_millis;

/**
* It is the time duration of how long the export can run before it is cancelled
* It is not currently used by the SDK and the parameter is ignored
* TODO: Implement the parameter in BatchLogRecordProcessor
*/
std::chrono::milliseconds export_timeout_millis;

/**
* The maximum batch size of every export. It must be smaller or
* equal to max_queue_size.
*/
size_t max_export_batch_size = 512;
size_t max_export_batch_size;
};

} // namespace logs
Expand Down
1 change: 1 addition & 0 deletions sdk/src/logs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_library(
simple_log_record_processor.cc
simple_log_record_processor_factory.cc
batch_log_record_processor.cc
batch_log_record_processor_options.cc
batch_log_record_processor_factory.cc
logger_config.cc
logger_context.cc
Expand Down
69 changes: 69 additions & 0 deletions sdk/src/logs/batch_log_record_processor_options.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <stddef.h>
#include <chrono>
#include <cstdint>

#include "opentelemetry/sdk/common/env_variables.h"
#include "opentelemetry/sdk/logs/batch_log_record_processor_options.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace logs
{

constexpr const char *kMaxQueueSizeEnv = "OTEL_BLRP_MAX_QUEUE_SIZE";
constexpr const char *kScheduleDelayEnv = "OTEL_BLRP_SCHEDULE_DELAY";
constexpr const char *kExportTimeoutEnv = "OTEL_BLRP_EXPORT_TIMEOUT";
constexpr const char *kMaxExportBatchSizeEnv = "OTEL_BLRP_MAX_EXPORT_BATCH_SIZE";

const size_t kDefaultMaxQueueSize = 2048;
const std::chrono::milliseconds kDefaultScheduleDelayMillis = std::chrono::milliseconds(1000);
const std::chrono::milliseconds kDefaultExportTimeout = std::chrono::milliseconds(30000);
const size_t kDefaultMaxExportBatchSize = 512;

inline size_t GetMaxQueueSizeFromEnv()
{
std::uint32_t value;
if (!opentelemetry::sdk::common::GetUintEnvironmentVariable(kMaxQueueSizeEnv, value))
{
return kDefaultMaxQueueSize;
}
return static_cast<size_t>(value);
}

inline std::chrono::milliseconds GetDurationFromEnv(
const char *env_var,
const std::chrono::milliseconds &default_duration)
{
std::chrono::system_clock::duration duration{0};
if (!opentelemetry::sdk::common::GetDurationEnvironmentVariable(env_var, duration))
{
return default_duration;
}
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
}

inline size_t GetMaxExportBatchSizeFromEnv()
{
std::uint32_t value;
if (!opentelemetry::sdk::common::GetUintEnvironmentVariable(kMaxExportBatchSizeEnv, value))
{
return kDefaultMaxExportBatchSize;
}
return static_cast<size_t>(value);
}

BatchLogRecordProcessorOptions::BatchLogRecordProcessorOptions()
: max_queue_size(GetMaxQueueSizeFromEnv()),
schedule_delay_millis(GetDurationFromEnv(kScheduleDelayEnv, kDefaultScheduleDelayMillis)),
export_timeout_millis(GetDurationFromEnv(kExportTimeoutEnv, kDefaultExportTimeout)),
max_export_batch_size(GetMaxExportBatchSizeFromEnv())
{}

} // namespace logs
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
82 changes: 82 additions & 0 deletions sdk/test/logs/batch_log_record_processor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <gtest/gtest.h>
#include <stdint.h>
#include <stdlib.h>
#include <atomic>
#include <chrono>
#include <cstddef>
Expand All @@ -21,10 +22,17 @@
#include "opentelemetry/nostd/variant.h"
#include "opentelemetry/sdk/common/exporter_utils.h"
#include "opentelemetry/sdk/logs/batch_log_record_processor.h"
#include "opentelemetry/sdk/logs/batch_log_record_processor_options.h"
#include "opentelemetry/sdk/logs/exporter.h"
#include "opentelemetry/sdk/logs/processor.h"
#include "opentelemetry/sdk/logs/recordable.h"

#if defined(_MSC_VER)
# include "opentelemetry/sdk/common/env_variables.h"
using opentelemetry::sdk::common::setenv;
using opentelemetry::sdk::common::unsetenv;
#endif

using namespace opentelemetry::sdk::logs;
using namespace opentelemetry::sdk::common;

Expand Down Expand Up @@ -355,3 +363,77 @@ TEST_F(BatchLogRecordProcessorTest, TestScheduledDelayMillis)
EXPECT_EQ("Log" + std::to_string(i), logs_received->at(i)->GetBody());
}
}

TEST_F(BatchLogRecordProcessorTest, TestDefaultValues)
{
BatchLogRecordProcessorOptions options;

EXPECT_EQ(options.max_queue_size, static_cast<size_t>(2048));
EXPECT_EQ(options.schedule_delay_millis, std::chrono::milliseconds(1000));
EXPECT_EQ(options.export_timeout_millis, std::chrono::milliseconds(30000));
EXPECT_EQ(options.max_export_batch_size, static_cast<size_t>(512));
}

TEST_F(BatchLogRecordProcessorTest, TestMaxQueueSizeFromEnv)
{
setenv("OTEL_BLRP_MAX_QUEUE_SIZE", "1234", 1);

BatchLogRecordProcessorOptions options;

EXPECT_EQ(options.max_queue_size, static_cast<size_t>(1234));

unsetenv("OTEL_BLRP_MAX_QUEUE_SIZE");
}

TEST_F(BatchLogRecordProcessorTest, TestScheduleDelayFromEnv)
{
setenv("OTEL_BLRP_SCHEDULE_DELAY", "7s", 1);

BatchLogRecordProcessorOptions options;

EXPECT_EQ(options.schedule_delay_millis, std::chrono::milliseconds(7000));

unsetenv("OTEL_BLRP_SCHEDULE_DELAY");
}

TEST_F(BatchLogRecordProcessorTest, TestExportTimeoutFromEnv)
{
setenv("OTEL_BLRP_EXPORT_TIMEOUT", "250ms", 1);

BatchLogRecordProcessorOptions options;

EXPECT_EQ(options.export_timeout_millis, std::chrono::milliseconds(250));

unsetenv("OTEL_BLRP_EXPORT_TIMEOUT");
}

TEST_F(BatchLogRecordProcessorTest, TestMaxExportBatchSizeFromEnv)
{
setenv("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE", "42", 1);

BatchLogRecordProcessorOptions options;

EXPECT_EQ(options.max_export_batch_size, static_cast<size_t>(42));

unsetenv("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE");
}

TEST_F(BatchLogRecordProcessorTest, TestOptionsReadFromMultipleEnvVars)
{
setenv("OTEL_BLRP_MAX_QUEUE_SIZE", "3000", 1);
setenv("OTEL_BLRP_SCHEDULE_DELAY", "2s", 1);
setenv("OTEL_BLRP_EXPORT_TIMEOUT", "1s", 1);
setenv("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE", "256", 1);

BatchLogRecordProcessorOptions options;

EXPECT_EQ(options.max_queue_size, static_cast<size_t>(3000));
EXPECT_EQ(options.schedule_delay_millis, std::chrono::milliseconds(2000));
EXPECT_EQ(options.export_timeout_millis, std::chrono::milliseconds(1000));
EXPECT_EQ(options.max_export_batch_size, static_cast<size_t>(256));

unsetenv("OTEL_BLRP_MAX_QUEUE_SIZE");
unsetenv("OTEL_BLRP_SCHEDULE_DELAY");
unsetenv("OTEL_BLRP_EXPORT_TIMEOUT");
unsetenv("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE");
}
Loading