diff --git a/api/include/opentelemetry/metrics/noop.h b/api/include/opentelemetry/metrics/noop.h index 19b9443a7d..3332384ee2 100644 --- a/api/include/opentelemetry/metrics/noop.h +++ b/api/include/opentelemetry/metrics/noop.h @@ -24,7 +24,12 @@ class NoopCounter : public Counter nostd::string_view unit) noexcept {} void Add(T value) noexcept override {} + void Add(T value, const opentelemetry::context::Context &context) noexcept override {} void Add(T value, const common::KeyValueIterable &attributes) noexcept override {} + void Add(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override + {} }; template @@ -35,8 +40,11 @@ class NoopHistogram : public Histogram nostd::string_view description, nostd::string_view unit) noexcept {} - void Record(T value) noexcept override {} - void Record(T value, const common::KeyValueIterable &attributes) noexcept override {} + void Record(T value, const opentelemetry::context::Context &context) noexcept override {} + void Record(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override + {} }; template @@ -48,7 +56,12 @@ class NoopUpDownCounter : public UpDownCounter nostd::string_view unit) noexcept {} void Add(T value) noexcept override {} + void Add(T value, const opentelemetry::context::Context &context) noexcept override {} void Add(T value, const common::KeyValueIterable &attributes) noexcept override {} + void Add(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override + {} }; template diff --git a/api/include/opentelemetry/metrics/sync_instruments.h b/api/include/opentelemetry/metrics/sync_instruments.h index 35cb8621ab..e8239743a1 100644 --- a/api/include/opentelemetry/metrics/sync_instruments.h +++ b/api/include/opentelemetry/metrics/sync_instruments.h @@ -6,6 +6,7 @@ # include "opentelemetry/common/attribute_value.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/context/context.h" # include "opentelemetry/nostd/span.h" # include "opentelemetry/nostd/string_view.h" # include "opentelemetry/nostd/type_traits.h" @@ -29,6 +30,8 @@ class Counter : public SynchronousInstrument */ virtual void Add(T value) noexcept = 0; + virtual void Add(T value, const opentelemetry::context::Context &context) noexcept = 0; + /** * Add adds the value to the counter's sum. The attributes should contain * the keys and values to be associated with this value. Counters only @@ -40,19 +43,44 @@ class Counter : public SynchronousInstrument virtual void Add(T value, const common::KeyValueIterable &attributes) noexcept = 0; + virtual void Add(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + template ::value> * = nullptr> void Add(T value, const U &attributes) noexcept { - this->Add(value, common::KeyValueIterableView{attributes}); + auto context = opentelemetry::context::Context{}; + this->Add(value, common::KeyValueIterableView{attributes}, context); + } + + template ::value> * = nullptr> + void Add(T value, const U &attributes, const opentelemetry::context::Context &context) noexcept + { + this->Add(value, common::KeyValueIterableView{attributes}, context); } void Add(T value, std::initializer_list> attributes) noexcept { - this->Add(value, nostd::span>{ - attributes.begin(), attributes.end()}); + auto context = opentelemetry::context::Context{}; + this->Add(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); + } + + void Add(T value, + std::initializer_list> attributes, + const opentelemetry::context::Context &context) noexcept + { + this->Add(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); } }; @@ -67,7 +95,7 @@ class Histogram : public SynchronousInstrument * * @param value The increment amount. May be positive, negative or zero. */ - virtual void Record(T value) noexcept = 0; + virtual void Record(T value, const opentelemetry::context::Context &context) noexcept = 0; /** * Records a value with a set of attributes. @@ -75,21 +103,26 @@ class Histogram : public SynchronousInstrument * @param value The increment amount. May be positive, negative or zero. * @param attributes A set of attributes to associate with the count. */ - virtual void Record(T value, const common::KeyValueIterable &attributes) noexcept = 0; + virtual void Record(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; template ::value> * = nullptr> - void Record(T value, const U &attributes) noexcept + void Record(T value, const U &attributes, const opentelemetry::context::Context &context) noexcept { - this->Record(value, common::KeyValueIterableView{attributes}); + this->Record(value, common::KeyValueIterableView{attributes}, context); } - void Record(T value, - std::initializer_list> - attributes) noexcept + void Record( + T value, + std::initializer_list> attributes, + const opentelemetry::context::Context &context) noexcept { - this->Record(value, nostd::span>{ - attributes.begin(), attributes.end()}); + this->Record(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); } }; @@ -106,6 +139,8 @@ class UpDownCounter : public SynchronousInstrument */ virtual void Add(T value) noexcept = 0; + virtual void Add(T value, const opentelemetry::context::Context &context) noexcept = 0; + /** * Add a value with a set of attributes. * @@ -114,19 +149,44 @@ class UpDownCounter : public SynchronousInstrument */ virtual void Add(T value, const common::KeyValueIterable &attributes) noexcept = 0; + virtual void Add(T value, + const common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + template ::value> * = nullptr> void Add(T value, const U &attributes) noexcept { - this->Add(value, common::KeyValueIterableView{attributes}); + auto context = opentelemetry::context::Context{}; + this->Add(value, common::KeyValueIterableView{attributes}, context); + } + + template ::value> * = nullptr> + void Add(T value, const U &attributes, const opentelemetry::context::Context &context) noexcept + { + this->Add(value, common::KeyValueIterableView{attributes}, context); } void Add(T value, std::initializer_list> attributes) noexcept { - this->Add(value, nostd::span>{ - attributes.begin(), attributes.end()}); + auto context = opentelemetry::context::Context{}; + this->Add(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); + } + + void Add(T value, + std::initializer_list> attributes, + const opentelemetry::context::Context &context) noexcept + { + this->Add(value, + nostd::span>{ + attributes.begin(), attributes.end()}, + context); } }; diff --git a/api/test/metrics/BUILD b/api/test/metrics/BUILD new file mode 100644 index 0000000000..d15d7511f4 --- /dev/null +++ b/api/test/metrics/BUILD @@ -0,0 +1,31 @@ +load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark") + +cc_test( + name = "noop_sync_instrument_test", + srcs = [ + "noop_sync_instrument_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "meter_provider_test", + srcs = [ + "meter_provider_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/api/test/metrics/noop_sync_instrument_test.cc b/api/test/metrics/noop_sync_instrument_test.cc index 4597e79e8b..62be4f34ab 100644 --- a/api/test/metrics/noop_sync_instrument_test.cc +++ b/api/test/metrics/noop_sync_instrument_test.cc @@ -14,8 +14,11 @@ TEST(Counter, Add) std::map labels = {{"k1", "v1"}}; EXPECT_NO_THROW(counter->Add(10l, labels)); + EXPECT_NO_THROW(counter->Add(10l, labels, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter->Add(2l)); + EXPECT_NO_THROW(counter->Add(2l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter->Add(10l, {{"k1", "1"}, {"k2", 2}})); + EXPECT_NO_THROW(counter->Add(10l, {{"k1", "1"}, {"k2", 2}}, opentelemetry::context::Context{})); } TEST(histogram, Record) @@ -24,9 +27,10 @@ TEST(histogram, Record) new opentelemetry::metrics::NoopHistogram("test", "none", "unitless")}; std::map labels = {{"k1", "v1"}}; - EXPECT_NO_THROW(counter->Record(10l, labels)); - EXPECT_NO_THROW(counter->Record(2l)); - EXPECT_NO_THROW(counter->Record(10l, {{"k1", "1"}, {"k2", 2}})); + EXPECT_NO_THROW(counter->Record(10l, labels, opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter->Record(2l, opentelemetry::context::Context{})); + EXPECT_NO_THROW( + counter->Record(10l, {{"k1", "1"}, {"k2", 2}}, opentelemetry::context::Context{})); } TEST(UpDownCountr, Record) @@ -36,8 +40,11 @@ TEST(UpDownCountr, Record) std::map labels = {{"k1", "v1"}}; EXPECT_NO_THROW(counter->Add(10l, labels)); + EXPECT_NO_THROW(counter->Add(10l, labels, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter->Add(2l)); + EXPECT_NO_THROW(counter->Add(2l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter->Add(10l, {{"k1", "1"}, {"k2", 2}})); + EXPECT_NO_THROW(counter->Add(10l, {{"k1", "1"}, {"k2", 2}}, opentelemetry::context::Context{})); } #endif \ No newline at end of file diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/always_sample_filter.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/always_sample_filter.h new file mode 100644 index 0000000000..5e7f0436eb --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/always_sample_filter.h @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/exemplar/filter.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class AlwaysSampleFilter final : public ExemplarFilter +{ +public: + static nostd::shared_ptr GetAlwaysSampleFilter() + { + static nostd::shared_ptr alwaysSampleFilter{new AlwaysSampleFilter{}}; + return alwaysSampleFilter; + } + + bool ShouldSampleMeasurement(long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept override + { + return true; + } + + bool ShouldSampleMeasurement(double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept override + { + return true; + } + +private: + explicit AlwaysSampleFilter() = default; +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/data.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/data.h new file mode 100644 index 0000000000..14eac62499 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/data.h @@ -0,0 +1,45 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/common/timestamp.h" +# include "opentelemetry/context/context.h" +# include "opentelemetry/sdk/common/attribute_utils.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +using MetricAttributes = opentelemetry::sdk::common::OrderedAttributeMap; +/** + * A sample input measurement. + * + * Exemplars also hold information about the environment when the measurement was recorded, for + * example the span and trace ID of the active span when the exemplar was recorded. + */ +class ExemplarData +{ +public: + /** + * The set of key/value pairs that were filtered out by the aggregator, but recorded alongside the + * original measurement. Only key/value pairs that were filtered out by the aggregator should be + * included + */ + MetricAttributes GetFilteredAttributes(); + + /** Returns the timestamp in nanos when measurement was collected. */ + opentelemetry::common::SystemTimestamp GetEpochNanos(); + + /** + * Returns the SpanContext associated with this exemplar. If the exemplar was not recorded + * inside a sampled trace, the Context will be invalid. + */ + opentelemetry::context::Context GetSpanContext(); +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/filter.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/filter.h new file mode 100644 index 0000000000..4b512e1317 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/filter.h @@ -0,0 +1,39 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/context/context.h" +# include "opentelemetry/sdk/common/attribute_utils.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +using MetricAttributes = opentelemetry::sdk::common::OrderedAttributeMap; + +/** + * Exemplar filters are used to pre-filter measurements before attempting to store them in a + * reservoir. + */ +class ExemplarFilter +{ +public: + // Returns whether or not a reservoir should attempt to filter a measurement. + virtual bool ShouldSampleMeasurement(long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + + // Returns whether or not a reservoir should attempt to filter a measurement. + virtual bool ShouldSampleMeasurement(double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + + virtual ~ExemplarFilter() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/never_sample_filter.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/never_sample_filter.h new file mode 100644 index 0000000000..38f51778ce --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/never_sample_filter.h @@ -0,0 +1,43 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/exemplar/filter.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +class NeverSampleFilter final : public ExemplarFilter +{ +public: + static nostd::shared_ptr GetNeverSampleFilter() + { + nostd::shared_ptr neverSampleFilter{new NeverSampleFilter{}}; + return neverSampleFilter; + } + + bool ShouldSampleMeasurement(long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept override + { + return false; + } + + bool ShouldSampleMeasurement(double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context) noexcept override + { + return false; + } + +private: + explicit NeverSampleFilter() = default; +}; +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h new file mode 100644 index 0000000000..1fe0586d28 --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h @@ -0,0 +1,55 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include +# include "opentelemetry/context/context.h" +# include "opentelemetry/nostd/shared_ptr.h" +# include "opentelemetry/sdk/common/attribute_utils.h" +# include "opentelemetry/sdk/metrics/exemplar/reservoir.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +class NoExemplarReservoir final : public ExemplarReservoir +{ + +public: + static nostd::shared_ptr GetNoExemplarReservoir() + { + return nostd::shared_ptr{new NoExemplarReservoir{}}; + } + + void OfferMeasurement(long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context, + const opentelemetry::common::SystemTimestamp ×tamp) noexcept override + { + // Stores nothing + } + + void OfferMeasurement(double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context, + const opentelemetry::common::SystemTimestamp ×tamp) noexcept override + { + // Stores nothing. + } + + std::vector CollectAndReset( + const MetricAttributes &pointAttributes) noexcept override + { + return std::vector{}; + } + +private: + explicit NoExemplarReservoir() = default; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/exemplar/reservoir.h b/sdk/include/opentelemetry/sdk/metrics/exemplar/reservoir.h new file mode 100644 index 0000000000..25e8421d6b --- /dev/null +++ b/sdk/include/opentelemetry/sdk/metrics/exemplar/reservoir.h @@ -0,0 +1,55 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW +# include +# include "opentelemetry/sdk/metrics/exemplar/data.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ +/** + * An interface for an exemplar reservoir of samples. + * + *

This represents a reservoir for a specific "point" of metric data. + */ +class ExemplarReservoir +{ +public: + virtual ~ExemplarReservoir() = default; + + /** Offers a long measurement to be sampled. */ + virtual void OfferMeasurement( + long value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context, + const opentelemetry::common::SystemTimestamp ×tamp) noexcept = 0; + + /** Offers a double measurement to be sampled. */ + virtual void OfferMeasurement( + double value, + const MetricAttributes &attributes, + const opentelemetry::context::Context &context, + const opentelemetry::common::SystemTimestamp ×tamp) noexcept = 0; + + /** + * Builds vector of Exemplars for exporting from the current reservoir. + * + *

Additionally, clears the reservoir for the next sampling period. + * + * @param pointAttributes the Attributes associated with the metric point. + * ExemplarDatas should filter these out of their final data state. + * @return A vector of sampled exemplars for this point. Implementers are expected to + * filter out pointAttributes from the original recorded attributes. + */ + virtual std::vector CollectAndReset( + const MetricAttributes &pointAttributes) noexcept = 0; +}; + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE +#endif diff --git a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h index ffd47c5c92..f3b998451b 100644 --- a/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/measurement_processor.h @@ -2,10 +2,12 @@ // SPDX-License-Identifier: Apache-2.0 #pragma once +#include #ifndef ENABLE_METRICS_PREVIEW # include # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/metric_reader.h" # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" @@ -25,15 +27,18 @@ static std::size_t MakeKey(const MetricReader &metric_reader) class MeasurementProcessor { public: - virtual void RecordLong(long value) noexcept = 0; + virtual void RecordLong(long value, const opentelemetry::context::Context &context) noexcept = 0; virtual void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; - virtual void RecordDouble(double value) noexcept = 0; + virtual void RecordDouble(double value, + const opentelemetry::context::Context &context) noexcept = 0; virtual void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; virtual bool Collect(MetricReader &reader, AggregationTemporarily aggregation_temporarily, @@ -50,43 +55,46 @@ class DefaultMeasurementProcessor : public MeasurementProcessor InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, InstrumentValueType::kLong}; metric_storages_[MakeKey(reader)] = std::unique_ptr( - new SyncMetricStorage(instr_desc, AggregationType::kSum, new DefaultAttributesProcessor())); + new SyncMetricStorage(instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), + NoExemplarReservoir::GetNoExemplarReservoir())); return true; } - virtual void RecordLong(long value) noexcept override + virtual void RecordLong(long value, + const opentelemetry::context::Context &context) noexcept override { for (const auto &kv : metric_storages_) { - kv.second->RecordLong(value); + kv.second->RecordLong(value, context); } } - virtual void RecordLong( - long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + virtual void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { for (const auto &kv : metric_storages_) { - kv.second->RecordLong(value, attributes); + kv.second->RecordLong(value, attributes, context); } } - virtual void RecordDouble(double value) noexcept override + virtual void RecordDouble(double value, + const opentelemetry::context::Context &context) noexcept override { for (const auto &kv : metric_storages_) { - kv.second->RecordDouble(value); + kv.second->RecordDouble(value, context); } } - virtual void RecordDouble( - double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + virtual void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { for (const auto &kv : metric_storages_) { - kv.second->RecordDouble(value, attributes); + kv.second->RecordDouble(value, attributes, context); } } diff --git a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h index 8ae50554f1..20f7d56140 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/metric_storage.h @@ -5,6 +5,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/common/key_value_iterable_view.h" # include "opentelemetry/common/timestamp.h" +# include "opentelemetry/context/context.h" # include "opentelemetry/sdk/metrics/data/metric_data.h" # include "opentelemetry/sdk/metrics/instruments.h" OPENTELEMETRY_BEGIN_NAMESPACE @@ -30,15 +31,19 @@ class MetricStorage class WritableMetricStorage { public: - virtual void RecordLong(long value) noexcept = 0; + virtual void RecordLong(long value, const opentelemetry::context::Context &context) noexcept = 0; virtual void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; - virtual void RecordDouble(double value) noexcept = 0; + virtual void RecordDouble(double value, + const opentelemetry::context::Context &context) noexcept = 0; virtual void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept = 0; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept = 0; + virtual ~WritableMetricStorage() = default; }; @@ -63,16 +68,19 @@ class NoopMetricStorage : public MetricStorage class NoopWritableMetricStorage : public WritableMetricStorage { public: - void RecordLong(long value) noexcept = 0; + void RecordLong(long value, const opentelemetry::context::Context &context) noexcept = 0; void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override {} - void RecordDouble(double value) noexcept override {} + void RecordDouble(double value, const opentelemetry::context::Context &context) noexcept override + {} void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override {} }; diff --git a/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h index d9a66f732a..ceeafa0406 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/multi_metric_storage.h @@ -20,39 +20,41 @@ class MultiMetricStorage : public WritableMetricStorage public: void AddStorage(std::shared_ptr storage) { storages_.push_back(storage); } - virtual void RecordLong(long value) noexcept override + virtual void RecordLong(long value, + const opentelemetry::context::Context &context) noexcept override { for (auto &s : storages_) { - s->RecordLong(value); + s->RecordLong(value, context); } } - virtual void RecordLong( - long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + virtual void RecordLong(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { for (auto &s : storages_) { - s->RecordLong(value, attributes); + s->RecordLong(value, attributes, context); } } - virtual void RecordDouble(double value) noexcept override + virtual void RecordDouble(double value, + const opentelemetry::context::Context &context) noexcept override { for (auto &s : storages_) { - s->RecordDouble(value); + s->RecordDouble(value, context); } } - virtual void RecordDouble( - double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + virtual void RecordDouble(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { for (auto &s : storages_) { - s->RecordDouble(value, attributes); + s->RecordDouble(value, attributes, context); } } diff --git a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h index f8c2a86aa4..bfe50b152d 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -7,6 +7,7 @@ # include "opentelemetry/sdk/common/attributemap_hash.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" # include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h" +# include "opentelemetry/sdk/metrics/exemplar/reservoir.h" # include "opentelemetry/sdk/metrics/state/attributes_hashmap.h" # include "opentelemetry/sdk/metrics/state/metric_storage.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" @@ -27,11 +28,13 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage public: SyncMetricStorage(InstrumentDescriptor instrument_descriptor, const AggregationType aggregation_type, - const AttributesProcessor *attributes_processor) + const AttributesProcessor *attributes_processor, + nostd::shared_ptr &&exemplar_reservoir) : instrument_descriptor_(instrument_descriptor), aggregation_type_{aggregation_type}, attributes_hashmap_(new AttributesHashMap()), - attributes_processor_{attributes_processor} + attributes_processor_{attributes_processor}, + exemplar_reservoir_(exemplar_reservoir) { create_default_aggregation_ = [&]() -> std::unique_ptr { return std::move( @@ -39,45 +42,55 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage }; } - void RecordLong(long value) noexcept override + void RecordLong(long value, const opentelemetry::context::Context &context) noexcept override { if (instrument_descriptor_.value_type_ != InstrumentValueType::kLong) { return; } + exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now()); attributes_hashmap_->GetOrSetDefault({}, create_default_aggregation_)->Aggregate(value); } void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { if (instrument_descriptor_.value_type_ != InstrumentValueType::kLong) { return; } + exemplar_reservoir_->OfferMeasurement(value, attributes, context, + std::chrono::system_clock::now()); auto attr = attributes_processor_->process(attributes); attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value); } - void RecordDouble(double value) noexcept override + void RecordDouble(double value, const opentelemetry::context::Context &context) noexcept override { if (instrument_descriptor_.value_type_ != InstrumentValueType::kDouble) { return; } + exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now()); attributes_hashmap_->GetOrSetDefault({}, create_default_aggregation_)->Aggregate(value); } void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { + exemplar_reservoir_->OfferMeasurement(value, attributes, context, + std::chrono::system_clock::now()); if (instrument_descriptor_.value_type_ != InstrumentValueType::kDouble) { return; } + exemplar_reservoir_->OfferMeasurement(value, attributes, context, + std::chrono::system_clock::now()); auto attr = attributes_processor_->process(attributes); attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value); } @@ -102,6 +115,7 @@ class SyncMetricStorage : public MetricStorage, public WritableMetricStorage std::unique_ptr attributes_hashmap_; const AttributesProcessor *attributes_processor_; std::function()> create_default_aggregation_; + nostd::shared_ptr exemplar_reservoir_; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h index 3e8c5c8623..f4c9bae323 100644 --- a/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h +++ b/sdk/include/opentelemetry/sdk/metrics/sync_instruments.h @@ -39,8 +39,12 @@ class LongCounter : public Synchronous, public opentelemetry::metrics::Counter storage); void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Add(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Add(long value) noexcept override; + void Add(long value, const opentelemetry::context::Context &context) noexcept override; }; class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter @@ -52,8 +56,12 @@ class DoubleCounter : public Synchronous, public opentelemetry::metrics::Counter void Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Add(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Add(double value) noexcept override; + void Add(double value, const opentelemetry::context::Context &context) noexcept override; }; class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter @@ -63,8 +71,12 @@ class LongUpDownCounter : public Synchronous, public opentelemetry::metrics::UpD std::unique_ptr storage); void Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Add(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Add(long value) noexcept override; + void Add(long value, const opentelemetry::context::Context &context) noexcept override; }; class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::UpDownCounter @@ -75,8 +87,12 @@ class DoubleUpDownCounter : public Synchronous, public opentelemetry::metrics::U void Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + void Add(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; void Add(double value) noexcept override; + void Add(double value, const opentelemetry::context::Context &context) noexcept override; }; class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogram @@ -86,9 +102,10 @@ class LongHistogram : public Synchronous, public opentelemetry::metrics::Histogr std::unique_ptr storage); void Record(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override; + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override; - void Record(long value) noexcept override; + void Record(long value, const opentelemetry::context::Context &context) noexcept override; }; class DoubleHistogram : public Synchronous, public opentelemetry::metrics::Histogram @@ -98,9 +115,10 @@ class DoubleHistogram : public Synchronous, public opentelemetry::metrics::Histo std::unique_ptr storage); void Record(double value, - const opentelemetry::common::KeyValueIterable &attributes) 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; }; } // namespace metrics diff --git a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h index d82607357f..fdc4e35c53 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h @@ -23,8 +23,6 @@ class AttributesProcessor // @returns The processed attributes virtual MetricAttributes process( const opentelemetry::common::KeyValueIterable &attributes) const noexcept = 0; - - virtual ~AttributesProcessor() = default; }; /** diff --git a/sdk/src/metrics/meter.cc b/sdk/src/metrics/meter.cc index 360630e786..20e3b02768 100644 --- a/sdk/src/metrics/meter.cc +++ b/sdk/src/metrics/meter.cc @@ -6,6 +6,7 @@ # include "opentelemetry/metrics/noop.h" # include "opentelemetry/nostd/shared_ptr.h" # include "opentelemetry/sdk/metrics/async_instruments.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/sdk/metrics/sync_instruments.h" @@ -192,7 +193,8 @@ std::unique_ptr Meter::RegisterMetricStorage( view_instr_desc.name_ = view.GetName(); view_instr_desc.description_ = view.GetDescription(); auto storage = std::shared_ptr(new SyncMetricStorage( - view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor())); + view_instr_desc, view.GetAggregationType(), &view.GetAttributesProcessor(), + NoExemplarReservoir::GetNoExemplarReservoir())); storage_registry_[instrument_descriptor.name_] = storage; auto multi_storage = static_cast(storages.get()); multi_storage->AddStorage(storage); diff --git a/sdk/src/metrics/sync_instruments.cc b/sdk/src/metrics/sync_instruments.cc index 863e5b6323..5c94ae6f24 100644 --- a/sdk/src/metrics/sync_instruments.cc +++ b/sdk/src/metrics/sync_instruments.cc @@ -18,12 +18,26 @@ LongCounter::LongCounter(InstrumentDescriptor instrument_descriptor, void LongCounter::Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return storage_->RecordLong(value, attributes); + auto context = opentelemetry::context::Context{}; + return storage_->RecordLong(value, attributes, context); +} + +void LongCounter::Add(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordLong(value, attributes, context); } void LongCounter::Add(long value) noexcept { - return storage_->RecordLong(value); + auto context = opentelemetry::context::Context{}; + return storage_->RecordLong(value, context); +} + +void LongCounter::Add(long value, const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordLong(value, context); } DoubleCounter::DoubleCounter(InstrumentDescriptor instrument_descriptor, @@ -34,12 +48,26 @@ DoubleCounter::DoubleCounter(InstrumentDescriptor instrument_descriptor, void DoubleCounter::Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return storage_->RecordDouble(value, attributes); + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, attributes, context); +} + +void DoubleCounter::Add(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordDouble(value, attributes, context); } void DoubleCounter::Add(double value) noexcept { - return storage_->RecordDouble(value); + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, context); +} + +void DoubleCounter::Add(double value, const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordDouble(value, context); } LongUpDownCounter::LongUpDownCounter(InstrumentDescriptor instrument_descriptor, @@ -50,12 +78,26 @@ LongUpDownCounter::LongUpDownCounter(InstrumentDescriptor instrument_descriptor, void LongUpDownCounter::Add(long value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return storage_->RecordLong(value, attributes); + auto context = opentelemetry::context::Context{}; + return storage_->RecordLong(value, attributes, context); +} + +void LongUpDownCounter::Add(long value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordLong(value, attributes, context); } void LongUpDownCounter::Add(long value) noexcept { - return storage_->RecordLong(value); + auto context = opentelemetry::context::Context{}; + return storage_->RecordLong(value, context); +} + +void LongUpDownCounter::Add(long value, const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordLong(value, context); } DoubleUpDownCounter::DoubleUpDownCounter(InstrumentDescriptor instrument_descriptor, @@ -66,12 +108,26 @@ DoubleUpDownCounter::DoubleUpDownCounter(InstrumentDescriptor instrument_descrip void DoubleUpDownCounter::Add(double value, const opentelemetry::common::KeyValueIterable &attributes) noexcept { - return storage_->RecordDouble(value, attributes); + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, attributes, context); +} + +void DoubleUpDownCounter::Add(double value, + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordDouble(value, attributes, context); } void DoubleUpDownCounter::Add(double value) noexcept { - return storage_->RecordDouble(value); + auto context = opentelemetry::context::Context{}; + return storage_->RecordDouble(value, context); +} + +void DoubleUpDownCounter::Add(double value, const opentelemetry::context::Context &context) noexcept +{ + return storage_->RecordDouble(value, context); } LongHistogram::LongHistogram(InstrumentDescriptor instrument_descriptor, @@ -80,14 +136,15 @@ LongHistogram::LongHistogram(InstrumentDescriptor instrument_descriptor, {} void LongHistogram::Record(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept { - return storage_->RecordLong(value, attributes); + return storage_->RecordLong(value, attributes, context); } -void LongHistogram::Record(long value) noexcept +void LongHistogram::Record(long value, const opentelemetry::context::Context &context) noexcept { - return storage_->RecordLong(value); + return storage_->RecordLong(value, context); } DoubleHistogram::DoubleHistogram(InstrumentDescriptor instrument_descriptor, @@ -96,14 +153,15 @@ DoubleHistogram::DoubleHistogram(InstrumentDescriptor instrument_descriptor, {} void DoubleHistogram::Record(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept { - return storage_->RecordDouble(value, attributes); + return storage_->RecordDouble(value, attributes, context); } -void DoubleHistogram::Record(double value) noexcept +void DoubleHistogram::Record(double value, const opentelemetry::context::Context &context) noexcept { - return storage_->RecordDouble(value); + return storage_->RecordDouble(value, context); } } // namespace metrics diff --git a/sdk/test/metrics/BUILD b/sdk/test/metrics/BUILD index 9ca191574f..819a8d225f 100644 --- a/sdk/test/metrics/BUILD +++ b/sdk/test/metrics/BUILD @@ -80,6 +80,22 @@ cc_test( ], ) +cc_test( + name = "sync_instruments_test", + srcs = [ + "sync_instruments_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//sdk/src/metrics", + "//sdk/src/resource", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "async_metric_storage_test", srcs = [ diff --git a/sdk/test/metrics/CMakeLists.txt b/sdk/test/metrics/CMakeLists.txt index 3ee2be6552..fa1f22c73a 100644 --- a/sdk/test/metrics/CMakeLists.txt +++ b/sdk/test/metrics/CMakeLists.txt @@ -29,3 +29,5 @@ target_link_libraries(attributes_processor_benchmark benchmark::benchmark add_executable(attributes_hashmap_benchmark attributes_hashmap_benchmark.cc) target_link_libraries(attributes_hashmap_benchmark benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT} opentelemetry_common) + +add_subdirectory(exemplar) diff --git a/sdk/test/metrics/exemplar/BUILD b/sdk/test/metrics/exemplar/BUILD new file mode 100644 index 0000000000..6481f679d2 --- /dev/null +++ b/sdk/test/metrics/exemplar/BUILD @@ -0,0 +1,47 @@ +cc_test( + name = "no_exemplar_reservoir_test", + srcs = [ + "no_exemplar_reservoir_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "//sdk:headers", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "never_sample_filter_test", + srcs = [ + "never_sample_filter_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "//sdk:headers", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "always_sample_filter_test", + srcs = [ + "always_sample_filter_test.cc", + ], + tags = [ + "metrics", + "test", + ], + deps = [ + "//api", + "//sdk:headers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/sdk/test/metrics/exemplar/CMakeLists.txt b/sdk/test/metrics/exemplar/CMakeLists.txt new file mode 100644 index 0000000000..303294761a --- /dev/null +++ b/sdk/test/metrics/exemplar/CMakeLists.txt @@ -0,0 +1,10 @@ +foreach(testname no_exemplar_reservoir_test never_sample_filter_test + always_sample_filter_test) + add_executable(${testname} "${testname}.cc") + target_link_libraries(${testname} ${GTEST_BOTH_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} opentelemetry_metrics) + gtest_add_tests( + TARGET ${testname} + TEST_PREFIX metrics. + TEST_LIST ${testname}) +endforeach() diff --git a/sdk/test/metrics/exemplar/always_sample_filter_test.cc b/sdk/test/metrics/exemplar/always_sample_filter_test.cc new file mode 100644 index 0000000000..cf4e449957 --- /dev/null +++ b/sdk/test/metrics/exemplar/always_sample_filter_test.cc @@ -0,0 +1,19 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/exemplar/always_sample_filter.h" +# include + +using namespace opentelemetry::sdk::metrics; + +TEST(AlwaysSampleFilter, SampleMeasurement) +{ + auto filter = opentelemetry::sdk::metrics::AlwaysSampleFilter::GetAlwaysSampleFilter(); + ASSERT_TRUE( + filter->ShouldSampleMeasurement(1.0, MetricAttributes{}, opentelemetry::context::Context{})); + ASSERT_TRUE( + filter->ShouldSampleMeasurement(1l, MetricAttributes{}, opentelemetry::context::Context{})); +} + +#endif diff --git a/sdk/test/metrics/exemplar/never_sample_filter_test.cc b/sdk/test/metrics/exemplar/never_sample_filter_test.cc new file mode 100644 index 0000000000..930c572205 --- /dev/null +++ b/sdk/test/metrics/exemplar/never_sample_filter_test.cc @@ -0,0 +1,20 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/context/context.h" +#ifndef ENABLE_METRICS_PREVIEW +# include +# include "opentelemetry/sdk/metrics/exemplar/never_sample_filter.h" + +using namespace opentelemetry::sdk::metrics; + +TEST(NeverSampleFilter, SampleMeasurement) +{ + auto filter = opentelemetry::sdk::metrics::NeverSampleFilter::GetNeverSampleFilter(); + ASSERT_FALSE( + filter->ShouldSampleMeasurement(1.0, MetricAttributes{}, opentelemetry::context::Context{})); + ASSERT_FALSE( + filter->ShouldSampleMeasurement(1l, MetricAttributes{}, opentelemetry::context::Context{})); +} + +#endif diff --git a/sdk/test/metrics/exemplar/no_exemplar_reservoir_test.cc b/sdk/test/metrics/exemplar/no_exemplar_reservoir_test.cc new file mode 100644 index 0000000000..3e16940ff4 --- /dev/null +++ b/sdk/test/metrics/exemplar/no_exemplar_reservoir_test.cc @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#ifndef ENABLE_METRICS_PREVIEW +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" +# include + +using namespace opentelemetry::sdk::metrics; + +TEST(NoExemplarReservoir, OfferMeasurement) +{ + auto reservoir = opentelemetry::sdk::metrics::NoExemplarReservoir::GetNoExemplarReservoir(); + EXPECT_NO_THROW(reservoir->OfferMeasurement(1.0, MetricAttributes{}, + opentelemetry::context::Context{}, + std::chrono::system_clock::now())); + EXPECT_NO_THROW(reservoir->OfferMeasurement( + 1l, MetricAttributes{}, opentelemetry::context::Context{}, std::chrono::system_clock::now())); + auto exemplar_data = reservoir->CollectAndReset(MetricAttributes{}); + ASSERT_TRUE(exemplar_data.empty()); +} + +#endif diff --git a/sdk/test/metrics/multi_metric_storage_test.cc b/sdk/test/metrics/multi_metric_storage_test.cc index 6696c7bb0a..d88946485c 100644 --- a/sdk/test/metrics/multi_metric_storage_test.cc +++ b/sdk/test/metrics/multi_metric_storage_test.cc @@ -4,6 +4,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/instruments.h" # include @@ -15,18 +16,26 @@ using namespace opentelemetry::sdk::metrics; class TestMetricStorage : public WritableMetricStorage { public: - void RecordLong(long value) noexcept override { num_calls_long++; } + void RecordLong(long value, const opentelemetry::context::Context &context) noexcept override + { + num_calls_long++; + } void RecordLong(long value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { num_calls_long++; } - void RecordDouble(double value) noexcept override { num_calls_double++; } + void RecordDouble(double value, const opentelemetry::context::Context &context) noexcept override + { + num_calls_double++; + } void RecordDouble(double value, - const opentelemetry::common::KeyValueIterable &attributes) noexcept override + const opentelemetry::common::KeyValueIterable &attributes, + const opentelemetry::context::Context &context) noexcept override { num_calls_double++; } @@ -39,13 +48,13 @@ TEST(MultiMetricStorageTest, BasicTests) { std::shared_ptr storage( new TestMetricStorage()); - MultiMetricStorage storages; + MultiMetricStorage storages{}; storages.AddStorage(storage); - EXPECT_NO_THROW(storages.RecordLong(10l)); - EXPECT_NO_THROW(storages.RecordLong(20l)); + EXPECT_NO_THROW(storages.RecordLong(10l, opentelemetry::context::Context{})); + EXPECT_NO_THROW(storages.RecordLong(20l, opentelemetry::context::Context{})); - EXPECT_NO_THROW(storages.RecordDouble(10.0)); - EXPECT_NO_THROW(storages.RecordLong(30l)); + EXPECT_NO_THROW(storages.RecordDouble(10.0, opentelemetry::context::Context{})); + EXPECT_NO_THROW(storages.RecordLong(30l, opentelemetry::context::Context{})); EXPECT_EQ(static_cast(storage.get())->num_calls_long, 3); EXPECT_EQ(static_cast(storage.get())->num_calls_double, 1); diff --git a/sdk/test/metrics/sync_instruments_test.cc b/sdk/test/metrics/sync_instruments_test.cc index 27af8711d1..e029821d41 100644 --- a/sdk/test/metrics/sync_instruments_test.cc +++ b/sdk/test/metrics/sync_instruments_test.cc @@ -3,7 +3,9 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/sync_instruments.h" +# include "opentelemetry/context/context.h" # include "opentelemetry/sdk/instrumentationlibrary/instrumentation_library.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/state/multi_metric_storage.h" # include @@ -23,11 +25,16 @@ TEST(SyncInstruments, LongCounter) std::unique_ptr metric_storage(new MultiMetricStorage()); LongCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10l)); - EXPECT_NO_THROW(counter.Add(10l)); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add( 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add( + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}))); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } TEST(SyncInstruments, DoubleCounter) @@ -37,11 +44,16 @@ TEST(SyncInstruments, DoubleCounter) std::unique_ptr metric_storage(new MultiMetricStorage()); DoubleCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10.10)); - EXPECT_NO_THROW(counter.Add(10.10)); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add( 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add( + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}))); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } TEST(SyncInstruments, LongUpDownCounter) @@ -52,11 +64,16 @@ TEST(SyncInstruments, LongUpDownCounter) std::unique_ptr metric_storage(new MultiMetricStorage()); LongUpDownCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10l)); - EXPECT_NO_THROW(counter.Add(10l)); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add( 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add( + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}))); + EXPECT_NO_THROW(counter.Add(10l, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } TEST(SyncInstruments, DoubleUpDownCounter) @@ -67,10 +84,15 @@ TEST(SyncInstruments, DoubleUpDownCounter) std::unique_ptr metric_storage(new MultiMetricStorage()); DoubleUpDownCounter counter(instrument_descriptor, std::move(metric_storage)); EXPECT_NO_THROW(counter.Add(10.10)); - EXPECT_NO_THROW(counter.Add(10.10)); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Add( + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add( 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Add(10.10, opentelemetry::common::KeyValueIterableView({}))); } @@ -80,12 +102,14 @@ TEST(SyncInstruments, LongHistogram) "long_histogram", "description", "1", InstrumentType::kHistogram, InstrumentValueType::kLong}; std::unique_ptr metric_storage(new MultiMetricStorage()); LongHistogram counter(instrument_descriptor, std::move(metric_storage)); - EXPECT_NO_THROW(counter.Record(10l)); - EXPECT_NO_THROW(counter.Record(10l)); + EXPECT_NO_THROW(counter.Record(10l, opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Record(10l, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Record( - 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); - EXPECT_NO_THROW(counter.Record(10l, opentelemetry::common::KeyValueIterableView({}))); + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Record(10l, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } TEST(SyncInstruments, DoubleHistogram) @@ -95,12 +119,14 @@ TEST(SyncInstruments, DoubleHistogram) InstrumentValueType::kDouble}; std::unique_ptr metric_storage(new MultiMetricStorage()); DoubleHistogram counter(instrument_descriptor, std::move(metric_storage)); - EXPECT_NO_THROW(counter.Record(10.10)); - EXPECT_NO_THROW(counter.Record(10.10)); + EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::context::Context{})); EXPECT_NO_THROW(counter.Record( - 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); - EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::common::KeyValueIterableView({}))); + 10.10, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); + EXPECT_NO_THROW(counter.Record(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } #endif \ No newline at end of file diff --git a/sdk/test/metrics/sync_metric_storage_test.cc b/sdk/test/metrics/sync_metric_storage_test.cc index 23ef20b11d..c4bd154460 100644 --- a/sdk/test/metrics/sync_metric_storage_test.cc +++ b/sdk/test/metrics/sync_metric_storage_test.cc @@ -4,6 +4,7 @@ #ifndef ENABLE_METRICS_PREVIEW # include "opentelemetry/sdk/metrics/state/sync_metric_storage.h" # include "opentelemetry/common/key_value_iterable_view.h" +# include "opentelemetry/sdk/metrics/exemplar/no_exemplar_reservoir.h" # include "opentelemetry/sdk/metrics/instruments.h" # include "opentelemetry/sdk/metrics/view/attributes_processor.h" @@ -18,13 +19,16 @@ TEST(WritableMetricStorageTest, BasicTests) InstrumentDescriptor instr_desc = {"name", "desc", "1unit", InstrumentType::kCounter, InstrumentValueType::kLong}; - opentelemetry::sdk::metrics::SyncMetricStorage storage(instr_desc, AggregationType::kSum, - new DefaultAttributesProcessor()); - EXPECT_NO_THROW(storage.RecordLong(10l)); - EXPECT_NO_THROW(storage.RecordDouble(10.10)); + opentelemetry::sdk::metrics::SyncMetricStorage storage( + instr_desc, AggregationType::kSum, new DefaultAttributesProcessor(), + NoExemplarReservoir::GetNoExemplarReservoir()); + EXPECT_NO_THROW(storage.RecordLong(10l, opentelemetry::context::Context{})); + EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::context::Context{})); EXPECT_NO_THROW(storage.RecordLong( - 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}))); + 10l, opentelemetry::common::KeyValueIterableView({{"abc", "123"}, {"xyz", "456"}}), + opentelemetry::context::Context{})); - EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::common::KeyValueIterableView({}))); + EXPECT_NO_THROW(storage.RecordDouble(10.10, opentelemetry::common::KeyValueIterableView({}), + opentelemetry::context::Context{})); } #endif