diff --git a/CMakeLists.txt b/CMakeLists.txt index 14db63d130..e7597fc87a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,6 +192,9 @@ option(WITH_EXAMPLES "Whether to build examples" ON) option(WITH_METRICS_PREVIEW "Whether to build metrics preview" OFF) option(WITH_LOGS_PREVIEW "Whether to build logs preview" OFF) option(WITH_ASYNC_EXPORT_PREVIEW "Whether enable async export" OFF) +# Exemplar specs status is experimental, so behind feature flag by default +option(WITH_METRICS_EXEMPLAR_PREVIEW + "Whethere to enable exemplar within metrics" OFF) find_package(Threads) diff --git a/api/CMakeLists.txt b/api/CMakeLists.txt index a87bf54cb3..b7ed2d7f69 100644 --- a/api/CMakeLists.txt +++ b/api/CMakeLists.txt @@ -96,3 +96,8 @@ endif() if(WITH_ASYNC_EXPORT_PREVIEW) target_compile_definitions(opentelemetry_api INTERFACE ENABLE_ASYNC_EXPORT) endif() + +if(WITH_METRICS_EXEMPLAR_PREVIEW) + target_compile_definitions(opentelemetry_api + INTERFACE ENABLE_METRICS_EXEMPLAR_PREVIEW) +endif() diff --git a/ci/do_ci.sh b/ci/do_ci.sh index 791cc3b48a..be583d75e1 100755 --- a/ci/do_ci.sh +++ b/ci/do_ci.sh @@ -59,7 +59,7 @@ mkdir -p "${BUILD_DIR}" [ -z "${PLUGIN_DIR}" ] && export PLUGIN_DIR=$HOME/plugin mkdir -p "${PLUGIN_DIR}" -BAZEL_OPTIONS="--copt=-DENABLE_LOGS_PREVIEW --copt=-DENABLE_TEST" +BAZEL_OPTIONS="--copt=-DENABLE_LOGS_PREVIEW --copt=-DENABLE_TEST --copt=-DENABLE_METRICS_EXEMPLAR_PREVIEW" BAZEL_TEST_OPTIONS="$BAZEL_OPTIONS --test_output=errors" @@ -82,7 +82,8 @@ if [[ "$1" == "cmake.test" ]]; then -DWITH_ZIPKIN=ON \ -DWITH_JAEGER=ON \ -DWITH_ELASTICSEARCH=ON \ - -DWITH_METRICS_PREVIEW=ON \ + -DWITH_METRICS_PREVIEW=OFF \ + -DWITH_METRICS_EXEMPLAR_PREVIEW=ON \ -DWITH_LOGS_PREVIEW=ON \ -DCMAKE_CXX_FLAGS="-Werror" \ "${SRC_DIR}" @@ -99,6 +100,7 @@ elif [[ "$1" == "cmake.maintainer.test" ]]; then -DWITH_ELASTICSEARCH=ON \ -DWITH_LOGS_PREVIEW=ON \ -DWITH_METRICS_PREVIEW=OFF \ + -DWITH_METRICS_EXEMPLAR_PREVIEW=ON \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ -DOTELCPP_MAINTAINER_MODE=ON \ "${SRC_DIR}" @@ -114,6 +116,7 @@ elif [[ "$1" == "cmake.with_async_export.test" ]]; then -DWITH_JAEGER=ON \ -DWITH_ELASTICSEARCH=ON \ -DWITH_METRICS_PREVIEW=OFF \ + -DWITH_METRICS_EXEMPLAR_PREVIEW=ON \ -DWITH_LOGS_PREVIEW=ON \ -DCMAKE_CXX_FLAGS="-Werror" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ @@ -142,6 +145,7 @@ elif [[ "$1" == "cmake.abseil.test" ]]; then rm -rf * cmake -DCMAKE_BUILD_TYPE=Debug \ -DWITH_METRICS_PREVIEW=OFF \ + -DWITH_METRICS_EXEMPLAR_PREVIEW=ON \ -DWITH_LOGS_PREVIEW=ON \ -DCMAKE_CXX_FLAGS="-Werror" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ @@ -166,6 +170,7 @@ elif [[ "$1" == "cmake.c++20.stl.test" ]]; then rm -rf * cmake -DCMAKE_BUILD_TYPE=Debug \ -DWITH_METRICS_PREVIEW=OFF \ + -DWITH_METRICS_EXEMPLAR_PREVIEW=ON \ -DWITH_LOGS_PREVIEW=ON \ -DCMAKE_CXX_FLAGS="-Werror" \ -DWITH_ASYNC_EXPORT_PREVIEW=ON \ 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 dfbc4be2ef..f6af1f036f 100644 --- a/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h +++ b/sdk/include/opentelemetry/sdk/metrics/state/sync_metric_storage.h @@ -36,7 +36,9 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage aggregation_type_{aggregation_type}, attributes_hashmap_(new AttributesHashMap()), attributes_processor_{attributes_processor}, +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW exemplar_reservoir_(exemplar_reservoir), +# endif temporal_metric_storage_(instrument_descriptor, aggregation_config) { @@ -52,7 +54,9 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage { return; } +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now()); +# endif std::lock_guard guard(attribute_hashmap_lock_); attributes_hashmap_->GetOrSetDefault({}, create_default_aggregation_)->Aggregate(value); } @@ -65,9 +69,10 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage { return; } - +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW exemplar_reservoir_->OfferMeasurement(value, attributes, context, std::chrono::system_clock::now()); +# endif auto attr = attributes_processor_->process(attributes); std::lock_guard guard(attribute_hashmap_lock_); attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value); @@ -79,7 +84,9 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage { return; } +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW exemplar_reservoir_->OfferMeasurement(value, {}, context, std::chrono::system_clock::now()); +# endif std::lock_guard guard(attribute_hashmap_lock_); attributes_hashmap_->GetOrSetDefault({}, create_default_aggregation_)->Aggregate(value); } @@ -88,14 +95,18 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage const opentelemetry::common::KeyValueIterable &attributes, const opentelemetry::context::Context &context) noexcept override { +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW exemplar_reservoir_->OfferMeasurement(value, attributes, context, std::chrono::system_clock::now()); +# endif if (instrument_descriptor_.value_type_ != InstrumentValueType::kDouble) { return; } +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW exemplar_reservoir_->OfferMeasurement(value, attributes, context, std::chrono::system_clock::now()); +# endif auto attr = attributes_processor_->process(attributes); std::lock_guard guard(attribute_hashmap_lock_); attributes_hashmap_->GetOrSetDefault(attr, create_default_aggregation_)->Aggregate(value); @@ -120,7 +131,9 @@ class SyncMetricStorage : public MetricStorage, public SyncWritableMetricStorage std::unordered_map last_reported_metrics_; const AttributesProcessor *attributes_processor_; std::function()> create_default_aggregation_; +# ifdef ENABLE_METRICS_EXEMPLAR_PREVIEW nostd::shared_ptr exemplar_reservoir_; +# endif TemporalMetricStorage temporal_metric_storage_; opentelemetry::common::SpinLockMutex attribute_hashmap_lock_; };