Skip to content

Commit 179e4b0

Browse files
Copilotlalitb
andauthored
[SDK] Fix BatchSpanProcessorOptions and BatchLogRecordProcessorOptions to restore C++20 aggregate initialization support (#3780)
* Initial plan * Fix BatchSpanProcessorOptions to restore C++20 aggregate initialization support - Remove user-defined constructor that prevented C++20 designated initializers - Add inline helper functions in batch_span_processor_options_env namespace - Use default member initializers calling the helper functions - Update tests with corrected default values (2048 for max_queue_size, 30000ms for export_timeout) - Add test for aggregate initialization and C++20 designated initializers Co-authored-by: lalitb <[email protected]> * Update PR progress Co-authored-by: lalitb <[email protected]> * Remove accidentally committed CodeQL build artifacts and update .gitignore Co-authored-by: lalitb <[email protected]> * Fix C++14 compatibility: remove inline variables from header Move constants inside inline functions to avoid using inline variables which are a C++17 feature not available in C++14. Co-authored-by: lalitb <[email protected]> * Fix Windows DLL compatibility: move helper functions to .cc files - Move env helper function implementations from header to .cc files - Declare helper functions with OPENTELEMETRY_EXPORT in header - Apply the same pattern to BatchLogRecordProcessorOptions - Add comprehensive documentation to both options structs - Fix "to to" typo in BatchSpanProcessorOptions documentation This fixes the Windows single DLL build issue by avoiding inline functions in header files that are called from default member initializers. Co-authored-by: lalitb <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: lalitb <[email protected]>
1 parent 17ffe4e commit 179e4b0

File tree

6 files changed

+218
-63
lines changed

6 files changed

+218
-63
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,7 @@ functional/cert/server_cert_b.csr
8080
functional/cert/server_cert_b.pem
8181
functional/cert/server_cert_b-key.pem
8282
functional/cert/unreadable.pem
83+
84+
# CodeQL build artifacts
85+
_codeql_build_dir/
86+
_codeql_detected_source_root

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

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,71 @@ namespace sdk
1515
namespace logs
1616
{
1717

18+
namespace batch_log_record_processor_options_env
19+
{
20+
21+
/// @brief Returns the max queue size from the OTEL_BLRP_MAX_QUEUE_SIZE environment variable
22+
/// or the default value (2048) if not set.
23+
OPENTELEMETRY_EXPORT size_t GetMaxQueueSizeFromEnv();
24+
25+
/// @brief Returns the schedule delay from the OTEL_BLRP_SCHEDULE_DELAY environment variable
26+
/// or the default value (1000ms) if not set.
27+
OPENTELEMETRY_EXPORT std::chrono::milliseconds GetScheduleDelayFromEnv();
28+
29+
/// @brief Returns the export timeout from the OTEL_BLRP_EXPORT_TIMEOUT environment variable
30+
/// or the default value (30000ms) if not set.
31+
OPENTELEMETRY_EXPORT std::chrono::milliseconds GetExportTimeoutFromEnv();
32+
33+
/// @brief Returns the max export batch size from the OTEL_BLRP_MAX_EXPORT_BATCH_SIZE environment
34+
/// variable or the default value (512) if not set.
35+
OPENTELEMETRY_EXPORT size_t GetMaxExportBatchSizeFromEnv();
36+
37+
} // namespace batch_log_record_processor_options_env
38+
1839
/**
1940
* Struct to hold batch LogRecordProcessor options.
41+
*
42+
* This is an aggregate type that supports C++20 designated initializers.
43+
* Default values are read from environment variables when an instance is created:
44+
* - OTEL_BLRP_MAX_QUEUE_SIZE (default: 2048)
45+
* - OTEL_BLRP_SCHEDULE_DELAY (default: 1000ms)
46+
* - OTEL_BLRP_EXPORT_TIMEOUT (default: 30000ms)
47+
* - OTEL_BLRP_MAX_EXPORT_BATCH_SIZE (default: 512)
48+
*
49+
* Usage notes:
50+
* - If you use default initialization (e.g., `BatchLogRecordProcessorOptions opts{}`), all fields
51+
* are set by reading the environment variables (or hardcoded defaults if unset).
52+
* - If you use aggregate initialization with explicit values (positional or designated),
53+
* those values override the environment variable defaults for the specified fields.
54+
* - With C++20 designated initializers, you can override only specific fields; unspecified
55+
* fields will use environment variables or hardcoded defaults.
2056
*/
2157
struct OPENTELEMETRY_EXPORT BatchLogRecordProcessorOptions
2258
{
23-
BatchLogRecordProcessorOptions();
2459
/**
25-
* The maximum buffer/queue size. After the size is reached, spans are
60+
* The maximum buffer/queue size. After the size is reached, log records are
2661
* dropped.
2762
*/
28-
size_t max_queue_size;
63+
size_t max_queue_size = batch_log_record_processor_options_env::GetMaxQueueSizeFromEnv();
2964

3065
/* The time interval between two consecutive exports. */
31-
std::chrono::milliseconds schedule_delay_millis;
66+
std::chrono::milliseconds schedule_delay_millis =
67+
batch_log_record_processor_options_env::GetScheduleDelayFromEnv();
3268

3369
/**
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
70+
* The maximum time allowed to export data.
71+
* It is not currently used by the SDK and the parameter is ignored.
3672
* TODO: Implement the parameter in BatchLogRecordProcessor
3773
*/
38-
std::chrono::milliseconds export_timeout_millis;
74+
std::chrono::milliseconds export_timeout_millis =
75+
batch_log_record_processor_options_env::GetExportTimeoutFromEnv();
3976

4077
/**
4178
* The maximum batch size of every export. It must be smaller or
4279
* equal to max_queue_size.
4380
*/
44-
size_t max_export_batch_size;
81+
size_t max_export_batch_size =
82+
batch_log_record_processor_options_env::GetMaxExportBatchSizeFromEnv();
4583
};
4684

4785
} // namespace logs

sdk/include/opentelemetry/sdk/trace/batch_span_processor_options.h

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#pragma once
55

66
#include <chrono>
7+
#include <cstddef>
78

89
#include "opentelemetry/version.h"
910

@@ -14,33 +15,77 @@ namespace sdk
1415
namespace trace
1516
{
1617

18+
namespace batch_span_processor_options_env
19+
{
20+
21+
/// @brief Returns the max queue size from the OTEL_BSP_MAX_QUEUE_SIZE environment variable
22+
/// or the default value (2048) if not set.
23+
OPENTELEMETRY_EXPORT size_t GetMaxQueueSizeFromEnv();
24+
25+
/// @brief Returns the schedule delay from the OTEL_BSP_SCHEDULE_DELAY environment variable
26+
/// or the default value (5000ms) if not set.
27+
OPENTELEMETRY_EXPORT std::chrono::milliseconds GetScheduleDelayFromEnv();
28+
29+
/// @brief Returns the export timeout from the OTEL_BSP_EXPORT_TIMEOUT environment variable
30+
/// or the default value (30000ms) if not set.
31+
OPENTELEMETRY_EXPORT std::chrono::milliseconds GetExportTimeoutFromEnv();
32+
33+
/// @brief Returns the max export batch size from the OTEL_BSP_MAX_EXPORT_BATCH_SIZE environment
34+
/// variable or the default value (512) if not set.
35+
OPENTELEMETRY_EXPORT size_t GetMaxExportBatchSizeFromEnv();
36+
37+
} // namespace batch_span_processor_options_env
38+
1739
/**
1840
* Struct to hold batch SpanProcessor options.
41+
*
42+
* This is an aggregate type that supports C++20 designated initializers.
43+
* Default values are read from environment variables when an instance is created:
44+
* - OTEL_BSP_MAX_QUEUE_SIZE (default: 2048)
45+
* - OTEL_BSP_SCHEDULE_DELAY (default: 5000ms)
46+
* - OTEL_BSP_EXPORT_TIMEOUT (default: 30000ms)
47+
* - OTEL_BSP_MAX_EXPORT_BATCH_SIZE (default: 512)
48+
*
49+
* Usage notes:
50+
* - If you use default initialization (e.g., `BatchSpanProcessorOptions opts{}`), all fields
51+
* are set by reading the environment variables (or hardcoded defaults if unset).
52+
* - If you use aggregate initialization with explicit values (positional or designated),
53+
* those values override the environment variable defaults for the specified fields.
54+
* - With C++20 designated initializers, you can override only specific fields; unspecified
55+
* fields will use environment variables or hardcoded defaults.
56+
*
57+
* Examples:
58+
* // All fields use env vars or hardcoded defaults
59+
* BatchSpanProcessorOptions opts1{};
60+
*
61+
* // C++20: Only max_queue_size overridden, other fields read from env vars/defaults
62+
* BatchSpanProcessorOptions opts3{.max_queue_size = 100};
1963
*/
2064
struct OPENTELEMETRY_EXPORT BatchSpanProcessorOptions
2165
{
22-
BatchSpanProcessorOptions();
2366
/**
2467
* The maximum buffer/queue size. After the size is reached, spans are
2568
* dropped.
2669
*/
27-
size_t max_queue_size;
70+
size_t max_queue_size = batch_span_processor_options_env::GetMaxQueueSizeFromEnv();
2871

2972
/* The time interval between two consecutive exports. */
30-
std::chrono::milliseconds schedule_delay_millis;
73+
std::chrono::milliseconds schedule_delay_millis =
74+
batch_span_processor_options_env::GetScheduleDelayFromEnv();
3175

3276
/**
33-
* The maximum time allowed to to export data
34-
* It is not currently used by the SDK and the parameter is ignored
77+
* The maximum time allowed to export data.
78+
* It is not currently used by the SDK and the parameter is ignored.
3579
* TODO: Implement the parameter in BatchSpanProcessor
3680
*/
37-
std::chrono::milliseconds export_timeout;
81+
std::chrono::milliseconds export_timeout =
82+
batch_span_processor_options_env::GetExportTimeoutFromEnv();
3883

3984
/**
4085
* The maximum batch size of every export. It must be smaller or
4186
* equal to max_queue_size.
4287
*/
43-
size_t max_export_batch_size;
88+
size_t max_export_batch_size = batch_span_processor_options_env::GetMaxExportBatchSizeFromEnv();
4489
};
4590

4691
} // namespace trace
Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#include <stddef.h>
54
#include <chrono>
5+
#include <cstddef>
66
#include <cstdint>
77

88
#include "opentelemetry/sdk/common/env_variables.h"
@@ -14,18 +14,20 @@ namespace sdk
1414
{
1515
namespace logs
1616
{
17+
namespace batch_log_record_processor_options_env
18+
{
1719

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";
20+
// Environment variable names
21+
static constexpr const char *kMaxQueueSizeEnv = "OTEL_BLRP_MAX_QUEUE_SIZE";
22+
static constexpr const char *kScheduleDelayEnv = "OTEL_BLRP_SCHEDULE_DELAY";
23+
static constexpr const char *kExportTimeoutEnv = "OTEL_BLRP_EXPORT_TIMEOUT";
24+
static constexpr const char *kMaxExportBatchSizeEnv = "OTEL_BLRP_MAX_EXPORT_BATCH_SIZE";
2225

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;
26+
// Default values
27+
static constexpr size_t kDefaultMaxQueueSize = 2048;
28+
static constexpr size_t kDefaultMaxExportBatchSize = 512;
2729

28-
inline size_t GetMaxQueueSizeFromEnv()
30+
size_t GetMaxQueueSizeFromEnv()
2931
{
3032
std::uint32_t value{};
3133
if (!opentelemetry::sdk::common::GetUintEnvironmentVariable(kMaxQueueSizeEnv, value))
@@ -35,19 +37,29 @@ inline size_t GetMaxQueueSizeFromEnv()
3537
return static_cast<size_t>(value);
3638
}
3739

38-
inline std::chrono::milliseconds GetDurationFromEnv(
39-
const char *env_var,
40-
const std::chrono::milliseconds &default_duration)
40+
std::chrono::milliseconds GetScheduleDelayFromEnv()
4141
{
42+
static const std::chrono::milliseconds kDefaultScheduleDelay{1000};
4243
std::chrono::system_clock::duration duration{0};
43-
if (!opentelemetry::sdk::common::GetDurationEnvironmentVariable(env_var, duration))
44+
if (!opentelemetry::sdk::common::GetDurationEnvironmentVariable(kScheduleDelayEnv, duration))
4445
{
45-
return default_duration;
46+
return kDefaultScheduleDelay;
4647
}
4748
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
4849
}
4950

50-
inline size_t GetMaxExportBatchSizeFromEnv()
51+
std::chrono::milliseconds GetExportTimeoutFromEnv()
52+
{
53+
static const std::chrono::milliseconds kDefaultExportTimeout{30000};
54+
std::chrono::system_clock::duration duration{0};
55+
if (!opentelemetry::sdk::common::GetDurationEnvironmentVariable(kExportTimeoutEnv, duration))
56+
{
57+
return kDefaultExportTimeout;
58+
}
59+
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
60+
}
61+
62+
size_t GetMaxExportBatchSizeFromEnv()
5163
{
5264
std::uint32_t value{};
5365
if (!opentelemetry::sdk::common::GetUintEnvironmentVariable(kMaxExportBatchSizeEnv, value))
@@ -57,13 +69,7 @@ inline size_t GetMaxExportBatchSizeFromEnv()
5769
return static_cast<size_t>(value);
5870
}
5971

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-
72+
} // namespace batch_log_record_processor_options_env
6773
} // namespace logs
6874
} // namespace sdk
6975
OPENTELEMETRY_END_NAMESPACE
Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
#include <stddef.h>
54
#include <chrono>
5+
#include <cstddef>
66
#include <cstdint>
77

88
#include "opentelemetry/sdk/common/env_variables.h"
@@ -14,18 +14,20 @@ namespace sdk
1414
{
1515
namespace trace
1616
{
17+
namespace batch_span_processor_options_env
18+
{
1719

18-
constexpr const char *kMaxQueueSizeEnv = "OTEL_BSP_MAX_QUEUE_SIZE";
19-
constexpr const char *kScheduleDelayEnv = "OTEL_BSP_SCHEDULE_DELAY";
20-
constexpr const char *kExportTimeoutEnv = "OTEL_BSP_EXPORT_TIMEOUT";
21-
constexpr const char *kMaxExportBatchSizeEnv = "OTEL_BSP_MAX_EXPORT_BATCH_SIZE";
20+
// Environment variable names
21+
static constexpr const char *kMaxQueueSizeEnv = "OTEL_BSP_MAX_QUEUE_SIZE";
22+
static constexpr const char *kScheduleDelayEnv = "OTEL_BSP_SCHEDULE_DELAY";
23+
static constexpr const char *kExportTimeoutEnv = "OTEL_BSP_EXPORT_TIMEOUT";
24+
static constexpr const char *kMaxExportBatchSizeEnv = "OTEL_BSP_MAX_EXPORT_BATCH_SIZE";
2225

23-
const size_t kDefaultMaxQueueSize = 2084;
24-
const std::chrono::milliseconds kDefaultScheduleDelayMillis = std::chrono::milliseconds(5000);
25-
const std::chrono::milliseconds kDefaultExportTimeout = std::chrono::milliseconds(3000);
26-
const size_t kDefaultMaxExportBatchSize = 512;
26+
// Default values
27+
static constexpr size_t kDefaultMaxQueueSize = 2048;
28+
static constexpr size_t kDefaultMaxExportBatchSize = 512;
2729

28-
inline size_t GetMaxQueueSizeFromEnv()
30+
size_t GetMaxQueueSizeFromEnv()
2931
{
3032
std::uint32_t value{};
3133
if (!opentelemetry::sdk::common::GetUintEnvironmentVariable(kMaxQueueSizeEnv, value))
@@ -35,19 +37,29 @@ inline size_t GetMaxQueueSizeFromEnv()
3537
return static_cast<size_t>(value);
3638
}
3739

38-
inline std::chrono::milliseconds GetDurationFromEnv(
39-
const char *env_var,
40-
const std::chrono::milliseconds &default_duration)
40+
std::chrono::milliseconds GetScheduleDelayFromEnv()
4141
{
42+
static const std::chrono::milliseconds kDefaultScheduleDelay{5000};
4243
std::chrono::system_clock::duration duration{0};
43-
if (!opentelemetry::sdk::common::GetDurationEnvironmentVariable(env_var, duration))
44+
if (!opentelemetry::sdk::common::GetDurationEnvironmentVariable(kScheduleDelayEnv, duration))
4445
{
45-
return default_duration;
46+
return kDefaultScheduleDelay;
4647
}
4748
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
4849
}
4950

50-
inline size_t GetMaxExportBatchSizeFromEnv()
51+
std::chrono::milliseconds GetExportTimeoutFromEnv()
52+
{
53+
static const std::chrono::milliseconds kDefaultExportTimeout{30000};
54+
std::chrono::system_clock::duration duration{0};
55+
if (!opentelemetry::sdk::common::GetDurationEnvironmentVariable(kExportTimeoutEnv, duration))
56+
{
57+
return kDefaultExportTimeout;
58+
}
59+
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
60+
}
61+
62+
size_t GetMaxExportBatchSizeFromEnv()
5163
{
5264
std::uint32_t value{};
5365
if (!opentelemetry::sdk::common::GetUintEnvironmentVariable(kMaxExportBatchSizeEnv, value))
@@ -57,13 +69,7 @@ inline size_t GetMaxExportBatchSizeFromEnv()
5769
return static_cast<size_t>(value);
5870
}
5971

60-
BatchSpanProcessorOptions::BatchSpanProcessorOptions()
61-
: max_queue_size(GetMaxQueueSizeFromEnv()),
62-
schedule_delay_millis(GetDurationFromEnv(kScheduleDelayEnv, kDefaultScheduleDelayMillis)),
63-
export_timeout(GetDurationFromEnv(kExportTimeoutEnv, kDefaultExportTimeout)),
64-
max_export_batch_size(GetMaxExportBatchSizeFromEnv())
65-
{}
66-
72+
} // namespace batch_span_processor_options_env
6773
} // namespace trace
6874
} // namespace sdk
6975
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)