diff --git a/CHANGELOG.md b/CHANGELOG.md index 865a2f680b..96b77f7481 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ Increment the: ## [Unreleased] +* [CONFIGURATION] Apply metric view `attribute_keys` include and exclude patterns + when building the SDK, with exclude patterns taking precedence. + [#3546](https://github.com/open-telemetry/opentelemetry-cpp/issues/3546) + * [CONFIGURATION] Add support for the composite sampler configuration (programmatic and from yaml) ([#4366](https://github.com/open-telemetry/opentelemetry-cpp/pull/4366)) diff --git a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h index b4d5e61801..26294a99f2 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h @@ -3,13 +3,14 @@ #pragma once -#include #include #include #include +#include #include "opentelemetry/common/attribute_value.h" #include "opentelemetry/common/key_value_iterable.h" +#include "opentelemetry/nostd/function_ref.h" #include "opentelemetry/nostd/string_view.h" #include "opentelemetry/sdk/common/custom_hash_equality.h" #include "opentelemetry/sdk/metrics/state/filtered_ordered_attribute_map.h" @@ -162,6 +163,30 @@ class FilteringExcludeAttributesProcessor : public AttributesProcessor FilterAttributeMap exclude_list_; }; +/** + * IncludeExcludeAttributesProcessor filters attributes using wildcard include and exclude + * patterns. Exclude patterns take precedence over include patterns. + */ +class OPENTELEMETRY_EXPORT IncludeExcludeAttributesProcessor final : public AttributesProcessor +{ +public: + IncludeExcludeAttributesProcessor(bool include_all, + std::vector included_patterns, + std::vector excluded_patterns); + + MetricAttributes process( + const opentelemetry::common::KeyValueIterable &attributes) const noexcept override; + + bool isPresent(nostd::string_view key) const noexcept override; + +private: + static bool MatchesAny(const std::vector &patterns, nostd::string_view key) noexcept; + + bool include_all_{true}; + std::vector included_patterns_; + std::vector excluded_patterns_; +}; + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/configuration/sdk_builder.cc b/sdk/src/configuration/sdk_builder.cc index 2667b31b74..62ec3b052b 100644 --- a/sdk/src/configuration/sdk_builder.cc +++ b/sdk/src/configuration/sdk_builder.cc @@ -140,6 +140,7 @@ #include "opentelemetry/sdk/configuration/span_processor_configuration.h" #include "opentelemetry/sdk/configuration/span_processor_configuration_visitor.h" #include "opentelemetry/sdk/configuration/string_array_attribute_value_configuration.h" +#include "opentelemetry/sdk/configuration/string_array_configuration.h" #include "opentelemetry/sdk/configuration/string_attribute_value_configuration.h" #include "opentelemetry/sdk/configuration/text_map_propagator_builder.h" #include "opentelemetry/sdk/configuration/trace_id_ratio_based_sampler_builder.h" @@ -1799,16 +1800,31 @@ std::unique_ptr SdkBuilder::Crea std::unique_ptr SdkBuilder::CreateAttributesProcessor( - const std::unique_ptr - & /* model */) const + const std::unique_ptr &model) + const { - std::unique_ptr sdk; + using opentelemetry::sdk::metrics::DefaultAttributesProcessor; + using opentelemetry::sdk::metrics::IncludeExcludeAttributesProcessor; - // FIXME-SDK: https://github.com/open-telemetry/opentelemetry-cpp/issues/3546 - // FIXME-SDK: Need a subclass of AttributesProcessor for IncludeExclude - OTEL_INTERNAL_LOG_WARN("IncludeExclude attribute processor not supported, ignoring"); + if (model->included == nullptr && model->excluded == nullptr) + { + return std::make_unique(); + } - return sdk; + std::vector included_patterns; + if (model->included != nullptr) + { + included_patterns = model->included->string_array; + } + + std::vector excluded_patterns; + if (model->excluded != nullptr) + { + excluded_patterns = model->excluded->string_array; + } + + return std::make_unique( + included_patterns.empty(), std::move(included_patterns), std::move(excluded_patterns)); } void SdkBuilder::AddView( diff --git a/sdk/src/metrics/BUILD b/sdk/src/metrics/BUILD index be1bdc73de..c6c9bf1512 100644 --- a/sdk/src/metrics/BUILD +++ b/sdk/src/metrics/BUILD @@ -15,6 +15,7 @@ cc_library( "//sdk/src/common:disabled", "//sdk/src/common:global_log_handler", "//sdk/src/common:random", + "//sdk/src/common:wildcard_match", "//sdk/src/resource", ], ) diff --git a/sdk/src/metrics/CMakeLists.txt b/sdk/src/metrics/CMakeLists.txt index 1ced671f3a..588af0fcd6 100644 --- a/sdk/src/metrics/CMakeLists.txt +++ b/sdk/src/metrics/CMakeLists.txt @@ -30,6 +30,7 @@ add_library( aggregation/sum_aggregation.cc data/circular_buffer.cc exemplar/reservoir.cc + view/attributes_processor.cc view/instrument_selector_factory.cc view/meter_selector_factory.cc view/view_factory.cc diff --git a/sdk/src/metrics/view/attributes_processor.cc b/sdk/src/metrics/view/attributes_processor.cc new file mode 100644 index 0000000000..fa60b0d691 --- /dev/null +++ b/sdk/src/metrics/view/attributes_processor.cc @@ -0,0 +1,68 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/metrics/view/attributes_processor.h" + +#include + +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/common/key_value_iterable.h" +#include "opentelemetry/nostd/function_ref.h" +#include "opentelemetry/nostd/string_view.h" +#include "opentelemetry/version.h" +#include "src/common/wildcard_match.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace metrics +{ + +IncludeExcludeAttributesProcessor::IncludeExcludeAttributesProcessor( + bool include_all, + std::vector included_patterns, + std::vector excluded_patterns) + : include_all_(include_all), + included_patterns_(std::move(included_patterns)), + excluded_patterns_(std::move(excluded_patterns)) +{} + +MetricAttributes IncludeExcludeAttributesProcessor::process( + const opentelemetry::common::KeyValueIterable &attributes) const noexcept +{ + MetricAttributes result; + attributes.ForEachKeyValue( + [&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept { + if (isPresent(key)) + { + result.SetAttribute(key, value); + } + return true; + }); + + result.UpdateHash(); + return result; +} + +bool IncludeExcludeAttributesProcessor::isPresent(nostd::string_view key) const noexcept +{ + return (include_all_ || MatchesAny(included_patterns_, key)) && + !MatchesAny(excluded_patterns_, key); +} + +bool IncludeExcludeAttributesProcessor::MatchesAny(const std::vector &patterns, + nostd::string_view key) noexcept +{ + for (const auto &pattern : patterns) + { + if (common::WildcardMatch(pattern, key)) + { + return true; + } + } + return false; +} + +} // namespace metrics +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/configuration/sdk_builder_test.cc b/sdk/test/configuration/sdk_builder_test.cc index 242ddc4049..7fbde787a1 100644 --- a/sdk/test/configuration/sdk_builder_test.cc +++ b/sdk/test/configuration/sdk_builder_test.cc @@ -36,6 +36,7 @@ #include "opentelemetry/sdk/configuration/explicit_bucket_histogram_aggregation_configuration.h" #include "opentelemetry/sdk/configuration/extension_push_metric_exporter_builder.h" #include "opentelemetry/sdk/configuration/extension_push_metric_exporter_configuration.h" +#include "opentelemetry/sdk/configuration/include_exclude_configuration.h" #include "opentelemetry/sdk/configuration/instrument_type.h" #include "opentelemetry/sdk/configuration/logger_config_configuration.h" #include "opentelemetry/sdk/configuration/logger_configurator_configuration.h" @@ -51,6 +52,7 @@ #include "opentelemetry/sdk/configuration/sdk_builder.h" #include "opentelemetry/sdk/configuration/severity_number.h" #include "opentelemetry/sdk/configuration/span_limits_configuration.h" +#include "opentelemetry/sdk/configuration/string_array_configuration.h" #include "opentelemetry/sdk/configuration/trace_id_ratio_based_sampler_configuration.h" #include "opentelemetry/sdk/configuration/tracer_provider_configuration.h" #include "opentelemetry/sdk/configuration/unsupported_exception.h" @@ -69,6 +71,7 @@ #include "opentelemetry/sdk/metrics/data/point_data.h" #include "opentelemetry/sdk/metrics/instruments.h" #include "opentelemetry/sdk/metrics/metric_reader.h" +#include "opentelemetry/sdk/metrics/view/attributes_processor.h" #include "opentelemetry/sdk/metrics/view/view.h" #include "opentelemetry/sdk/metrics/view/view_registry.h" #include "opentelemetry/sdk/resource/resource.h" @@ -813,6 +816,75 @@ TEST(SdkBuilder, CreatePeriodicMetricReader) 800u); } +TEST(SdkBuilder, CreateAttributesProcessor) +{ + std::map attributes = {{"included", 1}, {"excluded", 2}, {"unlisted", 3}}; + opentelemetry::common::KeyValueIterableView> iterable(attributes); + config_sdk::SdkBuilder builder(std::make_shared()); + + // When both lists are configured, exclusion takes precedence over inclusion. + { + auto model = std::make_unique(); + model->included = std::make_unique(); + model->included->string_array = {"included", "excluded"}; + model->excluded = std::make_unique(); + model->excluded->string_array = {"excluded"}; + + auto processor = builder.CreateAttributesProcessor(model); + ASSERT_NE(processor, nullptr); + auto filtered = processor->process(iterable); + + EXPECT_EQ(filtered.size(), 1u); + EXPECT_NE(filtered.find("included"), filtered.end()); + } + + // Wildcard patterns are evaluated per key, with exclusion taking precedence. + { + std::map wildcard_attributes = { + {"foo.bar", 1}, {"foo.baz", 2}, {"question.x", 3}, {"question.xy", 4}, {"other", 5}}; + opentelemetry::common::KeyValueIterableView> wildcard_iterable( + wildcard_attributes); + + auto model = std::make_unique(); + model->included = std::make_unique(); + model->included->string_array = {"foo.*", "question.?"}; + model->excluded = std::make_unique(); + model->excluded->string_array = {"foo.bar"}; + + auto processor = builder.CreateAttributesProcessor(model); + ASSERT_NE(processor, nullptr); + auto filtered = processor->process(wildcard_iterable); + + EXPECT_EQ(filtered.size(), 2u); + EXPECT_NE(filtered.find("foo.baz"), filtered.end()); + EXPECT_NE(filtered.find("question.x"), filtered.end()); + } + + // An exclude-only configuration retains every key that is not excluded. + { + auto model = std::make_unique(); + model->excluded = std::make_unique(); + model->excluded->string_array = {"excluded"}; + + auto processor = builder.CreateAttributesProcessor(model); + ASSERT_NE(processor, nullptr); + auto filtered = processor->process(iterable); + + EXPECT_EQ(filtered.size(), 2u); + EXPECT_EQ(filtered.find("excluded"), filtered.end()); + } + + // An empty include/exclude block leaves attributes unchanged. + { + auto model = std::make_unique(); + auto processor = builder.CreateAttributesProcessor(model); + ASSERT_NE(processor, nullptr); + auto filtered = processor->process(iterable); + + EXPECT_EQ(filtered.size(), attributes.size()); + } +} + namespace { diff --git a/sdk/test/configuration/yaml_metrics_test.cc b/sdk/test/configuration/yaml_metrics_test.cc index 778c716acc..d1119662be 100644 --- a/sdk/test/configuration/yaml_metrics_test.cc +++ b/sdk/test/configuration/yaml_metrics_test.cc @@ -7,6 +7,8 @@ #include #include +#include "opentelemetry/common/key_value_iterable_view.h" +#include "opentelemetry/nostd/utility.h" #include "opentelemetry/sdk/configuration/base2_exponential_bucket_histogram_aggregation_configuration.h" #include "opentelemetry/sdk/configuration/cardinality_limits_configuration.h" #include "opentelemetry/sdk/configuration/configuration.h" @@ -29,6 +31,8 @@ #include "opentelemetry/sdk/configuration/periodic_metric_reader_configuration.h" #include "opentelemetry/sdk/configuration/prometheus_pull_metric_exporter_configuration.h" #include "opentelemetry/sdk/configuration/pull_metric_reader_configuration.h" +#include "opentelemetry/sdk/configuration/registry.h" +#include "opentelemetry/sdk/configuration/sdk_builder.h" #include "opentelemetry/sdk/configuration/string_array_configuration.h" #include "opentelemetry/sdk/configuration/temporality_preference.h" #include "opentelemetry/sdk/configuration/translation_strategy.h" @@ -36,6 +40,7 @@ #include "opentelemetry/sdk/configuration/view_selector_configuration.h" #include "opentelemetry/sdk/configuration/view_stream_configuration.h" #include "opentelemetry/sdk/configuration/yaml_configuration_parser.h" +#include "opentelemetry/sdk/metrics/view/attributes_processor.h" static std::unique_ptr DoParse( const std::string &yaml) @@ -1400,6 +1405,48 @@ file_format: "1.0-metrics" ASSERT_EQ(view->stream->attribute_keys->excluded->string_array[1], "bar.ex"); } +class YamlMetricsEmptyIncluded : public ::testing::TestWithParam +{}; + +TEST_P(YamlMetricsEmptyIncluded, RetainsAllAttributes) +{ + std::string yaml = R"( +file_format: "1.0-metrics" +meter_provider: + readers: + - periodic: + exporter: + console: + views: + - selector: + stream: + attribute_keys: + included: )"; + yaml.append(GetParam()); + yaml.push_back('\n'); + + auto config = DoParse(yaml); + ASSERT_NE(config, nullptr); + ASSERT_NE(config->meter_provider, nullptr); + ASSERT_EQ(config->meter_provider->views.size(), 1u); + auto &model = config->meter_provider->views[0]->stream->attribute_keys; + ASSERT_NE(model, nullptr); + ASSERT_NE(model->included, nullptr); + ASSERT_TRUE(model->included->string_array.empty()); + + std::map attributes = {{"first", 1}, {"second", 2}}; + opentelemetry::common::KeyValueIterableView> iterable(attributes); + opentelemetry::sdk::configuration::SdkBuilder builder( + std::make_shared()); + auto processor = builder.CreateAttributesProcessor(model); + ASSERT_NE(processor, nullptr); + + auto filtered = processor->process(iterable); + EXPECT_EQ(filtered.size(), attributes.size()); +} + +INSTANTIATE_TEST_SUITE_P(EmptyIncludedForms, YamlMetricsEmptyIncluded, ::testing::Values("[]", "")); + TEST(YamlMetrics, no_meter_configurator) { std::string yaml = R"(