Skip to content

Commit 353ad0d

Browse files
committed
Add tests
1 parent 8d63d6f commit 353ad0d

File tree

8 files changed

+414
-24
lines changed

8 files changed

+414
-24
lines changed

.bazelrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ build --copt -DGRPC_BAZEL_BUILD
1717
build:windows --dynamic_mode=off
1818

1919
# Set minimum supported C++ version
20-
build:macos --host_cxxopt=-std=c++17 --cxxopt=-std=c++17
21-
build:linux --host_cxxopt=-std=c++17 --cxxopt=-std=c++17
22-
build:windows --host_cxxopt=/std:c++17 --cxxopt=/std:c++17
20+
build:macos --host_cxxopt=-std=c++14 --cxxopt=-std=c++14
21+
build:linux --host_cxxopt=-std=c++14 --cxxopt=-std=c++14
22+
build:windows --host_cxxopt=/std:c++14 --cxxopt=/std:c++14
2323

2424
# --config=asan : Address Sanitizer.
2525
common:asan --copt -DADDRESS_SANITIZER

sdk/include/opentelemetry/sdk/metrics/meter_context.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,20 @@ class MeterContext : public std::enable_shared_from_this<MeterContext>
9898
opentelemetry::common::SystemTimestamp GetSDKStartTime() noexcept;
9999

100100
/**
101-
* Attaches a metric reader to list of configured readers for this Meter context.
102-
* @param reader The metric reader for this meter context. This
103-
* must not be a nullptr.
104-
* @param metric_filter The optional metric filter for the metric producer on which the supplied
105-
* metric reader is to be registered.
101+
* Create a MetricCollector from a MetricReader using this MeterContext and add it to the list of
102+
* configured collectors.
103+
* @param reader The MetricReader for which a MetricCollector is to be created. This must not be a
104+
* nullptr.
105+
* @param metric_filter The optional MetricFilter used when creating the MetricCollector.
106+
* @return The MetricCollector created.
106107
*
107108
* Note: This reader may not receive any in-flight meter data, but will get newly created meter
108-
* data. Note: This method is not thread safe, and should ideally be called from main thread.
109+
* data.
110+
* Note: This method is not thread safe, and should ideally be called from main thread.
109111
*/
110-
void AddMetricReader(std::shared_ptr<MetricReader> reader,
111-
absl::optional<MetricFilter> metric_filter = absl::nullopt) noexcept;
112+
std::shared_ptr<MetricCollector> AddMetricReader(
113+
std::shared_ptr<MetricReader> reader,
114+
absl::optional<MetricFilter> metric_filter = absl::nullopt) noexcept;
112115

113116
/**
114117
* Attaches a View to list of configured Views for this Meter context.

sdk/include/opentelemetry/sdk/metrics/meter_provider.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,20 @@ class OPENTELEMETRY_EXPORT MeterProvider final : public opentelemetry::metrics::
8181
const sdk::resource::Resource &GetResource() const noexcept;
8282

8383
/**
84-
* Attaches a metric reader to list of configured readers for this Meter providers.
85-
* @param reader The metric reader for this meter provider. This
86-
* must not be a nullptr.
87-
* @param metric_filter The optional metric filter for the metric producer on which the supplied
88-
* metric reader is to be registered.
84+
* Create a MetricCollector from a MetricReader using the MeterContext of this MeterProvider and
85+
* add it to the list of configured collectors.
86+
* @param reader The MetricReader for which a MetricCollector is to be created. This must not be a
87+
* nullptr.
88+
* @param metric_filter The optional MetricFilter used when creating the MetricCollector.
89+
* @return The MetricCollector created.
8990
*
9091
* Note: This reader may not receive any in-flight meter data, but will get newly created meter
91-
* data. Note: This method is not thread safe, and should ideally be called from main thread.
92+
* data.
93+
* Note: This method is not thread safe, and should ideally be called from main thread.
9294
*/
93-
void AddMetricReader(std::shared_ptr<MetricReader> reader,
94-
absl::optional<MetricFilter> metric_filter = absl::nullopt) noexcept;
95+
std::shared_ptr<MetricCollector> AddMetricReader(
96+
std::shared_ptr<MetricReader> reader,
97+
absl::optional<MetricFilter> metric_filter = absl::nullopt) noexcept;
9598

9699
/**
97100
* Attaches a View to list of configured Views for this Meter provider.

sdk/src/metrics/meter_context.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ opentelemetry::common::SystemTimestamp MeterContext::GetSDKStartTime() noexcept
8080
return sdk_start_ts_;
8181
}
8282

83-
void MeterContext::AddMetricReader(std::shared_ptr<MetricReader> reader,
84-
absl::optional<MetricFilter> metric_filter) noexcept
83+
std::shared_ptr<MetricCollector> MeterContext::AddMetricReader(
84+
std::shared_ptr<MetricReader> reader,
85+
absl::optional<MetricFilter> metric_filter) noexcept
8586
{
8687
auto collector =
8788
std::shared_ptr<MetricCollector>{new MetricCollector(this, std::move(reader), metric_filter)};
8889
collectors_.push_back(collector);
90+
return collector;
8991
}
9092

9193
void MeterContext::AddView(std::unique_ptr<InstrumentSelector> instrument_selector,

sdk/src/metrics/meter_provider.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ const resource::Resource &MeterProvider::GetResource() const noexcept
110110
return context_->GetResource();
111111
}
112112

113-
void MeterProvider::AddMetricReader(std::shared_ptr<MetricReader> reader,
114-
absl::optional<MetricFilter> metric_filter) noexcept
113+
std::shared_ptr<MetricCollector> MeterProvider::AddMetricReader(
114+
std::shared_ptr<MetricReader> reader,
115+
absl::optional<MetricFilter> metric_filter) noexcept
115116
{
116-
context_->AddMetricReader(std::move(reader), metric_filter);
117+
return context_->AddMetricReader(std::move(reader), metric_filter);
117118
}
118119

119120
void MeterProvider::AddView(std::unique_ptr<InstrumentSelector> instrument_selector,

sdk/test/metrics/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ cc_test(
2727
],
2828
deps = [
2929
"metrics_common_test_utils",
30+
"//exporters/ostream:ostream_metric_exporter",
3031
"@com_google_googletest//:gtest_main",
3132
],
3233
)

sdk/test/metrics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ foreach(
2727
observer_result_test
2828
sync_instruments_test
2929
async_instruments_test
30+
metric_collector_test
3031
metric_reader_test
3132
observable_registry_test
3233
periodic_exporting_metric_reader_test

0 commit comments

Comments
 (0)