From 823bc41fb3168bdbe3a7e0ea79b06a7d6e26240a Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Sat, 19 Jul 2025 22:52:09 +0200 Subject: [PATCH 1/2] [SDK] View should not have a unit --- examples/metrics_simple/metrics_ostream.cc | 10 +++++----- examples/otlp/grpc_metric_main.cc | 3 +-- .../opentelemetry/sdk/metrics/view/view.h | 3 --- .../sdk/metrics/view/view_factory.h | 7 ------- sdk/src/metrics/view/view_factory.cc | 18 ++++-------------- .../metrics/histogram_aggregation_benchmark.cc | 3 +-- sdk/test/metrics/histogram_aggregation_test.cc | 3 +-- sdk/test/metrics/histogram_test.cc | 8 ++++---- sdk/test/metrics/meter_test.cc | 2 +- sdk/test/metrics/sum_aggregation_test.cc | 18 +++++++----------- 10 files changed, 24 insertions(+), 51 deletions(-) diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index a7b6bcdd08..d305ffd52d 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -71,8 +71,8 @@ void InitMetrics(const std::string &name) auto meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema); - auto sum_view = metrics_sdk::ViewFactory::Create(name, "description", unit, - metrics_sdk::AggregationType::kSum); + auto sum_view = + metrics_sdk::ViewFactory::Create(name, "description", metrics_sdk::AggregationType::kSum); provider->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view)); @@ -84,7 +84,7 @@ void InitMetrics(const std::string &name) auto observable_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema); - auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description", unit, + auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description", metrics_sdk::AggregationType::kSum); provider->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector), @@ -109,7 +109,7 @@ void InitMetrics(const std::string &name) std::move(histogram_aggregation_config)); auto histogram_view = metrics_sdk::ViewFactory::Create( - name, "description", unit, metrics_sdk::AggregationType::kHistogram, aggregation_config); + name, "description", metrics_sdk::AggregationType::kHistogram, aggregation_config); provider->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), std::move(histogram_view)); @@ -132,7 +132,7 @@ void InitMetrics(const std::string &name) std::move(histogram_base2_aggregation_config)); auto histogram_base2_view = metrics_sdk::ViewFactory::Create( - name, "description", unit, metrics_sdk::AggregationType::kBase2ExponentialHistogram, + name, "description", metrics_sdk::AggregationType::kBase2ExponentialHistogram, base2_aggregation_config); provider->AddView(std::move(histogram_base2_instrument_selector), diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index cbd3f84825..31e72c44ac 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -83,8 +83,7 @@ void InitMetrics(std::string &name) std::move(histogram_aggregation_config)); auto histogram_view = metric_sdk::ViewFactory::Create( - name, "des", unit, metric_sdk::AggregationType::kBase2ExponentialHistogram, - aggregation_config); + name, "des", metric_sdk::AggregationType::kBase2ExponentialHistogram, aggregation_config); provider->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), std::move(histogram_view)); diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view.h b/sdk/include/opentelemetry/sdk/metrics/view/view.h index e1c6237b36..49c52ff5d3 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view.h @@ -26,7 +26,6 @@ class View public: View(const std::string &name, const std::string &description = "", - const std::string &unit = "", AggregationType aggregation_type = AggregationType::kDefault, std::shared_ptr aggregation_config = nullptr, std::unique_ptr attributes_processor = @@ -34,7 +33,6 @@ class View new opentelemetry::sdk::metrics::DefaultAttributesProcessor())) : name_(name), description_(description), - unit_(unit), aggregation_type_{aggregation_type}, aggregation_config_{aggregation_config}, attributes_processor_{std::move(attributes_processor)} @@ -62,7 +60,6 @@ class View private: std::string name_; std::string description_; - std::string unit_; AggregationType aggregation_type_; std::shared_ptr aggregation_config_; std::shared_ptr attributes_processor_; diff --git a/sdk/include/opentelemetry/sdk/metrics/view/view_factory.h b/sdk/include/opentelemetry/sdk/metrics/view/view_factory.h index 2fa1733f92..df3c966571 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/view_factory.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/view_factory.h @@ -30,22 +30,15 @@ class OPENTELEMETRY_EXPORT ViewFactory static std::unique_ptr Create(const std::string &name, const std::string &description, - const std::string &unit); - - static std::unique_ptr Create(const std::string &name, - const std::string &description, - const std::string &unit, AggregationType aggregation_type); static std::unique_ptr Create(const std::string &name, const std::string &description, - const std::string &unit, AggregationType aggregation_type, std::shared_ptr aggregation_config); static std::unique_ptr Create(const std::string &name, const std::string &description, - const std::string &unit, AggregationType aggregation_type, std::shared_ptr aggregation_config, std::unique_ptr attributes_processor); diff --git a/sdk/src/metrics/view/view_factory.cc b/sdk/src/metrics/view/view_factory.cc index 319c10b956..13d4678ece 100644 --- a/sdk/src/metrics/view/view_factory.cc +++ b/sdk/src/metrics/view/view_factory.cc @@ -25,46 +25,36 @@ std::unique_ptr ViewFactory::Create(const std::string &name) std::unique_ptr ViewFactory::Create(const std::string &name, const std::string &description) { - return Create(name, description, "", AggregationType::kDefault); + return Create(name, description, AggregationType::kDefault); } std::unique_ptr ViewFactory::Create(const std::string &name, const std::string &description, - const std::string &unit) -{ - return Create(name, description, unit, AggregationType::kDefault); -} - -std::unique_ptr ViewFactory::Create(const std::string &name, - const std::string &description, - const std::string &unit, AggregationType aggregation_type) { std::shared_ptr aggregation_config(nullptr); - return Create(name, description, unit, aggregation_type, aggregation_config); + return Create(name, description, aggregation_type, aggregation_config); } std::unique_ptr ViewFactory::Create(const std::string &name, const std::string &description, - const std::string &unit, AggregationType aggregation_type, std::shared_ptr aggregation_config) { auto attributes_processor = std::unique_ptr(new DefaultAttributesProcessor()); - return Create(name, description, unit, aggregation_type, std::move(aggregation_config), + return Create(name, description, aggregation_type, std::move(aggregation_config), std::move(attributes_processor)); } std::unique_ptr ViewFactory::Create(const std::string &name, const std::string &description, - const std::string &unit, AggregationType aggregation_type, std::shared_ptr aggregation_config, std::unique_ptr attributes_processor) { - std::unique_ptr view(new View(name, description, unit, aggregation_type, + std::unique_ptr view(new View(name, description, aggregation_type, std::move(aggregation_config), std::move(attributes_processor))); return view; diff --git a/sdk/test/metrics/histogram_aggregation_benchmark.cc b/sdk/test/metrics/histogram_aggregation_benchmark.cc index 45fcb5b1a4..e4b90eebc4 100644 --- a/sdk/test/metrics/histogram_aggregation_benchmark.cc +++ b/sdk/test/metrics/histogram_aggregation_benchmark.cc @@ -116,8 +116,7 @@ void RunBase2ExponentialHistogramAggregation(benchmark::State &state, int scale) config.max_scale_ = scale; std::unique_ptr histogram_view{ - new View("base2_expohisto", "description", instrument_unit, - AggregationType::kBase2ExponentialHistogram, + new View("base2_expohisto", "description", AggregationType::kBase2ExponentialHistogram, std::make_shared(config))}; std::unique_ptr views{new ViewRegistry()}; diff --git a/sdk/test/metrics/histogram_aggregation_test.cc b/sdk/test/metrics/histogram_aggregation_test.cc index 25640209d1..9de7af7d8e 100644 --- a/sdk/test/metrics/histogram_aggregation_test.cc +++ b/sdk/test/metrics/histogram_aggregation_test.cc @@ -87,8 +87,7 @@ TEST(CounterToHistogram, Double) std::shared_ptr reader{new MockMetricReader(std::move(exporter))}; mp.AddMetricReader(reader); - std::unique_ptr view{ - new View("view1", "view1_description", "unit", AggregationType::kHistogram)}; + std::unique_ptr view{new View("view1", "view1_description", AggregationType::kHistogram)}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kCounter, "counter1", "unit")}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; diff --git a/sdk/test/metrics/histogram_test.cc b/sdk/test/metrics/histogram_test.cc index a75b4f3f13..ad819d9e99 100644 --- a/sdk/test/metrics/histogram_test.cc +++ b/sdk/test/metrics/histogram_test.cc @@ -103,7 +103,7 @@ TEST(Histogram, DoubleCustomBuckets) std::shared_ptr config(new HistogramAggregationConfig()); config->boundaries_ = {10, 20, 30, 40}; std::unique_ptr view{ - new View("view1", "view1_description", instrument_unit, AggregationType::kHistogram, config)}; + new View("view1", "view1_description", AggregationType::kHistogram, config)}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; @@ -163,7 +163,7 @@ TEST(Histogram, DoubleEmptyBuckets) std::shared_ptr config(new HistogramAggregationConfig()); config->boundaries_ = {}; std::unique_ptr view{ - new View("view1", "view1_description", instrument_unit, AggregationType::kHistogram, config)}; + new View("view1", "view1_description", AggregationType::kHistogram, config)}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; @@ -278,7 +278,7 @@ TEST(Histogram, UInt64CustomBuckets) std::shared_ptr config(new HistogramAggregationConfig()); config->boundaries_ = {10, 20, 30, 40}; std::unique_ptr view{ - new View("view1", "view1_description", "ms", AggregationType::kHistogram, config)}; + new View("view1", "view1_description", AggregationType::kHistogram, config)}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; @@ -338,7 +338,7 @@ TEST(Histogram, UInt64EmptyBuckets) std::shared_ptr config(new HistogramAggregationConfig()); config->boundaries_ = {}; std::unique_ptr view{ - new View("view1", "view1_description", "ms", AggregationType::kHistogram, config)}; + new View("view1", "view1_description", AggregationType::kHistogram, config)}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; diff --git a/sdk/test/metrics/meter_test.cc b/sdk/test/metrics/meter_test.cc index a4212996a3..5fe99b41a8 100644 --- a/sdk/test/metrics/meter_test.cc +++ b/sdk/test/metrics/meter_test.cc @@ -881,7 +881,7 @@ TEST(MeterTest, RecordAfterProviderDestructionWithCustomProcessor_NoResetInMain) // Register a View with custom processor std::unique_ptr view( - new View("my_counter", "", "", AggregationType::kSum, nullptr, std::move(processor))); + new View("my_counter", "", AggregationType::kSum, nullptr, std::move(processor))); std::unique_ptr instr_selector( new InstrumentSelector(InstrumentType::kCounter, "my_counter", "")); std::unique_ptr meter_selector(new MeterSelector("test_meter", "", "")); diff --git a/sdk/test/metrics/sum_aggregation_test.cc b/sdk/test/metrics/sum_aggregation_test.cc index 732f29e1f4..40d3bbf89a 100644 --- a/sdk/test/metrics/sum_aggregation_test.cc +++ b/sdk/test/metrics/sum_aggregation_test.cc @@ -49,8 +49,7 @@ TEST(HistogramToSum, Double) std::shared_ptr reader{new MockMetricReader(std::move(exporter))}; mp.AddMetricReader(reader); - std::unique_ptr view{ - new View("view1", "view1_description", instrument_unit, AggregationType::kSum)}; + std::unique_ptr view{new View("view1", "view1_description", AggregationType::kSum)}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; @@ -109,9 +108,8 @@ TEST(HistogramToSumFilterAttributes, Double) std::shared_ptr reader{new MockMetricReader(std::move(exporter))}; mp.AddMetricReader(reader); - std::unique_ptr view{new View("view1", "view1_description", instrument_unit, - AggregationType::kSum, dummy_aggregation_config, - std::move(attrproc))}; + std::unique_ptr view{new View("view1", "view1_description", AggregationType::kSum, + dummy_aggregation_config, std::move(attrproc))}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; @@ -153,7 +151,7 @@ TEST(CounterToSum, Double) std::shared_ptr reader{new MockMetricReader(std::move(exporter))}; mp.AddMetricReader(reader); - std::unique_ptr view{new View("view1", "view1_description", "ms", AggregationType::kSum)}; + std::unique_ptr view{new View("view1", "view1_description", AggregationType::kSum)}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kCounter, "counter1", "ms")}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; @@ -212,9 +210,8 @@ TEST(CounterToSumFilterAttributes, Double) std::shared_ptr reader{new MockMetricReader(std::move(exporter))}; mp.AddMetricReader(reader); - std::unique_ptr view{new View("view1", "view1_description", instrument_unit, - AggregationType::kSum, dummy_aggregation_config, - std::move(attrproc))}; + std::unique_ptr view{new View("view1", "view1_description", AggregationType::kSum, + dummy_aggregation_config, std::move(attrproc))}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kCounter, instrument_name, instrument_unit)}; std::unique_ptr meter_selector{new MeterSelector("meter1", "version1", "schema1")}; @@ -265,8 +262,7 @@ TEST_P(UpDownCounterToSumFixture, Double) if (is_matching_view) { - std::unique_ptr view{ - new View("view1", "view1_description", instrument_unit, AggregationType::kSum)}; + std::unique_ptr view{new View("view1", "view1_description", AggregationType::kSum)}; std::unique_ptr instrument_selector{ new InstrumentSelector(InstrumentType::kUpDownCounter, instrument_name, instrument_unit)}; std::unique_ptr meter_selector{ From 3191265a52a175159986c9cc4e9d62e835b50f0a Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Sat, 19 Jul 2025 23:08:44 +0200 Subject: [PATCH 2/2] CHANGELOG --- CHANGELOG.md | 11 +++++++++++ examples/prometheus/main.cc | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22fd5e9fed..4b18de77bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,17 @@ Increment the: * [SDK] Update default exemplar reservoir size for exponential histograms [#3551](https://github.com/open-telemetry/opentelemetry-cpp/pull/3551) +* [SDK] View should not have a unit + [#3552](https://github.com/open-telemetry/opentelemetry-cpp/pull/3552) + +Breaking changes: + +* [SDK] View should not have a unit + [#3552](https://github.com/open-telemetry/opentelemetry-cpp/pull/3552) + * The `unit` parameter has been removed from the `View` constructor + and `ViewFactory::Create` methods. + * Please adjust SDK configuration code accordingly. + ## [1.22 2025-07-11] * [DOC] Udpate link to membership document diff --git a/examples/prometheus/main.cc b/examples/prometheus/main.cc index b2c2c74fec..0f82394e8e 100644 --- a/examples/prometheus/main.cc +++ b/examples/prometheus/main.cc @@ -66,7 +66,7 @@ void InitMetrics(const std::string &name, const std::string &addr) auto meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema); - auto sum_view = metrics_sdk::ViewFactory::Create(counter_name, "description", counter_unit, + auto sum_view = metrics_sdk::ViewFactory::Create(counter_name, "description", metrics_sdk::AggregationType::kSum); p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view)); @@ -80,8 +80,8 @@ void InitMetrics(const std::string &name, const std::string &addr) auto histogram_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema); - auto histogram_view = metrics_sdk::ViewFactory::Create( - histogram_name, "description", histogram_unit, metrics_sdk::AggregationType::kHistogram); + auto histogram_view = metrics_sdk::ViewFactory::Create(histogram_name, "description", + metrics_sdk::AggregationType::kHistogram); p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector), std::move(histogram_view));