diff --git a/api/include/opentelemetry/metrics/meter.h b/api/include/opentelemetry/metrics/meter.h index ced5890310..59cfb87822 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,27 @@ 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 Gauge. + * + * @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; +#endif + /** * 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..1d508b9387 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -71,6 +71,26 @@ class NoopUpDownCounter : public UpDownCounter {} }; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 +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 + {} +}; +#endif + class NoopObservableInstrument : public ObservableInstrument { public: @@ -140,6 +160,22 @@ 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 = "", + 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)}; + } +#endif + 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..9eaec3352f 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -247,5 +247,82 @@ class UpDownCounter : public SynchronousInstrument } }; +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 +/* 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); + } +}; +#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 c4be2b530f..e288615293 100644 --- a/examples/common/metrics_foo_library/foo_library.cc +++ b/examples/common/metrics_foo_library/foo_library.cc @@ -107,3 +107,22 @@ void foo_library::histogram_example(const std::string &name) std::this_thread::sleep_for(std::chrono::milliseconds(250)); } } + +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 +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)); + } +} +#endif diff --git a/examples/common/metrics_foo_library/foo_library.h b/examples/common/metrics_foo_library/foo_library.h index 217f91671b..05f0f16c23 100644 --- a/examples/common/metrics_foo_library/foo_library.h +++ b/examples/common/metrics_foo_library/foo_library.h @@ -11,4 +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 5bdc7339eb..6d8c1ef6e0 100644 --- a/examples/metrics_simple/metrics_ostream.cc +++ b/examples/metrics_simple/metrics_ostream.cc @@ -148,15 +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 ce259005be..48be48171e 100644 --- a/examples/otlp/file_metric_main.cc +++ b/examples/otlp/file_metric_main.cc @@ -98,15 +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 60d59d1e28..804bb91014 100644 --- a/examples/otlp/grpc_metric_main.cc +++ b/examples/otlp/grpc_metric_main.cc @@ -93,15 +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 53f5fe38bc..f7afc975ac 100644 --- a/examples/otlp/http_metric_main.cc +++ b/examples/otlp/http_metric_main.cc @@ -133,15 +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 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..76ad07b2e3 100644 --- a/sdk/include/opentelemetry/sdk/metrics/instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/instruments.h @@ -21,7 +21,8 @@ enum class InstrumentType kUpDownCounter, kObservableCounter, kObservableGauge, - kObservableUpDownCounter + kObservableUpDownCounter, + kGauge }; enum class InstrumentClass diff --git a/sdk/include/opentelemetry/sdk/metrics/meter.h b/sdk/include/opentelemetry/sdk/metrics/meter.h index b29bede012..2e9153e151 100644 --- a/sdk/include/opentelemetry/sdk/metrics/meter.h +++ b/sdk/include/opentelemetry/sdk/metrics/meter.h @@ -75,6 +75,18 @@ 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 = "", + nostd::string_view unit = "") noexcept override; + + nostd::unique_ptr> CreateDoubleGauge( + nostd::string_view name, + nostd::string_view description = "", + nostd::string_view unit = "") noexcept override; +#endif + 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 039a37e748..ff5eaeb282 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -100,6 +100,40 @@ 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: + 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; +}; +#endif + 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..6d09f264db 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))}; } +#if OPENTELEMETRY_ABI_VERSION_NO >= 2 +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))}; +} +#endif + opentelemetry::nostd::shared_ptr Meter::CreateInt64ObservableGauge(opentelemetry::nostd::string_view name, opentelemetry::nostd::string_view description, diff --git a/sdk/src/metrics/state/metric_collector.cc b/sdk/src/metrics/state/metric_collector.cc index 2a799b7fda..35d5e53939 100644 --- a/sdk/src/metrics/state/metric_collector.cc +++ b/sdk/src/metrics/state/metric_collector.cc @@ -36,7 +36,18 @@ 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; } MetricProducer::Result MetricCollector::Produce() noexcept diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index 56429a079f..de66e93054 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -292,6 +292,127 @@ 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)) +{ + 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); +} +#endif + LongHistogram::LongHistogram(const InstrumentDescriptor &instrument_descriptor, std::unique_ptr storage) : Synchronous(instrument_descriptor, std::move(storage)) 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 fb59e1d033..22a05988dc 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -112,6 +112,45 @@ 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", + 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({})); +} +#endif + 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..f826755af3 --- /dev/null +++ b/sdk/test/metrics/sync_metric_storage_gauge_test.cc @@ -0,0 +1,189 @@ +// 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) +{ +#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::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 + 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 +#else + EXPECT_TRUE(true); +#endif +} + +INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestLong, + WritableMetricStorageTestFixture, + ::testing::Values(AggregationTemporality::kCumulative)); + +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"}}; + + 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 + 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 +#else + EXPECT_TRUE(true); +#endif +} + +INSTANTIATE_TEST_SUITE_P(WritableMetricStorageTestDouble, + WritableMetricStorageTestFixture, + ::testing::Values(AggregationTemporality::kCumulative));