From 63fa75e7e66296609637e10533221e1791e23182 Mon Sep 17 00:00:00 2001 From: Uzay Uysal Date: Wed, 14 Aug 2024 18:41:00 +0200 Subject: [PATCH 1/8] Add synchronous gauge --- api/include/opentelemetry/metrics/meter.h | 22 +++ api/include/opentelemetry/metrics/noop.h | 34 ++++ .../opentelemetry/metrics/sync_instruments.h | 75 ++++++++ .../common/metrics_foo_library/foo_library.cc | 19 ++ .../common/metrics_foo_library/foo_library.h | 1 + examples/metrics_simple/metrics_ostream.cc | 6 + examples/otlp/file_metric_main.cc | 6 + examples/otlp/grpc_metric_main.cc | 6 + examples/otlp/http_metric_main.cc | 6 + exporters/otlp/src/otlp_metric_utils.cc | 2 + .../metrics/aggregation/default_aggregation.h | 1 + .../opentelemetry/sdk/metrics/instruments.h | 1 + sdk/include/opentelemetry/sdk/metrics/meter.h | 10 + .../sdk/metrics/sync_instruments.h | 32 ++++ sdk/src/metrics/meter.cc | 44 +++++ sdk/src/metrics/sync_instruments.cc | 128 +++++++++++++ sdk/test/metrics/BUILD | 16 ++ sdk/test/metrics/CMakeLists.txt | 1 + sdk/test/metrics/sync_instruments_test.cc | 40 ++++ .../metrics/sync_metric_storage_gauge_test.cc | 177 ++++++++++++++++++ 20 files changed, 627 insertions(+) create mode 100644 sdk/test/metrics/sync_metric_storage_gauge_test.cc diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index ced5890310..463c1d1ee0 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -21,6 +21,9 @@ class Histogram; template class UpDownCounter; +template +class Gauge; + class ObservableInstrument; /** @@ -91,6 +94,25 @@ class Meter nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; + /** + * Creates a Gauge with the passed characteristics and returns a unique_ptr to that Counter. + * + * @param name the name of the new Gauge. + * @param description a brief description of what the Gauge is used for. + * @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html. + * @return a unique pointer to the created Gauge. + */ + + virtual nostd::unique_ptr> CreateInt64Gauge( + nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept = 0; + + virtual nostd::unique_ptr> CreateDoubleGauge( + nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept = 0; + /** * Creates a Asynchronous (Observable) Gauge with the passed characteristics and returns a * shared_ptr to that Observable Gauge diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index d34a0e681b..830877f16c 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -71,6 +71,24 @@ class NoopUpDownCounter : public UpDownCounter {} }; +template +class NoopGauge : public Gauge +{ +public: + NoopGauge(nostd::string_view /* name */, + nostd::string_view /* description */, + nostd::string_view /* unit */) noexcept + {} + ~NoopGauge() override = default; + void Record(T /* value */) noexcept override {} + void Record(T /* value */, const context::Context & /* context */) noexcept override {} + void Record(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {} + void Record(T /* value */, + const common::KeyValueIterable & /* attributes */, + const context::Context & /* context */) noexcept override + {} +}; + class NoopObservableInstrument : public ObservableInstrument { public: @@ -140,6 +158,22 @@ class NoopMeter final : public Meter return nostd::unique_ptr>{new NoopHistogram(name, description, unit)}; } + nostd::unique_ptr> CreateInt64Gauge( + nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override + { + return nostd::unique_ptr>{new NoopGauge(name, description, unit)}; + } + + nostd::unique_ptr> CreateDoubleGauge( + nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override + { + return nostd::unique_ptr>{new NoopGauge(name, description, unit)}; + } + nostd::shared_ptr CreateInt64ObservableGauge( nostd::string_view name, nostd::string_view description = "", diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 4471677433..f9b0fde521 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -247,5 +247,80 @@ class UpDownCounter : public SynchronousInstrument } }; +/* A Gauge instrument that records values. */ +template +class Gauge : public SynchronousInstrument +{ + +public: + /** + * Record a value + * + * @param value The measurement value. May be positive, negative or zero. + */ + virtual void Record(T value) noexcept = 0; + + /** + * Record a value + * + * @param value The measurement value. May be positive, negative or zero. + * @param context The explicit context to associate with this measurement. + */ + virtual void Record(T value, const context::Context &context) noexcept = 0; + + /** + * Record a value with a set of attributes. + * + * @param value The measurement value. May be positive, negative or zero. + * @param attributes A set of attributes to associate with the value. + */ + + virtual void Record(T value, const common::KeyValueIterable &attributes) noexcept = 0; + + /** + * Record a value with a set of attributes. + * + * @param value The measurement value. May be positive, negative or zero. + * @param attributes A set of attributes to associate with the value. + * @param context The explicit context to associate with this measurement. + */ + virtual void Record(T value, + const common::KeyValueIterable &attributes, + const context::Context &context) noexcept = 0; + + template ::value> * = nullptr> + void Record(T value, const U &attributes) noexcept + { + this->Record(value, common::KeyValueIterableView{attributes}); + } + + template ::value> * = nullptr> + void Record(T value, const U &attributes, const context::Context &context) noexcept + { + this->Record(value, common::KeyValueIterableView{attributes}, context); + } + + void Record(T value, + std::initializer_list> + attributes) noexcept + { + this->Record(value, nostd::span>{ + attributes.begin(), attributes.end()}); + } + + void Record(T value, + std::initializer_list> attributes, + const context::Context &context) noexcept + { + this->Record(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); + } +}; + + } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index c4be2b530f..0c600020de 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -106,4 +106,23 @@ void foo_library::histogram_example(const std::string &name) histogram_counter->Record(val, labelkv, context); std::this_thread::sleep_for(std::chrono::milliseconds(250)); } + + +} + +void foo_library::gauge_example(const std::string &name) +{ + std::string gauge_name = name + "_gauge"; + auto provider = metrics_api::Provider::GetMeterProvider(); + opentelemetry::nostd::shared_ptr meter = provider->GetMeter(name, "1.2.0"); + auto gauge = meter->CreateInt64Gauge(gauge_name, "des", "unit"); + auto context = opentelemetry::context::Context{}; + for (uint32_t i = 0; i < 20; ++i) + { + int64_t val = (rand() % 100) + 100; + std::map labels = get_random_attr(); + auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; + gauge->Record(val, labelkv, context); + std::this_thread::sleep_for(std::chrono::milliseconds(250)); + } } diff --git a/examples/common/metrics_foo_library/foo_library.h b/examples/common/metrics_foo_library/foo_library.h index 217f91671b..02511a5b66 100644 --- a/examples/common/metrics_foo_library/foo_library.h +++ b/examples/common/metrics_foo_library/foo_library.h @@ -11,4 +11,5 @@ class foo_library static void counter_example(const std::string &name); static void histogram_example(const std::string &name); static void observable_counter_example(const std::string &name); + static void gauge_example(const std::string &name); }; diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 5bdc7339eb..266a76d2e8 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -148,15 +148,21 @@ int main(int argc, char **argv) { foo_library::histogram_example(name); } + else if (example_type == "gauge") + { + foo_library::gauge_example(name); + } else { std::thread counter_example{&foo_library::counter_example, name}; std::thread observable_counter_example{&foo_library::observable_counter_example, name}; std::thread histogram_example{&foo_library::histogram_example, name}; + std::thread gauge_example{&foo_library::gauge_example, name}; counter_example.join(); observable_counter_example.join(); histogram_example.join(); + gauge_example.join(); } CleanupMetrics(); diff --git a/examples/otlp/file_metric_main.cc b/examples/otlp/file_metric_main.cc index ce259005be..50e56d92d4 100644 --- a/examples/otlp/file_metric_main.cc +++ b/examples/otlp/file_metric_main.cc @@ -98,15 +98,21 @@ int main(int argc, char *argv[]) { foo_library::histogram_example(name); } + else if (example_type == "gauge") + { + foo_library::gauge_example(name); + } else { std::thread counter_example{&foo_library::counter_example, name}; std::thread observable_counter_example{&foo_library::observable_counter_example, name}; std::thread histogram_example{&foo_library::histogram_example, name}; + std::thread gauge_example{&foo_library::gauge_example, name}; counter_example.join(); observable_counter_example.join(); histogram_example.join(); + gauge_example.join(); } CleanupMetrics(); diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 60d59d1e28..8397cad5c9 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -93,15 +93,21 @@ int main(int argc, char *argv[]) { foo_library::histogram_example(name); } + else if (example_type == "gauge") + { + foo_library::gauge_example(name); + } else { std::thread counter_example{&foo_library::counter_example, name}; std::thread observable_counter_example{&foo_library::observable_counter_example, name}; std::thread histogram_example{&foo_library::histogram_example, name}; + std::thread gauge_example{&foo_library::gauge_example, name}; counter_example.join(); observable_counter_example.join(); histogram_example.join(); + gauge_example.join(); } CleanupMetrics(); diff --git a/examples/otlp/http_metric_main.cc b/examples/otlp/http_metric_main.cc index 53f5fe38bc..49348a2cd7 100644 --- a/examples/otlp/http_metric_main.cc +++ b/examples/otlp/http_metric_main.cc @@ -133,15 +133,21 @@ int main(int argc, char *argv[]) { foo_library::histogram_example(name); } + else if (example_type == "gauge") + { + foo_library::gauge_example(name); + } else { std::thread counter_example{&foo_library::counter_example, name}; std::thread observable_counter_example{&foo_library::observable_counter_example, name}; std::thread histogram_example{&foo_library::histogram_example, name}; + std::thread gauge_example{&foo_library::gauge_example, name}; counter_example.join(); observable_counter_example.join(); histogram_example.join(); + gauge_example.join(); } CleanupMetrics(); diff --git a/exporters/otlp/src/otlp_metric_utils.cc b/exporters/otlp/src/otlp_metric_utils.cc index da162d117e..c8502d65c5 100644 --- a/exporters/otlp/src/otlp_metric_utils.cc +++ b/exporters/otlp/src/otlp_metric_utils.cc @@ -297,6 +297,7 @@ sdk::metrics::AggregationTemporality OtlpMetricUtils::DeltaTemporalitySelector( case sdk::metrics::InstrumentType::kObservableCounter: case sdk::metrics::InstrumentType::kHistogram: case sdk::metrics::InstrumentType::kObservableGauge: + case sdk::metrics::InstrumentType::kGauge: return sdk::metrics::AggregationTemporality::kDelta; case sdk::metrics::InstrumentType::kUpDownCounter: case sdk::metrics::InstrumentType::kObservableUpDownCounter: @@ -320,6 +321,7 @@ sdk::metrics::AggregationTemporality OtlpMetricUtils::LowMemoryTemporalitySelect case sdk::metrics::InstrumentType::kHistogram: return sdk::metrics::AggregationTemporality::kDelta; case sdk::metrics::InstrumentType::kObservableCounter: + case sdk::metrics::InstrumentType::kGauge: case sdk::metrics::InstrumentType::kObservableGauge: case sdk::metrics::InstrumentType::kUpDownCounter: case sdk::metrics::InstrumentType::kObservableUpDownCounter: diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h index 4f3346707f..b33afb9d4e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h @@ -180,6 +180,7 @@ class DefaultAggregation return AggregationType::kSum; case InstrumentType::kHistogram: return AggregationType::kHistogram; + case InstrumentType::kGauge: case InstrumentType::kObservableGauge: return AggregationType::kLastValue; default: diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index d7c6870945..f377495eed 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -19,6 +19,7 @@ enum class InstrumentType kCounter, kHistogram, kUpDownCounter, + kGauge, kObservableCounter, kObservableGauge, kObservableUpDownCounter diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index b29bede012..68a009f64e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -75,6 +75,16 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view description = "", nostd::string_view unit = "") noexcept override; + nostd::unique_ptr> CreateInt64Gauge( + nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override; + + nostd::unique_ptr> CreateDoubleGauge( + nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override; + nostd::shared_ptr CreateInt64ObservableGauge( nostd::string_view name, nostd::string_view description = "", diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h index 1d5f8dbc55..1eceb579a6 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -101,6 +101,38 @@ class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::U void Add(double value, const opentelemetry::context::Context &context) noexcept override; }; +class LongGauge : public Synchronous, public opentelemetry::metrics::Gauge +{ +public: + LongGauge(const InstrumentDescriptor &instrument_descriptor, + std::unique_ptr storage); + + void Record(int64_t value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Record(int64_t value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; + + void Record(int64_t value) noexcept override; + void Record(int64_t value, const opentelemetry::context::Context &context) noexcept override; +}; + +class DoubleGauge : public Synchronous, public opentelemetry::metrics::Gauge +{ +public: + DoubleGauge(const InstrumentDescriptor &instrument_descriptor, + std::unique_ptr storage); + + void Record(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Record(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; + + void Record(double value) noexcept override; + void Record(double value, const opentelemetry::context::Context &context) noexcept override; +}; + class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogram { public: diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index c61f3a8d31..13b3090a83 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -186,6 +186,50 @@ opentelemetry::nostd::unique_ptr> Meter::CreateDouble new DoubleHistogram(instrument_descriptor, std::move(storage))}; } +opentelemetry::nostd::unique_ptr> Meter::CreateInt64Gauge( + opentelemetry::nostd::string_view name, + opentelemetry::nostd::string_view description, + opentelemetry::nostd::string_view unit) noexcept +{ + if (!ValidateInstrument(name, description, unit)) + { + OTEL_INTERNAL_LOG_ERROR("Meter::CreateInt64Gauge - failed. Invalid parameters." + << name << " " << description << " " << unit + << ". Measurements won't be recorded."); + return opentelemetry::nostd::unique_ptr>( + new metrics::NoopGauge(name, description, unit)); + } + InstrumentDescriptor instrument_descriptor = { + std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, + std::string{unit.data(), unit.size()}, InstrumentType::kGauge, + InstrumentValueType::kLong}; + auto storage = RegisterSyncMetricStorage(instrument_descriptor); + return opentelemetry::nostd::unique_ptr>{ + new LongGauge(instrument_descriptor, std::move(storage))}; +} + +opentelemetry::nostd::unique_ptr> Meter::CreateDoubleGauge( + opentelemetry::nostd::string_view name, + opentelemetry::nostd::string_view description, + opentelemetry::nostd::string_view unit) noexcept +{ + if (!ValidateInstrument(name, description, unit)) + { + OTEL_INTERNAL_LOG_ERROR("Meter::CreateDoubleGauge - failed. Invalid parameters." + << name << " " << description << " " << unit + << ". Measurements won't be recorded."); + return opentelemetry::nostd::unique_ptr>( + new metrics::NoopGauge(name, description, unit)); + } + InstrumentDescriptor instrument_descriptor = { + std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, + std::string{unit.data(), unit.size()}, InstrumentType::kGauge, + InstrumentValueType::kDouble}; + auto storage = RegisterSyncMetricStorage(instrument_descriptor); + return opentelemetry::nostd::unique_ptr>{ + new DoubleGauge(instrument_descriptor, std::move(storage))}; +} + opentelemetry::nostd::shared_ptr Meter::CreateInt64ObservableGauge(opentelemetry::nostd::string_view name, opentelemetry::nostd::string_view description, diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index 56429a079f..498c851431 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -292,6 +292,134 @@ void DoubleUpDownCounter::Add(double value, const opentelemetry::context::Contex return storage_->RecordDouble(value, context); } +LongGauge::LongGauge(const InstrumentDescriptor &instrument_descriptor, + std::unique_ptr storage) + : Synchronous(instrument_descriptor, std::move(storage)) +{ + if (!storage_) + { + OTEL_INTERNAL_LOG_ERROR( + "[LongGauge::LongGauge] - Error constructing LongGauge." + << "The metric storage is invalid for " << instrument_descriptor.name_); + } +} + +void LongGauge::Record(int64_t value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + auto context = opentelemetry::context::Context{}; + if (!storage_) + { + OTEL_INTERNAL_LOG_WARN( + "[LongGauge::Record(V,A)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); + return; + } + return storage_->RecordLong(value, attributes, context); +} + +void LongGauge::Record(int64_t value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + if (!storage_) + { + OTEL_INTERNAL_LOG_WARN( + "[LongGauge::Record(V,A,C)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); + return; + } + return storage_->RecordLong(value, attributes, context); +} + +void LongGauge::Record(int64_t value) noexcept +{ + auto context = opentelemetry::context::Context{}; + if (!storage_) + { + OTEL_INTERNAL_LOG_WARN("[LongGauge::Record(V)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); + return; + } + return storage_->RecordLong(value, context); +} + +void LongGauge::Record(int64_t value, const opentelemetry::context::Context &context) noexcept +{ + if (!storage_) + { + OTEL_INTERNAL_LOG_WARN( + "[LongGauge::Record(V,C)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); + return; + } + return storage_->RecordLong(value, context); +} + +DoubleGauge::DoubleGauge(const InstrumentDescriptor &instrument_descriptor, + std::unique_ptr storage) + : Synchronous(instrument_descriptor, std::move(storage)) +{ + if (!storage_) + { + OTEL_INTERNAL_LOG_ERROR( + "[DoubleGauge::DoubleGauge] - Error constructing DoubleUpDownCounter." + << "The metric storage is invalid for " << instrument_descriptor.name_); + } +} + +void DoubleGauge::Record(double value, + const opentelemetry::common::KeyValueIterable &attributes) noexcept +{ + if (!storage_) + { + OTEL_INTERNAL_LOG_WARN( + "[DoubleGauge::Record(V,A)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); + } + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, attributes, context); +} + +void DoubleGauge::Record(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + if (!storage_) + { + OTEL_INTERNAL_LOG_WARN( + "[DoubleGauge::Record(V,A,C)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); + return; + } + return storage_->RecordDouble(value, attributes, context); +} + +void DoubleGauge::Record(double value) noexcept +{ + if (!storage_) + { + OTEL_INTERNAL_LOG_WARN( + "[DoubleGauge::Record(V)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); + return; + } + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, context); +} + +void DoubleGauge::Record(double value, const opentelemetry::context::Context &context) noexcept +{ + if (!storage_) + { + OTEL_INTERNAL_LOG_WARN( + "[DoubleGauge::Record(V,C)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); + return; + } + return storage_->RecordDouble(value, context); +} + LongHistogram::LongHistogram(const InstrumentDescriptor &instrument_descriptor, std::unique_ptr storage) : Synchronous(instrument_descriptor, std::move(storage)) diff --git a/sdk/test/metrics/BUILD b/sdk/test/metrics/BUILD index 3cf379494d..993bb13ac5 100644 --- a/sdk/test/metrics/BUILD +++ b/sdk/test/metrics/BUILD @@ -145,6 +145,22 @@ cc_test( ], ) +cc_test( + name = "sync_metric_storage_gauge_test", + srcs = [ + "sync_metric_storage_gauge_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "metrics_common_test_utils", + "//sdk/src/resource", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "sync_metric_storage_histogram_test", srcs = [ diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index ccc31de689..9effa73a4c 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -19,6 +19,7 @@ foreach( cardinality_limit_test histogram_test sync_metric_storage_counter_test + sync_metric_storage_gauge_test sync_metric_storage_histogram_test sync_metric_storage_up_down_counter_test async_metric_storage_test diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index 723d29f20e..7852221783 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -105,6 +105,46 @@ TEST(SyncInstruments, DoubleUpDownCounter) counter.Add(10.10, opentelemetry::common::KeyValueIterableView({})); } +TEST(SyncInstruments, LongGauge) +{ + InstrumentDescriptor instrument_descriptor = {"long_gauge", "description", "1", + InstrumentType::kGauge, + InstrumentValueType::kLong}; + std::unique_ptr metric_storage(new SyncMultiMetricStorage()); + LongGauge gauge(instrument_descriptor, std::move(metric_storage)); + gauge.Record(10); + gauge.Record(10, opentelemetry::context::Context{}); + + gauge.Record(10, + opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{}); + gauge.Record(10, + opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}})); + gauge.Record(10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{}); + gauge.Record(10, opentelemetry::common::KeyValueIterableView({})); +} + +TEST(SyncInstruments, DoubleGauge) +{ + InstrumentDescriptor instrument_descriptor = {"double_gauge", "description", "1", + InstrumentType::kGauge, + InstrumentValueType::kDouble}; + std::unique_ptr metric_storage(new SyncMultiMetricStorage()); + DoubleGauge gauge(instrument_descriptor, std::move(metric_storage)); + gauge.Record(10.10); + gauge.Record(10.10, opentelemetry::context::Context{}); + + gauge.Record(10.10, + opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{}); + gauge.Record(10.10, + opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}})); + gauge.Record(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{}); + gauge.Record(10.10, opentelemetry::common::KeyValueIterableView({})); +} + TEST(SyncInstruments, LongHistogram) { InstrumentDescriptor instrument_descriptor = { diff --git a/sdk/test/metrics/sync_metric_storage_gauge_test.cc b/sdk/test/metrics/sync_metric_storage_gauge_test.cc new file mode 100644 index 0000000000..ed7c3f3df1 --- /dev/null +++ b/sdk/test/metrics/sync_metric_storage_gauge_test.cc @@ -0,0 +1,177 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "common.h" + +#include "opentelemetry/common/key_value_iterable_view.h" +#include "opentelemetry/nostd/shared_ptr.h" +#include "opentelemetry/sdk/metrics/instruments.h" +#include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" +#include "opentelemetry/sdk/metrics/view/attributes_processor.h" + +#include +#include +#include + +using namespace opentelemetry::sdk::metrics; +using namespace opentelemetry::common; +namespace nostd = opentelemetry::nostd; + +class WritableMetricStorageTestFixture : public ::testing::TestWithParam +{}; + +TEST_P(WritableMetricStorageTestFixture, LongGaugeLastValueAggregation) +{ + AggregationTemporality temporality = GetParam(); + auto sdk_start_ts = std::chrono::system_clock::now(); + InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge, + InstrumentValueType::kLong}; + std::map attributes_roomA = {{"Room.id", "Rack A"}}; + std::map attributes_roomB = {{"Room.id", "Rack B"}}; + + std::unique_ptr default_attributes_processor{ + new DefaultAttributesProcessor{}}; + opentelemetry::sdk::metrics::SyncMetricStorage storage( + instr_desc, AggregationType::kLastValue, default_attributes_processor.get(), + nullptr); + + int64_t bg_noise_level_1_roomA = 10; + int64_t bg_noise_level_1_roomB = 20; + + storage.RecordLong(bg_noise_level_1_roomA, + KeyValueIterableView>(attributes_roomA), + opentelemetry::context::Context{}); + storage.RecordLong(bg_noise_level_1_roomB, + KeyValueIterableView>(attributes_roomB), + opentelemetry::context::Context{}); + + int64_t bg_noise_level_2_roomA = 43; + int64_t bg_noise_level_2_roomB = 25; + + storage.RecordLong(bg_noise_level_2_roomA, + KeyValueIterableView>(attributes_roomA), + opentelemetry::context::Context{}); + storage.RecordLong(bg_noise_level_2_roomB, + KeyValueIterableView>(attributes_roomB), + opentelemetry::context::Context{}); + + + std::shared_ptr collector(new MockCollectorHandle(temporality)); + std::vector> collectors; + collectors.push_back(collector); + + // Some computation here + auto collection_ts = std::chrono::system_clock::now(); + size_t count_attributes = 0; + storage.Collect( + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) { + for (const auto &data_attr : metric_data.point_data_attr_) + { + auto lastvalue_data = opentelemetry::nostd::get(data_attr.point_data); + if (opentelemetry::nostd::get( + data_attr.attributes.find("Room.id")->second) == "Rack A") + { + if (temporality == AggregationTemporality::kCumulative) + { + EXPECT_EQ(opentelemetry::nostd::get(lastvalue_data.value_), + bg_noise_level_2_roomA); + } + count_attributes++; + } + else if (opentelemetry::nostd::get( + data_attr.attributes.find("Room.id")->second) == "Rack B") + { + if (temporality == AggregationTemporality::kCumulative) + { + EXPECT_EQ(opentelemetry::nostd::get(lastvalue_data.value_), + bg_noise_level_2_roomB); + } + count_attributes++; + } + } + return true; + }); + EXPECT_EQ(count_attributes, 2); // RackA and RackB +} + +INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestLong, + WritableMetricStorageTestFixture, + ::testing::Values(AggregationTemporality::kCumulative)); + +TEST_P(WritableMetricStorageTestFixture, DoubleGaugeLastValueAggregation) +{ + AggregationTemporality temporality = GetParam(); + auto sdk_start_ts = std::chrono::system_clock::now(); + InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge, + InstrumentValueType::kDouble}; + std::map attributes_roomA = {{"Room.id", "Rack A"}}; + std::map attributes_roomB = {{"Room.id", "Rack B"}}; + + std::unique_ptr default_attributes_processor{ + new DefaultAttributesProcessor{}}; + opentelemetry::sdk::metrics::SyncMetricStorage storage( + instr_desc, AggregationType::kLastValue, default_attributes_processor.get(), + nullptr); + + double bg_noise_level_1_roomA = 4.3; + double bg_noise_level_1_roomB = 2.5; + + storage.RecordDouble(bg_noise_level_1_roomA, + KeyValueIterableView>(attributes_roomA), + opentelemetry::context::Context{}); + storage.RecordDouble(bg_noise_level_1_roomB, + KeyValueIterableView>(attributes_roomB), + opentelemetry::context::Context{}); + + double bg_noise_level_2_roomA = 10.5; + double bg_noise_level_2_roomB = 20.5; + + storage.RecordDouble(bg_noise_level_2_roomA, + KeyValueIterableView>(attributes_roomA), + opentelemetry::context::Context{}); + storage.RecordDouble(bg_noise_level_2_roomB, + KeyValueIterableView>(attributes_roomB), + opentelemetry::context::Context{}); + + + std::shared_ptr collector(new MockCollectorHandle(temporality)); + std::vector> collectors; + collectors.push_back(collector); + + // Some computation here + auto collection_ts = std::chrono::system_clock::now(); + size_t count_attributes = 0; + storage.Collect( + collector.get(), collectors, sdk_start_ts, collection_ts, [&](const MetricData &metric_data) { + for (const auto &data_attr : metric_data.point_data_attr_) + { + auto lastvalue_data = opentelemetry::nostd::get(data_attr.point_data); + if (opentelemetry::nostd::get( + data_attr.attributes.find("Room.id")->second) == "Rack A") + { + if (temporality == AggregationTemporality::kCumulative) + { + EXPECT_DOUBLE_EQ(opentelemetry::nostd::get(lastvalue_data.value_), + bg_noise_level_2_roomA); + } + count_attributes++; + } + else if (opentelemetry::nostd::get( + data_attr.attributes.find("Room.id")->second) == "Rack B") + { + if (temporality == AggregationTemporality::kCumulative) + { + EXPECT_DOUBLE_EQ(opentelemetry::nostd::get(lastvalue_data.value_), + bg_noise_level_2_roomB); + } + count_attributes++; + } + } + return true; + }); + EXPECT_EQ(count_attributes, 2); // RackA and RackB +} + +INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestDouble, + WritableMetricStorageTestFixture, + ::testing::Values(AggregationTemporality::kCumulative)); From 095435a119f6a694aea09903c9007a92b9938d30 Mon Sep 17 00:00:00 2001 From: Uzay Uysal Date: Wed, 14 Aug 2024 22:40:39 +0200 Subject: [PATCH 2/8] Add ABI Version macro, update tests --- api/include/opentelemetry/metrics/meter.h | 2 ++ api/include/opentelemetry/metrics/noop.h | 5 +++++ .../opentelemetry/metrics/sync_instruments.h | 3 ++- .../common/metrics_foo_library/foo_library.cc | 2 ++ .../common/metrics_foo_library/foo_library.h | 2 ++ examples/metrics_simple/metrics_ostream.cc | 6 ++++++ examples/otlp/file_metric_main.cc | 6 ++++++ examples/otlp/grpc_metric_main.cc | 6 ++++++ examples/otlp/http_metric_main.cc | 6 ++++++ exporters/otlp/src/otlp_metric_utils.cc | 4 ++++ .../metrics/aggregation/default_aggregation.h | 2 ++ .../opentelemetry/sdk/metrics/instruments.h | 2 ++ sdk/include/opentelemetry/sdk/metrics/meter.h | 2 ++ .../opentelemetry/sdk/metrics/sync_instruments.h | 2 ++ sdk/src/metrics/meter.cc | 2 ++ sdk/src/metrics/sync_instruments.cc | 2 ++ sdk/test/metrics/sync_instruments_test.cc | 2 ++ .../metrics/sync_metric_storage_gauge_test.cc | 16 ++++++++++++++++ 18 files changed, 71 insertions(+), 1 deletion(-) diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index 463c1d1ee0..99467f39c0 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -94,6 +94,7 @@ class Meter nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 /** * Creates a Gauge with the passed characteristics and returns a unique_ptr to that Counter. * @@ -112,6 +113,7 @@ class Meter nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept = 0; +#endif /** * Creates a Asynchronous (Observable) Gauge with the passed characteristics and returns a diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 830877f16c..92b12a3df8 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -71,6 +71,7 @@ class NoopUpDownCounter : public UpDownCounter {} }; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 template class NoopGauge : public Gauge { @@ -88,6 +89,8 @@ class NoopGauge : public Gauge const context::Context & /* context */) noexcept override {} }; +#endif + class NoopObservableInstrument : public ObservableInstrument { @@ -158,6 +161,7 @@ class NoopMeter final : public Meter return nostd::unique_ptr>{new NoopHistogram(name, description, unit)}; } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 nostd::unique_ptr> CreateInt64Gauge( nostd::string_view name, nostd::string_view description = "", @@ -173,6 +177,7 @@ class NoopMeter final : public Meter { return nostd::unique_ptr>{new NoopGauge(name, description, unit)}; } +#endif nostd::shared_ptr CreateInt64ObservableGauge( nostd::string_view name, diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index f9b0fde521..9da9857403 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -247,6 +247,7 @@ class UpDownCounter : public SynchronousInstrument } }; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 /* A Gauge instrument that records values. */ template class Gauge : public SynchronousInstrument @@ -320,7 +321,7 @@ class Gauge : public SynchronousInstrument context); } }; - +#endif } // namespace metrics OPENTELEMETRY_END_NAMESPACE diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 0c600020de..2f07b765ef 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -110,6 +110,7 @@ void foo_library::histogram_example(const std::string &name) } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 void foo_library::gauge_example(const std::string &name) { std::string gauge_name = name + "_gauge"; @@ -126,3 +127,4 @@ void foo_library::gauge_example(const std::string &name) std::this_thread::sleep_for(std::chrono::milliseconds(250)); } } +#endif diff --git a/examples/common/metrics_foo_library/foo_library.h b/examples/common/metrics_foo_library/foo_library.h index 02511a5b66..05f0f16c23 100644 --- a/examples/common/metrics_foo_library/foo_library.h +++ b/examples/common/metrics_foo_library/foo_library.h @@ -11,5 +11,7 @@ class foo_library static void counter_example(const std::string &name); static void histogram_example(const std::string &name); static void observable_counter_example(const std::string &name); +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 static void gauge_example(const std::string &name); +#endif }; diff --git a/examples/metrics_simple/metrics_ostream.cc b/examples/metrics_simple/metrics_ostream.cc index 266a76d2e8..6d8c1ef6e0 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -148,21 +148,27 @@ int main(int argc, char **argv) { foo_library::histogram_example(name); } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 else if (example_type == "gauge") { foo_library::gauge_example(name); } +#endif else { std::thread counter_example{&foo_library::counter_example, name}; std::thread observable_counter_example{&foo_library::observable_counter_example, name}; std::thread histogram_example{&foo_library::histogram_example, name}; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 std::thread gauge_example{&foo_library::gauge_example, name}; +#endif counter_example.join(); observable_counter_example.join(); histogram_example.join(); +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 gauge_example.join(); +#endif } CleanupMetrics(); diff --git a/examples/otlp/file_metric_main.cc b/examples/otlp/file_metric_main.cc index 50e56d92d4..48be48171e 100644 --- a/examples/otlp/file_metric_main.cc +++ b/examples/otlp/file_metric_main.cc @@ -98,21 +98,27 @@ int main(int argc, char *argv[]) { foo_library::histogram_example(name); } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 else if (example_type == "gauge") { foo_library::gauge_example(name); } +#endif else { std::thread counter_example{&foo_library::counter_example, name}; std::thread observable_counter_example{&foo_library::observable_counter_example, name}; std::thread histogram_example{&foo_library::histogram_example, name}; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 std::thread gauge_example{&foo_library::gauge_example, name}; +#endif counter_example.join(); observable_counter_example.join(); histogram_example.join(); +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 gauge_example.join(); +#endif } CleanupMetrics(); diff --git a/examples/otlp/grpc_metric_main.cc b/examples/otlp/grpc_metric_main.cc index 8397cad5c9..804bb91014 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -93,21 +93,27 @@ int main(int argc, char *argv[]) { foo_library::histogram_example(name); } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 else if (example_type == "gauge") { foo_library::gauge_example(name); } +#endif else { std::thread counter_example{&foo_library::counter_example, name}; std::thread observable_counter_example{&foo_library::observable_counter_example, name}; std::thread histogram_example{&foo_library::histogram_example, name}; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 std::thread gauge_example{&foo_library::gauge_example, name}; +#endif counter_example.join(); observable_counter_example.join(); histogram_example.join(); +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 gauge_example.join(); +#endif } CleanupMetrics(); diff --git a/examples/otlp/http_metric_main.cc b/examples/otlp/http_metric_main.cc index 49348a2cd7..f7afc975ac 100644 --- a/examples/otlp/http_metric_main.cc +++ b/examples/otlp/http_metric_main.cc @@ -133,21 +133,27 @@ int main(int argc, char *argv[]) { foo_library::histogram_example(name); } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 else if (example_type == "gauge") { foo_library::gauge_example(name); } +#endif else { std::thread counter_example{&foo_library::counter_example, name}; std::thread observable_counter_example{&foo_library::observable_counter_example, name}; std::thread histogram_example{&foo_library::histogram_example, name}; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 std::thread gauge_example{&foo_library::gauge_example, name}; +#endif counter_example.join(); observable_counter_example.join(); histogram_example.join(); +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 gauge_example.join(); +#endif } CleanupMetrics(); diff --git a/exporters/otlp/src/otlp_metric_utils.cc b/exporters/otlp/src/otlp_metric_utils.cc index c8502d65c5..389663c3d1 100644 --- a/exporters/otlp/src/otlp_metric_utils.cc +++ b/exporters/otlp/src/otlp_metric_utils.cc @@ -297,7 +297,9 @@ sdk::metrics::AggregationTemporality OtlpMetricUtils::DeltaTemporalitySelector( case sdk::metrics::InstrumentType::kObservableCounter: case sdk::metrics::InstrumentType::kHistogram: case sdk::metrics::InstrumentType::kObservableGauge: +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 case sdk::metrics::InstrumentType::kGauge: +#endif return sdk::metrics::AggregationTemporality::kDelta; case sdk::metrics::InstrumentType::kUpDownCounter: case sdk::metrics::InstrumentType::kObservableUpDownCounter: @@ -321,7 +323,9 @@ sdk::metrics::AggregationTemporality OtlpMetricUtils::LowMemoryTemporalitySelect case sdk::metrics::InstrumentType::kHistogram: return sdk::metrics::AggregationTemporality::kDelta; case sdk::metrics::InstrumentType::kObservableCounter: +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 case sdk::metrics::InstrumentType::kGauge: +#endif case sdk::metrics::InstrumentType::kObservableGauge: case sdk::metrics::InstrumentType::kUpDownCounter: case sdk::metrics::InstrumentType::kObservableUpDownCounter: diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h index b33afb9d4e..340283d96e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h @@ -180,7 +180,9 @@ class DefaultAggregation return AggregationType::kSum; case InstrumentType::kHistogram: return AggregationType::kHistogram; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 case InstrumentType::kGauge: +#endif case InstrumentType::kObservableGauge: return AggregationType::kLastValue; default: diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index f377495eed..27047b7038 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -19,7 +19,9 @@ enum class InstrumentType kCounter, kHistogram, kUpDownCounter, +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 kGauge, +#endif kObservableCounter, kObservableGauge, kObservableUpDownCounter diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index 68a009f64e..dc026f5169 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -75,6 +75,7 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view description = "", nostd::string_view unit = "") noexcept override; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 nostd::unique_ptr> CreateInt64Gauge( nostd::string_view name, nostd::string_view description = "", @@ -84,6 +85,7 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override; +#endif nostd::shared_ptr CreateInt64ObservableGauge( nostd::string_view name, diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h index 1eceb579a6..e319d0c7e9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -101,6 +101,7 @@ class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::U void Add(double value, const opentelemetry::context::Context &context) noexcept override; }; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 class LongGauge : public Synchronous, public opentelemetry::metrics::Gauge { public: @@ -132,6 +133,7 @@ class DoubleGauge : public Synchronous, public opentelemetry::metrics::Gauge { diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 13b3090a83..e6d16eb588 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -186,6 +186,7 @@ opentelemetry::nostd::unique_ptr> Meter::CreateDouble new DoubleHistogram(instrument_descriptor, std::move(storage))}; } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 opentelemetry::nostd::unique_ptr> Meter::CreateInt64Gauge( opentelemetry::nostd::string_view name, opentelemetry::nostd::string_view description, @@ -229,6 +230,7 @@ opentelemetry::nostd::unique_ptr> Meter::CreateDoubleGaug return opentelemetry::nostd::unique_ptr>{ new DoubleGauge(instrument_descriptor, std::move(storage))}; } +#endif opentelemetry::nostd::shared_ptr Meter::CreateInt64ObservableGauge(opentelemetry::nostd::string_view name, diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index 498c851431..6bf0433f01 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -292,6 +292,7 @@ void DoubleUpDownCounter::Add(double value, const opentelemetry::context::Contex return storage_->RecordDouble(value, context); } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 LongGauge::LongGauge(const InstrumentDescriptor &instrument_descriptor, std::unique_ptr storage) : Synchronous(instrument_descriptor, std::move(storage)) @@ -419,6 +420,7 @@ void DoubleGauge::Record(double value, const opentelemetry::context::Context &co } return storage_->RecordDouble(value, context); } +#endif LongHistogram::LongHistogram(const InstrumentDescriptor &instrument_descriptor, std::unique_ptr storage) diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index 7852221783..daa0430881 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -105,6 +105,7 @@ TEST(SyncInstruments, DoubleUpDownCounter) counter.Add(10.10, opentelemetry::common::KeyValueIterableView({})); } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 TEST(SyncInstruments, LongGauge) { InstrumentDescriptor instrument_descriptor = {"long_gauge", "description", "1", @@ -144,6 +145,7 @@ TEST(SyncInstruments, DoubleGauge) opentelemetry::context::Context{}); gauge.Record(10.10, opentelemetry::common::KeyValueIterableView({})); } +#endif TEST(SyncInstruments, LongHistogram) { diff --git a/sdk/test/metrics/sync_metric_storage_gauge_test.cc b/sdk/test/metrics/sync_metric_storage_gauge_test.cc index ed7c3f3df1..07bab98be1 100644 --- a/sdk/test/metrics/sync_metric_storage_gauge_test.cc +++ b/sdk/test/metrics/sync_metric_storage_gauge_test.cc @@ -22,6 +22,7 @@ class WritableMetricStorageTestFixture : public ::testing::TestWithParam= 2 AggregationTemporality temporality = GetParam(); auto sdk_start_ts = std::chrono::system_clock::now(); InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge, @@ -33,6 +34,10 @@ TEST_P(WritableMetricStorageTestFixture, LongGaugeLastValueAggregation) new DefaultAttributesProcessor{}}; opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kLastValue, default_attributes_processor.get(), +#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW + ExemplarFilterType::kAlwaysOff, + ExemplarReservoir::GetNoExemplarReservoir(), +#endif nullptr); int64_t bg_noise_level_1_roomA = 10; @@ -92,6 +97,9 @@ TEST_P(WritableMetricStorageTestFixture, LongGaugeLastValueAggregation) return true; }); EXPECT_EQ(count_attributes, 2); // RackA and RackB +#else + EXPECT_TRUE(true); +#endif } INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestLong, @@ -100,6 +108,7 @@ INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestLong, TEST_P(WritableMetricStorageTestFixture, DoubleGaugeLastValueAggregation) { +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 AggregationTemporality temporality = GetParam(); auto sdk_start_ts = std::chrono::system_clock::now(); InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge, @@ -111,6 +120,10 @@ TEST_P(WritableMetricStorageTestFixture, DoubleGaugeLastValueAggregation) new DefaultAttributesProcessor{}}; opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kLastValue, default_attributes_processor.get(), +#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW + ExemplarFilterType::kAlwaysOff, + ExemplarReservoir::GetNoExemplarReservoir(), +#endif nullptr); double bg_noise_level_1_roomA = 4.3; @@ -170,6 +183,9 @@ TEST_P(WritableMetricStorageTestFixture, DoubleGaugeLastValueAggregation) return true; }); EXPECT_EQ(count_attributes, 2); // RackA and RackB +#else + EXPECT_TRUE(true); +#endif } INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestDouble, From bd8cb234495cfeca03c2c8ed26c94e55f90cd1a4 Mon Sep 17 00:00:00 2001 From: Uzay Uysal Date: Wed, 14 Aug 2024 23:13:54 +0200 Subject: [PATCH 3/8] Fix function description --- api/include/opentelemetry/metrics/meter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index 99467f39c0..59cfb87822 100644 --- a/api/include/opentelemetry/metrics/meter.h +++ b/api/include/opentelemetry/metrics/meter.h @@ -96,7 +96,7 @@ class Meter #if OPENTELEMETRY_ABI_VERSION_NO >= 2 /** - * Creates a Gauge with the passed characteristics and returns a unique_ptr to that Counter. + * Creates a Gauge with the passed characteristics and returns a unique_ptr to that Gauge. * * @param name the name of the new Gauge. * @param description a brief description of what the Gauge is used for. From eecf89f26e5320924d85649afbad3d2feb6eec84 Mon Sep 17 00:00:00 2001 From: Uzay Uysal Date: Fri, 16 Aug 2024 20:39:16 +0200 Subject: [PATCH 4/8] Fix formatting --- api/include/opentelemetry/metrics/noop.h | 23 +++---- .../opentelemetry/metrics/sync_instruments.h | 23 +++---- .../common/metrics_foo_library/foo_library.cc | 4 +- sdk/include/opentelemetry/sdk/metrics/meter.h | 2 +- .../sdk/metrics/sync_instruments.h | 16 ++--- sdk/src/metrics/meter.cc | 6 +- sdk/src/metrics/sync_instruments.cc | 61 ++++++++----------- sdk/test/metrics/sync_instruments_test.cc | 25 ++++---- .../metrics/sync_metric_storage_gauge_test.cc | 60 +++++++++--------- 9 files changed, 99 insertions(+), 121 deletions(-) diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 92b12a3df8..1d508b9387 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -77,21 +77,20 @@ class NoopGauge : public Gauge { public: NoopGauge(nostd::string_view /* name */, - nostd::string_view /* description */, - nostd::string_view /* unit */) noexcept + nostd::string_view /* description */, + nostd::string_view /* unit */) noexcept {} ~NoopGauge() override = default; void Record(T /* value */) noexcept override {} void Record(T /* value */, const context::Context & /* context */) noexcept override {} void Record(T /* value */, const common::KeyValueIterable & /* attributes */) noexcept override {} void Record(T /* value */, - const common::KeyValueIterable & /* attributes */, - const context::Context & /* context */) noexcept override + const common::KeyValueIterable & /* attributes */, + const context::Context & /* context */) noexcept override {} }; #endif - class NoopObservableInstrument : public ObservableInstrument { public: @@ -162,18 +161,16 @@ class NoopMeter final : public Meter } #if OPENTELEMETRY_ABI_VERSION_NO >= 2 - nostd::unique_ptr> CreateInt64Gauge( - nostd::string_view name, - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::unique_ptr> CreateInt64Gauge(nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{new NoopGauge(name, description, unit)}; } - nostd::unique_ptr> CreateDoubleGauge( - nostd::string_view name, - nostd::string_view description = "", - nostd::string_view unit = "") noexcept override + nostd::unique_ptr> CreateDoubleGauge(nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override { return nostd::unique_ptr>{new NoopGauge(name, description, unit)}; } diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 9da9857403..9eaec3352f 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -286,8 +286,8 @@ class Gauge : public SynchronousInstrument * @param context The explicit context to associate with this measurement. */ virtual void Record(T value, - const common::KeyValueIterable &attributes, - const context::Context &context) noexcept = 0; + const common::KeyValueIterable &attributes, + const context::Context &context) noexcept = 0; template ::value> * = nullptr> @@ -304,21 +304,22 @@ class Gauge : public SynchronousInstrument } void Record(T value, - std::initializer_list> - attributes) noexcept + std::initializer_list> + attributes) noexcept { this->Record(value, nostd::span>{ - attributes.begin(), attributes.end()}); + attributes.begin(), attributes.end()}); } - void Record(T value, - std::initializer_list> attributes, - const context::Context &context) noexcept + void Record( + T value, + std::initializer_list> attributes, + const context::Context &context) noexcept { this->Record(value, - nostd::span>{ - attributes.begin(), attributes.end()}, - context); + nostd::span>{ + attributes.begin(), attributes.end()}, + context); } }; #endif diff --git a/examples/common/metrics_foo_library/foo_library.cc b/examples/common/metrics_foo_library/foo_library.cc index 2f07b765ef..e288615293 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -106,8 +106,6 @@ void foo_library::histogram_example(const std::string &name) histogram_counter->Record(val, labelkv, context); std::this_thread::sleep_for(std::chrono::milliseconds(250)); } - - } #if OPENTELEMETRY_ABI_VERSION_NO >= 2 @@ -120,7 +118,7 @@ void foo_library::gauge_example(const std::string &name) auto context = opentelemetry::context::Context{}; for (uint32_t i = 0; i < 20; ++i) { - int64_t val = (rand() % 100) + 100; + int64_t val = (rand() % 100) + 100; std::map labels = get_random_attr(); auto labelkv = opentelemetry::common::KeyValueIterableView{labels}; gauge->Record(val, labelkv, context); diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index dc026f5169..2e9153e151 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -85,7 +85,7 @@ class Meter final : public opentelemetry::metrics::Meter nostd::string_view name, nostd::string_view description = "", nostd::string_view unit = "") noexcept override; -#endif +#endif nostd::shared_ptr CreateInt64ObservableGauge( nostd::string_view name, diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h index e319d0c7e9..4e95ddfbc9 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -106,13 +106,13 @@ class LongGauge : public Synchronous, public opentelemetry::metrics::Gauge storage); + std::unique_ptr storage); void Record(int64_t value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; void Record(int64_t value, - const opentelemetry::common::KeyValueIterable &attributes, - const opentelemetry::context::Context &context) noexcept override; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Record(int64_t value) noexcept override; void Record(int64_t value, const opentelemetry::context::Context &context) noexcept override; @@ -122,13 +122,13 @@ class DoubleGauge : public Synchronous, public opentelemetry::metrics::Gauge storage); + std::unique_ptr storage); void Record(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + const opentelemetry::common::KeyValueIterable &attributes) noexcept override; void Record(double value, - const opentelemetry::common::KeyValueIterable &attributes, - const opentelemetry::context::Context &context) noexcept override; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Record(double value) noexcept override; void Record(double value, const opentelemetry::context::Context &context) noexcept override; diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index e6d16eb588..6d09f264db 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -202,8 +202,7 @@ opentelemetry::nostd::unique_ptr> Meter::CreateInt64Gaug } InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, - std::string{unit.data(), unit.size()}, InstrumentType::kGauge, - InstrumentValueType::kLong}; + std::string{unit.data(), unit.size()}, InstrumentType::kGauge, InstrumentValueType::kLong}; auto storage = RegisterSyncMetricStorage(instrument_descriptor); return opentelemetry::nostd::unique_ptr>{ new LongGauge(instrument_descriptor, std::move(storage))}; @@ -224,8 +223,7 @@ opentelemetry::nostd::unique_ptr> Meter::CreateDoubleGaug } InstrumentDescriptor instrument_descriptor = { std::string{name.data(), name.size()}, std::string{description.data(), description.size()}, - std::string{unit.data(), unit.size()}, InstrumentType::kGauge, - InstrumentValueType::kDouble}; + std::string{unit.data(), unit.size()}, InstrumentType::kGauge, InstrumentValueType::kDouble}; auto storage = RegisterSyncMetricStorage(instrument_descriptor); return opentelemetry::nostd::unique_ptr>{ new DoubleGauge(instrument_descriptor, std::move(storage))}; diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index 6bf0433f01..de66e93054 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -294,40 +294,37 @@ void DoubleUpDownCounter::Add(double value, const opentelemetry::context::Contex #if OPENTELEMETRY_ABI_VERSION_NO >= 2 LongGauge::LongGauge(const InstrumentDescriptor &instrument_descriptor, - std::unique_ptr storage) + std::unique_ptr storage) : Synchronous(instrument_descriptor, std::move(storage)) { if (!storage_) { - OTEL_INTERNAL_LOG_ERROR( - "[LongGauge::LongGauge] - Error constructing LongGauge." - << "The metric storage is invalid for " << instrument_descriptor.name_); + OTEL_INTERNAL_LOG_ERROR("[LongGauge::LongGauge] - Error constructing LongGauge." + << "The metric storage is invalid for " << instrument_descriptor.name_); } } void LongGauge::Record(int64_t value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept + const opentelemetry::common::KeyValueIterable &attributes) noexcept { auto context = opentelemetry::context::Context{}; if (!storage_) { - OTEL_INTERNAL_LOG_WARN( - "[LongGauge::Record(V,A)] Value not recorded - invalid storage for: " - << instrument_descriptor_.name_); + OTEL_INTERNAL_LOG_WARN("[LongGauge::Record(V,A)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); return; } return storage_->RecordLong(value, attributes, context); } void LongGauge::Record(int64_t value, - const opentelemetry::common::KeyValueIterable &attributes, - const opentelemetry::context::Context &context) noexcept + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept { if (!storage_) { - OTEL_INTERNAL_LOG_WARN( - "[LongGauge::Record(V,A,C)] Value not recorded - invalid storage for: " - << instrument_descriptor_.name_); + OTEL_INTERNAL_LOG_WARN("[LongGauge::Record(V,A,C)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); return; } return storage_->RecordLong(value, attributes, context); @@ -349,48 +346,44 @@ void LongGauge::Record(int64_t value, const opentelemetry::context::Context &con { if (!storage_) { - OTEL_INTERNAL_LOG_WARN( - "[LongGauge::Record(V,C)] Value not recorded - invalid storage for: " - << instrument_descriptor_.name_); + OTEL_INTERNAL_LOG_WARN("[LongGauge::Record(V,C)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); return; } return storage_->RecordLong(value, context); } DoubleGauge::DoubleGauge(const InstrumentDescriptor &instrument_descriptor, - std::unique_ptr storage) + std::unique_ptr storage) : Synchronous(instrument_descriptor, std::move(storage)) { if (!storage_) { - OTEL_INTERNAL_LOG_ERROR( - "[DoubleGauge::DoubleGauge] - Error constructing DoubleUpDownCounter." - << "The metric storage is invalid for " << instrument_descriptor.name_); + OTEL_INTERNAL_LOG_ERROR("[DoubleGauge::DoubleGauge] - Error constructing DoubleUpDownCounter." + << "The metric storage is invalid for " << instrument_descriptor.name_); } } void DoubleGauge::Record(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept + const opentelemetry::common::KeyValueIterable &attributes) noexcept { if (!storage_) { - OTEL_INTERNAL_LOG_WARN( - "[DoubleGauge::Record(V,A)] Value not recorded - invalid storage for: " - << instrument_descriptor_.name_); + OTEL_INTERNAL_LOG_WARN("[DoubleGauge::Record(V,A)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); } auto context = opentelemetry::context::Context{}; return storage_->RecordDouble(value, attributes, context); } void DoubleGauge::Record(double value, - const opentelemetry::common::KeyValueIterable &attributes, - const opentelemetry::context::Context &context) noexcept + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept { if (!storage_) { - OTEL_INTERNAL_LOG_WARN( - "[DoubleGauge::Record(V,A,C)] Value not recorded - invalid storage for: " - << instrument_descriptor_.name_); + OTEL_INTERNAL_LOG_WARN("[DoubleGauge::Record(V,A,C)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); return; } return storage_->RecordDouble(value, attributes, context); @@ -400,9 +393,8 @@ void DoubleGauge::Record(double value) noexcept { if (!storage_) { - OTEL_INTERNAL_LOG_WARN( - "[DoubleGauge::Record(V)] Value not recorded - invalid storage for: " - << instrument_descriptor_.name_); + OTEL_INTERNAL_LOG_WARN("[DoubleGauge::Record(V)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); return; } auto context = opentelemetry::context::Context{}; @@ -413,9 +405,8 @@ void DoubleGauge::Record(double value, const opentelemetry::context::Context &co { if (!storage_) { - OTEL_INTERNAL_LOG_WARN( - "[DoubleGauge::Record(V,C)] Value not recorded - invalid storage for: " - << instrument_descriptor_.name_); + OTEL_INTERNAL_LOG_WARN("[DoubleGauge::Record(V,C)] Value not recorded - invalid storage for: " + << instrument_descriptor_.name_); return; } return storage_->RecordDouble(value, context); diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index daa0430881..b5dd09f70e 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -109,40 +109,37 @@ TEST(SyncInstruments, DoubleUpDownCounter) TEST(SyncInstruments, LongGauge) { InstrumentDescriptor instrument_descriptor = {"long_gauge", "description", "1", - InstrumentType::kGauge, - InstrumentValueType::kLong}; + InstrumentType::kGauge, InstrumentValueType::kLong}; std::unique_ptr metric_storage(new SyncMultiMetricStorage()); LongGauge gauge(instrument_descriptor, std::move(metric_storage)); gauge.Record(10); gauge.Record(10, opentelemetry::context::Context{}); + gauge.Record(10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{}); gauge.Record(10, - opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), - opentelemetry::context::Context{}); - gauge.Record(10, - opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}})); + opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}})); gauge.Record(10, opentelemetry::common::KeyValueIterableView({}), - opentelemetry::context::Context{}); + opentelemetry::context::Context{}); gauge.Record(10, opentelemetry::common::KeyValueIterableView({})); } TEST(SyncInstruments, DoubleGauge) { - InstrumentDescriptor instrument_descriptor = {"double_gauge", "description", "1", - InstrumentType::kGauge, - InstrumentValueType::kDouble}; + InstrumentDescriptor instrument_descriptor = { + "double_gauge", "description", "1", InstrumentType::kGauge, InstrumentValueType::kDouble}; std::unique_ptr metric_storage(new SyncMultiMetricStorage()); DoubleGauge gauge(instrument_descriptor, std::move(metric_storage)); gauge.Record(10.10); gauge.Record(10.10, opentelemetry::context::Context{}); gauge.Record(10.10, - opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), - opentelemetry::context::Context{}); + opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{}); gauge.Record(10.10, - opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}})); + opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}})); gauge.Record(10.10, opentelemetry::common::KeyValueIterableView({}), - opentelemetry::context::Context{}); + opentelemetry::context::Context{}); gauge.Record(10.10, opentelemetry::common::KeyValueIterableView({})); } #endif diff --git a/sdk/test/metrics/sync_metric_storage_gauge_test.cc b/sdk/test/metrics/sync_metric_storage_gauge_test.cc index 07bab98be1..f826755af3 100644 --- a/sdk/test/metrics/sync_metric_storage_gauge_test.cc +++ b/sdk/test/metrics/sync_metric_storage_gauge_test.cc @@ -23,21 +23,20 @@ class WritableMetricStorageTestFixture : public ::testing::TestWithParam= 2 - AggregationTemporality temporality = GetParam(); - auto sdk_start_ts = std::chrono::system_clock::now(); - InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge, - InstrumentValueType::kLong}; - std::map attributes_roomA = {{"Room.id", "Rack A"}}; - std::map attributes_roomB = {{"Room.id", "Rack B"}}; + AggregationTemporality temporality = GetParam(); + auto sdk_start_ts = std::chrono::system_clock::now(); + InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge, + InstrumentValueType::kLong}; + std::map attributes_roomA = {{"Room.id", "Rack A"}}; + std::map attributes_roomB = {{"Room.id", "Rack B"}}; std::unique_ptr default_attributes_processor{ new DefaultAttributesProcessor{}}; opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kLastValue, default_attributes_processor.get(), -#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW - ExemplarFilterType::kAlwaysOff, - ExemplarReservoir::GetNoExemplarReservoir(), -#endif +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW + ExemplarFilterType::kAlwaysOff, ExemplarReservoir::GetNoExemplarReservoir(), +# endif nullptr); int64_t bg_noise_level_1_roomA = 10; @@ -60,7 +59,6 @@ TEST_P(WritableMetricStorageTestFixture, LongGaugeLastValueAggregation) KeyValueIterableView>(attributes_roomB), opentelemetry::context::Context{}); - std::shared_ptr collector(new MockCollectorHandle(temporality)); std::vector> collectors; collectors.push_back(collector); @@ -109,43 +107,41 @@ INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestLong, TEST_P(WritableMetricStorageTestFixture, DoubleGaugeLastValueAggregation) { #if OPENTELEMETRY_ABI_VERSION_NO >= 2 - AggregationTemporality temporality = GetParam(); - auto sdk_start_ts = std::chrono::system_clock::now(); - InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge, - InstrumentValueType::kDouble}; - std::map attributes_roomA = {{"Room.id", "Rack A"}}; - std::map attributes_roomB = {{"Room.id", "Rack B"}}; + AggregationTemporality temporality = GetParam(); + auto sdk_start_ts = std::chrono::system_clock::now(); + InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kGauge, + InstrumentValueType::kDouble}; + std::map attributes_roomA = {{"Room.id", "Rack A"}}; + std::map attributes_roomB = {{"Room.id", "Rack B"}}; std::unique_ptr default_attributes_processor{ new DefaultAttributesProcessor{}}; opentelemetry::sdk::metrics::SyncMetricStorage storage( instr_desc, AggregationType::kLastValue, default_attributes_processor.get(), -#ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW - ExemplarFilterType::kAlwaysOff, - ExemplarReservoir::GetNoExemplarReservoir(), -#endif +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW + ExemplarFilterType::kAlwaysOff, ExemplarReservoir::GetNoExemplarReservoir(), +# endif nullptr); double bg_noise_level_1_roomA = 4.3; double bg_noise_level_1_roomB = 2.5; storage.RecordDouble(bg_noise_level_1_roomA, - KeyValueIterableView>(attributes_roomA), - opentelemetry::context::Context{}); + KeyValueIterableView>(attributes_roomA), + opentelemetry::context::Context{}); storage.RecordDouble(bg_noise_level_1_roomB, - KeyValueIterableView>(attributes_roomB), - opentelemetry::context::Context{}); + KeyValueIterableView>(attributes_roomB), + opentelemetry::context::Context{}); double bg_noise_level_2_roomA = 10.5; double bg_noise_level_2_roomB = 20.5; storage.RecordDouble(bg_noise_level_2_roomA, - KeyValueIterableView>(attributes_roomA), - opentelemetry::context::Context{}); + KeyValueIterableView>(attributes_roomA), + opentelemetry::context::Context{}); storage.RecordDouble(bg_noise_level_2_roomB, - KeyValueIterableView>(attributes_roomB), - opentelemetry::context::Context{}); - + KeyValueIterableView>(attributes_roomB), + opentelemetry::context::Context{}); std::shared_ptr collector(new MockCollectorHandle(temporality)); std::vector> collectors; @@ -165,7 +161,7 @@ TEST_P(WritableMetricStorageTestFixture, DoubleGaugeLastValueAggregation) if (temporality == AggregationTemporality::kCumulative) { EXPECT_DOUBLE_EQ(opentelemetry::nostd::get(lastvalue_data.value_), - bg_noise_level_2_roomA); + bg_noise_level_2_roomA); } count_attributes++; } @@ -175,7 +171,7 @@ TEST_P(WritableMetricStorageTestFixture, DoubleGaugeLastValueAggregation) if (temporality == AggregationTemporality::kCumulative) { EXPECT_DOUBLE_EQ(opentelemetry::nostd::get(lastvalue_data.value_), - bg_noise_level_2_roomB); + bg_noise_level_2_roomB); } count_attributes++; } From 60df5325840dcd6df104f7c88ed12a4c38d2b357 Mon Sep 17 00:00:00 2001 From: Uzay Uysal Date: Tue, 15 Oct 2024 09:57:55 -0400 Subject: [PATCH 5/8] Remove ABI macros from SDK. --- exporters/otlp/src/otlp_metric_utils.cc | 4 ---- .../sdk/metrics/aggregation/default_aggregation.h | 2 -- sdk/include/opentelemetry/sdk/metrics/instruments.h | 2 -- 3 files changed, 8 deletions(-) diff --git a/exporters/otlp/src/otlp_metric_utils.cc b/exporters/otlp/src/otlp_metric_utils.cc index 389663c3d1..c8502d65c5 100644 --- a/exporters/otlp/src/otlp_metric_utils.cc +++ b/exporters/otlp/src/otlp_metric_utils.cc @@ -297,9 +297,7 @@ sdk::metrics::AggregationTemporality OtlpMetricUtils::DeltaTemporalitySelector( case sdk::metrics::InstrumentType::kObservableCounter: case sdk::metrics::InstrumentType::kHistogram: case sdk::metrics::InstrumentType::kObservableGauge: -#if OPENTELEMETRY_ABI_VERSION_NO >= 2 case sdk::metrics::InstrumentType::kGauge: -#endif return sdk::metrics::AggregationTemporality::kDelta; case sdk::metrics::InstrumentType::kUpDownCounter: case sdk::metrics::InstrumentType::kObservableUpDownCounter: @@ -323,9 +321,7 @@ sdk::metrics::AggregationTemporality OtlpMetricUtils::LowMemoryTemporalitySelect case sdk::metrics::InstrumentType::kHistogram: return sdk::metrics::AggregationTemporality::kDelta; case sdk::metrics::InstrumentType::kObservableCounter: -#if OPENTELEMETRY_ABI_VERSION_NO >= 2 case sdk::metrics::InstrumentType::kGauge: -#endif case sdk::metrics::InstrumentType::kObservableGauge: case sdk::metrics::InstrumentType::kUpDownCounter: case sdk::metrics::InstrumentType::kObservableUpDownCounter: diff --git a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h index 340283d96e..b33afb9d4e 100644 --- a/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h +++ b/sdk/include/opentelemetry/sdk/metrics/aggregation/default_aggregation.h @@ -180,9 +180,7 @@ class DefaultAggregation return AggregationType::kSum; case InstrumentType::kHistogram: return AggregationType::kHistogram; -#if OPENTELEMETRY_ABI_VERSION_NO >= 2 case InstrumentType::kGauge: -#endif case InstrumentType::kObservableGauge: return AggregationType::kLastValue; default: diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index 27047b7038..f377495eed 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -19,9 +19,7 @@ enum class InstrumentType kCounter, kHistogram, kUpDownCounter, -#if OPENTELEMETRY_ABI_VERSION_NO >= 2 kGauge, -#endif kObservableCounter, kObservableGauge, kObservableUpDownCounter From 855f86d82e4d3926856e80e6ee2985e1e5d28933 Mon Sep 17 00:00:00 2001 From: Uzay Uysal Date: Tue, 29 Oct 2024 09:47:44 -0400 Subject: [PATCH 6/8] Add error log for gauge delta temporality. --- sdk/src/metrics/state/metric_collector.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sdk/src/metrics/state/metric_collector.cc b/sdk/src/metrics/state/metric_collector.cc index a790a6b6a1..cb871886a7 100644 --- a/sdk/src/metrics/state/metric_collector.cc +++ b/sdk/src/metrics/state/metric_collector.cc @@ -35,7 +35,14 @@ MetricCollector::MetricCollector(opentelemetry::sdk::metrics::MeterContext *cont AggregationTemporality MetricCollector::GetAggregationTemporality( InstrumentType instrument_type) noexcept { - return metric_reader_->GetAggregationTemporality(instrument_type); + auto aggregation_temporality = metric_reader_->GetAggregationTemporality(instrument_type); + if(aggregation_temporality == AggregationTemporality::kDelta && instrument_type == InstrumentType::kGauge) { + OTEL_INTERNAL_LOG_ERROR("[MetricCollector::GetAggregationTemporality] - Error getting aggregation temporality." + << "Delta temporality for Synchronous Gauge is currently not supported, using cumulative temporality"); + + return AggregationTemporality::kCumulative; + } + return aggregation_temporality; } bool MetricCollector::Collect( From f84a896a619a3eb79d6f5ac2e0855e18dd072301 Mon Sep 17 00:00:00 2001 From: Uzay Uysal Date: Tue, 29 Oct 2024 09:54:20 -0400 Subject: [PATCH 7/8] Fix formatting --- sdk/src/metrics/state/metric_collector.cc | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sdk/src/metrics/state/metric_collector.cc b/sdk/src/metrics/state/metric_collector.cc index cb871886a7..ba5136b375 100644 --- a/sdk/src/metrics/state/metric_collector.cc +++ b/sdk/src/metrics/state/metric_collector.cc @@ -36,10 +36,14 @@ AggregationTemporality MetricCollector::GetAggregationTemporality( InstrumentType instrument_type) noexcept { auto aggregation_temporality = metric_reader_->GetAggregationTemporality(instrument_type); - if(aggregation_temporality == AggregationTemporality::kDelta && instrument_type == InstrumentType::kGauge) { - OTEL_INTERNAL_LOG_ERROR("[MetricCollector::GetAggregationTemporality] - Error getting aggregation temporality." - << "Delta temporality for Synchronous Gauge is currently not supported, using cumulative temporality"); - + if (aggregation_temporality == AggregationTemporality::kDelta && + instrument_type == InstrumentType::kGauge) + { + OTEL_INTERNAL_LOG_ERROR( + "[MetricCollector::GetAggregationTemporality] - Error getting aggregation temporality." + << "Delta temporality for Synchronous Gauge is currently not supported, using cumulative " + "temporality"); + return AggregationTemporality::kCumulative; } return aggregation_temporality; From 56bdf77f7ebc0099aea2a3e0cf50d489ca83d39b Mon Sep 17 00:00:00 2001 From: Marc Alff Date: Wed, 30 Oct 2024 21:49:45 +0100 Subject: [PATCH 8/8] Apply suggestions from code review Move kGauge to the end, for better ABI compatibility. --- sdk/include/opentelemetry/sdk/metrics/instruments.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/instruments.h b/sdk/include/opentelemetry/sdk/metrics/instruments.h index f377495eed..76ad07b2e3 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -19,10 +19,10 @@ enum class InstrumentType kCounter, kHistogram, kUpDownCounter, - kGauge, kObservableCounter, kObservableGauge, - kObservableUpDownCounter + kObservableUpDownCounter, + kGauge }; enum class InstrumentClass