Skip to content

Commit fb2a5aa

Browse files
committed
Merge branch 'main' into merge_config_sdk_builder
2 parents 4559186 + c406864 commit fb2a5aa

File tree

7 files changed

+109
-10
lines changed

7 files changed

+109
-10
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Increment the:
1818
* [TEST] Shared otel-cpp libs linked to latest static protobuf and grpc
1919
[#3544](https://github.com/open-telemetry/opentelemetry-cpp/pull/3544)
2020

21+
* [SDK] Implement env var configuration for PeriodicExportingMetricReader
22+
[#3549](https://github.com/open-telemetry/opentelemetry-cpp/pull/3549)
23+
24+
* [SDK] Update default exemplar reservoir size for exponential histograms
25+
[#3551](https://github.com/open-telemetry/opentelemetry-cpp/pull/3551)
26+
2127
## [1.22 2025-07-11]
2228

2329
* [DOC] Udpate link to membership document
@@ -1407,7 +1413,7 @@ Important changes:
14071413
* [ETW EXPORTER] Remove namespace using in ETW exporter which affects global
14081414
namespace
14091415
[#2531](https://github.com/open-telemetry/opentelemetry-cpp/pull/2531)
1410-
* [BUILD] Don't invoke vcpkg from this repo with CMAKE_TOOLCHAIN_FILE set
1416+
* [BUILD] Don't invoke vcpkg from this repo with CMAKE_TOOLCHAIN_FILE set
14111417
[#2527](https://github.com/open-telemetry/opentelemetry-cpp/pull/2527)
14121418
* [EXPORTER] Async exporting for otlp grpc
14131419
[#2407](https://github.com/open-telemetry/opentelemetry-cpp/pull/2407)

ext/src/dll/input.src

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ForceFlush@LoggerProvider@logs@sdk@v1@opentelemetry
1616
OStreamLogRecordExporter@logs@exporter@v1@opentelemetry
1717
Create@OStreamMetricExporterFactory@metrics@exporter@v1@opentelemetry
1818
Create@PeriodicExportingMetricReaderFactory@metrics@sdk@v1@opentelemetry
19+
PeriodicExportingMetricReaderOptions@metrics@sdk@v1@opentelemetry
1920
Create@MeterProviderFactory@metrics@sdk@v1@opentelemetry
2021
Create@MeterContextFactory@metrics@sdk@v1@opentelemetry
2122
Create@ViewFactory@metrics@sdk@v1@opentelemetry

sdk/include/opentelemetry/sdk/metrics/exemplar/reservoir_utils.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW
77

8+
# include <algorithm>
9+
810
# include "opentelemetry/common/macros.h"
911
# include "opentelemetry/sdk/metrics/aggregation/aggregation_config.h"
1012
# include "opentelemetry/sdk/metrics/exemplar/aligned_histogram_bucket_exemplar_reservoir.h"
@@ -27,6 +29,23 @@ static inline MapAndResetCellType GetMapAndResetCellMethod(
2729
return &ReservoirCell::GetAndResetDouble;
2830
}
2931

32+
static inline size_t GetSimpleFixedReservoirDefaultSize(const AggregationType agg_type,
33+
const AggregationConfig *const agg_config)
34+
35+
{
36+
constexpr size_t kMaxBase2ExponentialHistogramReservoirSize = 20;
37+
38+
if (agg_type == AggregationType::kBase2ExponentialHistogram)
39+
{
40+
const auto *histogram_agg_config =
41+
static_cast<const Base2ExponentialHistogramAggregationConfig *>(agg_config);
42+
return (std::min)(kMaxBase2ExponentialHistogramReservoirSize,
43+
histogram_agg_config->max_buckets_);
44+
}
45+
46+
return SimpleFixedSizeExemplarReservoir::kDefaultSimpleReservoirSize;
47+
}
48+
3049
static inline nostd::shared_ptr<ExemplarReservoir> GetExemplarReservoir(
3150
const AggregationType agg_type,
3251
const AggregationConfig *agg_config,
@@ -52,7 +71,7 @@ static inline nostd::shared_ptr<ExemplarReservoir> GetExemplarReservoir(
5271
}
5372

5473
return nostd::shared_ptr<ExemplarReservoir>(new SimpleFixedSizeExemplarReservoir(
55-
SimpleFixedSizeExemplarReservoir::kDefaultSimpleReservoirSize,
74+
GetSimpleFixedReservoirDefaultSize(agg_type, agg_config),
5675
SimpleFixedSizeExemplarReservoir::GetSimpleFixedSizeCellSelector(),
5776
GetMapAndResetCellMethod(instrument_descriptor)));
5877
}

sdk/include/opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h

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

44
#pragma once
55

6-
#include "opentelemetry/version.h"
7-
86
#include <chrono>
97

8+
#include "opentelemetry/version.h"
9+
1010
OPENTELEMETRY_BEGIN_NAMESPACE
1111
namespace sdk
1212
{
@@ -20,14 +20,15 @@ constexpr std::chrono::milliseconds kExportTimeOutMillis = std::chrono::millise
2020
* Struct to hold PeriodicExortingMetricReader options.
2121
*/
2222

23-
struct PeriodicExportingMetricReaderOptions
23+
struct OPENTELEMETRY_EXPORT PeriodicExportingMetricReaderOptions
2424
{
2525
/* The time interval between two consecutive exports. */
26-
std::chrono::milliseconds export_interval_millis =
27-
std::chrono::milliseconds(kExportIntervalMillis);
26+
std::chrono::milliseconds export_interval_millis;
2827

2928
/* how long the export can run before it is cancelled. */
30-
std::chrono::milliseconds export_timeout_millis = std::chrono::milliseconds(kExportTimeOutMillis);
29+
std::chrono::milliseconds export_timeout_millis;
30+
31+
PeriodicExportingMetricReaderOptions();
3132
};
3233

3334
} // namespace metrics

sdk/src/metrics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_library(
1515
instrument_metadata_validator.cc
1616
export/periodic_exporting_metric_reader.cc
1717
export/periodic_exporting_metric_reader_factory.cc
18+
export/periodic_exporting_metric_reader_options.cc
1819
state/filtered_ordered_attribute_map.cc
1920
state/metric_collector.cc
2021
state/observable_registry.cc
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include <chrono>
5+
6+
#include "opentelemetry/nostd/string_view.h"
7+
#include "opentelemetry/sdk/common/env_variables.h"
8+
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_options.h"
9+
#include "opentelemetry/version.h"
10+
11+
OPENTELEMETRY_BEGIN_NAMESPACE
12+
namespace sdk
13+
{
14+
namespace metrics
15+
{
16+
17+
std::chrono::milliseconds GetEnvDuration(nostd::string_view env_var_name,
18+
std::chrono::milliseconds default_value)
19+
{
20+
std::chrono::system_clock::duration duration;
21+
if (common::GetDurationEnvironmentVariable(env_var_name.data(), duration))
22+
{
23+
return std::chrono::duration_cast<std::chrono::milliseconds>(duration);
24+
}
25+
return default_value;
26+
}
27+
28+
PeriodicExportingMetricReaderOptions::PeriodicExportingMetricReaderOptions()
29+
: export_interval_millis(GetEnvDuration("OTEL_METRIC_EXPORT_INTERVAL", kExportIntervalMillis)),
30+
export_timeout_millis(GetEnvDuration("OTEL_METRIC_EXPORT_TIMEOUT", kExportTimeOutMillis))
31+
{}
32+
33+
} // namespace metrics
34+
} // namespace sdk
35+
OPENTELEMETRY_END_NAMESPACE

sdk/test/metrics/periodic_exporting_metric_reader_test.cc

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <gtest/gtest.h>
55
#include <stddef.h>
66
#include <chrono>
7+
#include <cstdlib>
78
#include <memory>
89
#include <ratio>
910
#include <thread>
@@ -18,6 +19,12 @@
1819
#include "opentelemetry/sdk/metrics/instruments.h"
1920
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
2021

22+
#if defined(_MSC_VER)
23+
# include "opentelemetry/sdk/common/env_variables.h"
24+
using opentelemetry::sdk::common::setenv;
25+
using opentelemetry::sdk::common::unsetenv;
26+
#endif
27+
2128
using namespace opentelemetry;
2229
using namespace opentelemetry::sdk::instrumentationscope;
2330
using namespace opentelemetry::sdk::metrics;
@@ -76,7 +83,7 @@ class MockMetricProducer : public MetricProducer
7683
size_t data_sent_size_{0};
7784
};
7885

79-
TEST(PeriodicExporingMetricReader, BasicTests)
86+
TEST(PeriodicExportingMetricReader, BasicTests)
8087
{
8188
std::unique_ptr<PushMetricExporter> exporter(
8289
new MockPushMetricExporter(std::chrono::milliseconds{0}));
@@ -95,7 +102,7 @@ TEST(PeriodicExporingMetricReader, BasicTests)
95102
static_cast<MockMetricProducer *>(&producer)->GetDataCount());
96103
}
97104

98-
TEST(PeriodicExporingMetricReader, Timeout)
105+
TEST(PeriodicExportingMetricReader, Timeout)
99106
{
100107
std::unique_ptr<PushMetricExporter> exporter(
101108
new MockPushMetricExporter(std::chrono::milliseconds{2000}));
@@ -109,3 +116,32 @@ TEST(PeriodicExporingMetricReader, Timeout)
109116
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
110117
reader->Shutdown();
111118
}
119+
120+
TEST(PeriodicExportingMetricReaderOptions, UsesEnvVars)
121+
{
122+
const char *env_interval = "OTEL_METRIC_EXPORT_INTERVAL";
123+
const char *env_timeout = "OTEL_METRIC_EXPORT_TIMEOUT";
124+
125+
setenv(env_interval, "1500ms", 1);
126+
setenv(env_timeout, "1000ms", 1);
127+
128+
PeriodicExportingMetricReaderOptions options;
129+
EXPECT_EQ(options.export_interval_millis, std::chrono::milliseconds(1500));
130+
EXPECT_EQ(options.export_timeout_millis, std::chrono::milliseconds(1000));
131+
132+
unsetenv(env_interval);
133+
unsetenv(env_timeout);
134+
}
135+
136+
TEST(PeriodicExportingMetricReaderOptions, UsesDefault)
137+
{
138+
const char *env_interval = "OTEL_METRIC_EXPORT_INTERVAL";
139+
const char *env_timeout = "OTEL_METRIC_EXPORT_TIMEOUT";
140+
141+
unsetenv(env_interval);
142+
unsetenv(env_timeout);
143+
144+
PeriodicExportingMetricReaderOptions options;
145+
EXPECT_EQ(options.export_interval_millis, std::chrono::milliseconds(60000));
146+
EXPECT_EQ(options.export_timeout_millis, std::chrono::milliseconds(30000));
147+
}

0 commit comments

Comments
 (0)