Skip to content

Commit f81224d

Browse files
[sdk] BatchLogRecordProcessorOptions from env variables (open-telemetry#3687)
1 parent eb659f5 commit f81224d

File tree

4 files changed

+164
-4
lines changed

4 files changed

+164
-4
lines changed

sdk/include/opentelemetry/sdk/logs/batch_log_record_processor_options.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,30 @@ namespace logs
1818
/**
1919
* Struct to hold batch LogRecordProcessor options.
2020
*/
21-
struct BatchLogRecordProcessorOptions
21+
struct OPENTELEMETRY_EXPORT BatchLogRecordProcessorOptions
2222
{
23+
BatchLogRecordProcessorOptions();
2324
/**
2425
* The maximum buffer/queue size. After the size is reached, spans are
2526
* dropped.
2627
*/
27-
size_t max_queue_size = 2048;
28+
size_t max_queue_size;
2829

2930
/* The time interval between two consecutive exports. */
30-
std::chrono::milliseconds schedule_delay_millis = std::chrono::milliseconds(5000);
31+
std::chrono::milliseconds schedule_delay_millis;
32+
33+
/**
34+
* It is the time duration of how long the export can run before it is cancelled
35+
* It is not currently used by the SDK and the parameter is ignored
36+
* TODO: Implement the parameter in BatchLogRecordProcessor
37+
*/
38+
std::chrono::milliseconds export_timeout_millis;
3139

3240
/**
3341
* The maximum batch size of every export. It must be smaller or
3442
* equal to max_queue_size.
3543
*/
36-
size_t max_export_batch_size = 512;
44+
size_t max_export_batch_size;
3745
};
3846

3947
} // namespace logs

sdk/src/logs/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_library(
1414
simple_log_record_processor.cc
1515
simple_log_record_processor_factory.cc
1616
batch_log_record_processor.cc
17+
batch_log_record_processor_options.cc
1718
batch_log_record_processor_factory.cc
1819
logger_config.cc
1920
logger_context.cc
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include <stddef.h>
5+
#include <chrono>
6+
#include <cstdint>
7+
8+
#include "opentelemetry/sdk/common/env_variables.h"
9+
#include "opentelemetry/sdk/logs/batch_log_record_processor_options.h"
10+
#include "opentelemetry/version.h"
11+
12+
OPENTELEMETRY_BEGIN_NAMESPACE
13+
namespace sdk
14+
{
15+
namespace logs
16+
{
17+
18+
constexpr const char *kMaxQueueSizeEnv = "OTEL_BLRP_MAX_QUEUE_SIZE";
19+
constexpr const char *kScheduleDelayEnv = "OTEL_BLRP_SCHEDULE_DELAY";
20+
constexpr const char *kExportTimeoutEnv = "OTEL_BLRP_EXPORT_TIMEOUT";
21+
constexpr const char *kMaxExportBatchSizeEnv = "OTEL_BLRP_MAX_EXPORT_BATCH_SIZE";
22+
23+
const size_t kDefaultMaxQueueSize = 2048;
24+
const std::chrono::milliseconds kDefaultScheduleDelayMillis = std::chrono::milliseconds(1000);
25+
const std::chrono::milliseconds kDefaultExportTimeout = std::chrono::milliseconds(30000);
26+
const size_t kDefaultMaxExportBatchSize = 512;
27+
28+
inline size_t GetMaxQueueSizeFromEnv()
29+
{
30+
std::uint32_t value;
31+
if (!opentelemetry::sdk::common::GetUintEnvironmentVariable(kMaxQueueSizeEnv, value))
32+
{
33+
return kDefaultMaxQueueSize;
34+
}
35+
return static_cast<size_t>(value);
36+
}
37+
38+
inline std::chrono::milliseconds GetDurationFromEnv(
39+
const char *env_var,
40+
const std::chrono::milliseconds &default_duration)
41+
{
42+
std::chrono::system_clock::duration duration{0};
43+
if (!opentelemetry::sdk::common::GetDurationEnvironmentVariable(env_var, duration))
44+
{
45+
return default_duration;
46+
}
47+
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
48+
}
49+
50+
inline size_t GetMaxExportBatchSizeFromEnv()
51+
{
52+
std::uint32_t value;
53+
if (!opentelemetry::sdk::common::GetUintEnvironmentVariable(kMaxExportBatchSizeEnv, value))
54+
{
55+
return kDefaultMaxExportBatchSize;
56+
}
57+
return static_cast<size_t>(value);
58+
}
59+
60+
BatchLogRecordProcessorOptions::BatchLogRecordProcessorOptions()
61+
: max_queue_size(GetMaxQueueSizeFromEnv()),
62+
schedule_delay_millis(GetDurationFromEnv(kScheduleDelayEnv, kDefaultScheduleDelayMillis)),
63+
export_timeout_millis(GetDurationFromEnv(kExportTimeoutEnv, kDefaultExportTimeout)),
64+
max_export_batch_size(GetMaxExportBatchSizeFromEnv())
65+
{}
66+
67+
} // namespace logs
68+
} // namespace sdk
69+
OPENTELEMETRY_END_NAMESPACE

sdk/test/logs/batch_log_record_processor_test.cc

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <gtest/gtest.h>
55
#include <stdint.h>
6+
#include <stdlib.h>
67
#include <atomic>
78
#include <chrono>
89
#include <cstddef>
@@ -21,10 +22,17 @@
2122
#include "opentelemetry/nostd/variant.h"
2223
#include "opentelemetry/sdk/common/exporter_utils.h"
2324
#include "opentelemetry/sdk/logs/batch_log_record_processor.h"
25+
#include "opentelemetry/sdk/logs/batch_log_record_processor_options.h"
2426
#include "opentelemetry/sdk/logs/exporter.h"
2527
#include "opentelemetry/sdk/logs/processor.h"
2628
#include "opentelemetry/sdk/logs/recordable.h"
2729

30+
#if defined(_MSC_VER)
31+
# include "opentelemetry/sdk/common/env_variables.h"
32+
using opentelemetry::sdk::common::setenv;
33+
using opentelemetry::sdk::common::unsetenv;
34+
#endif
35+
2836
using namespace opentelemetry::sdk::logs;
2937
using namespace opentelemetry::sdk::common;
3038

@@ -355,3 +363,77 @@ TEST_F(BatchLogRecordProcessorTest, TestScheduledDelayMillis)
355363
EXPECT_EQ("Log" + std::to_string(i), logs_received->at(i)->GetBody());
356364
}
357365
}
366+
367+
TEST_F(BatchLogRecordProcessorTest, TestDefaultValues)
368+
{
369+
BatchLogRecordProcessorOptions options;
370+
371+
EXPECT_EQ(options.max_queue_size, static_cast<size_t>(2048));
372+
EXPECT_EQ(options.schedule_delay_millis, std::chrono::milliseconds(1000));
373+
EXPECT_EQ(options.export_timeout_millis, std::chrono::milliseconds(30000));
374+
EXPECT_EQ(options.max_export_batch_size, static_cast<size_t>(512));
375+
}
376+
377+
TEST_F(BatchLogRecordProcessorTest, TestMaxQueueSizeFromEnv)
378+
{
379+
setenv("OTEL_BLRP_MAX_QUEUE_SIZE", "1234", 1);
380+
381+
BatchLogRecordProcessorOptions options;
382+
383+
EXPECT_EQ(options.max_queue_size, static_cast<size_t>(1234));
384+
385+
unsetenv("OTEL_BLRP_MAX_QUEUE_SIZE");
386+
}
387+
388+
TEST_F(BatchLogRecordProcessorTest, TestScheduleDelayFromEnv)
389+
{
390+
setenv("OTEL_BLRP_SCHEDULE_DELAY", "7s", 1);
391+
392+
BatchLogRecordProcessorOptions options;
393+
394+
EXPECT_EQ(options.schedule_delay_millis, std::chrono::milliseconds(7000));
395+
396+
unsetenv("OTEL_BLRP_SCHEDULE_DELAY");
397+
}
398+
399+
TEST_F(BatchLogRecordProcessorTest, TestExportTimeoutFromEnv)
400+
{
401+
setenv("OTEL_BLRP_EXPORT_TIMEOUT", "250ms", 1);
402+
403+
BatchLogRecordProcessorOptions options;
404+
405+
EXPECT_EQ(options.export_timeout_millis, std::chrono::milliseconds(250));
406+
407+
unsetenv("OTEL_BLRP_EXPORT_TIMEOUT");
408+
}
409+
410+
TEST_F(BatchLogRecordProcessorTest, TestMaxExportBatchSizeFromEnv)
411+
{
412+
setenv("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE", "42", 1);
413+
414+
BatchLogRecordProcessorOptions options;
415+
416+
EXPECT_EQ(options.max_export_batch_size, static_cast<size_t>(42));
417+
418+
unsetenv("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE");
419+
}
420+
421+
TEST_F(BatchLogRecordProcessorTest, TestOptionsReadFromMultipleEnvVars)
422+
{
423+
setenv("OTEL_BLRP_MAX_QUEUE_SIZE", "3000", 1);
424+
setenv("OTEL_BLRP_SCHEDULE_DELAY", "2s", 1);
425+
setenv("OTEL_BLRP_EXPORT_TIMEOUT", "1s", 1);
426+
setenv("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE", "256", 1);
427+
428+
BatchLogRecordProcessorOptions options;
429+
430+
EXPECT_EQ(options.max_queue_size, static_cast<size_t>(3000));
431+
EXPECT_EQ(options.schedule_delay_millis, std::chrono::milliseconds(2000));
432+
EXPECT_EQ(options.export_timeout_millis, std::chrono::milliseconds(1000));
433+
EXPECT_EQ(options.max_export_batch_size, static_cast<size_t>(256));
434+
435+
unsetenv("OTEL_BLRP_MAX_QUEUE_SIZE");
436+
unsetenv("OTEL_BLRP_SCHEDULE_DELAY");
437+
unsetenv("OTEL_BLRP_EXPORT_TIMEOUT");
438+
unsetenv("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE");
439+
}

0 commit comments

Comments
 (0)