From 3db66c920d196b4b5a21d0abee140315808b8c5e Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Mon, 8 Sep 2025 19:10:46 +0530 Subject: [PATCH 1/5] [Metrics] New Attribute Processor for Exclude list --- .../sdk/metrics/view/attributes_processor.h | 43 +++++++++++++++++++ sdk/test/metrics/attributes_processor_test.cc | 37 ++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h index 7ab8cafb13..c96087cef0 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h @@ -101,6 +101,49 @@ class FilteringAttributesProcessor : public AttributesProcessor std::unordered_map allowed_attribute_keys_; }; +/** + * FilteringExcludeAttributeProcessor filters by exclude attribute list and drops names if they are + * present in the exclude list + */ + +class FilteringExcludeAttributesProcessor : AttributesProcessor +{ +public: + FilteringExcludeAttributesProcessor(std::unordered_map &&exclude_list = {}) + : exclude_list_(std::move(exclude_list)) + {} + + FilteringExcludeAttributesProcessor(const std::unordered_map &exclude_list = {}) + : exclude_list_(exclude_list) + {} + + MetricAttributes process( + const opentelemetry::common::KeyValueIterable &attributes) const noexcept override + { + MetricAttributes result; + attributes.ForEachKeyValue( + [&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept { + if (exclude_list_.find(key.data()) == exclude_list_.end()) + { + result.SetAttribute(key, value); + return true; + } + return true; + }); + + result.UpdateHash(); + return result; + } + + bool isPresent(nostd::string_view key) const noexcept override + { + return (exclude_list_.find(key.data()) == exclude_list_.end()); + } + +private: + std::unordered_map exclude_list_; +}; + } // namespace metrics } // namespace sdk OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/test/metrics/attributes_processor_test.cc b/sdk/test/metrics/attributes_processor_test.cc index 25652ed3e1..aff40b4124 100644 --- a/sdk/test/metrics/attributes_processor_test.cc +++ b/sdk/test/metrics/attributes_processor_test.cc @@ -51,3 +51,40 @@ TEST(AttributesProcessor, FilteringAllAttributesProcessor) auto filtered_attributes = attributes_processor.process(iterable); EXPECT_EQ(filter.size(), kNumFilterAttributes); } + +TEST(AttributesProcessor, FilteringExcludeAttributesProcessor) +{ + const int kNumFilterAttributes = 3; + std::unordered_map filter = { + {"attr2", true}, {"attr4", true}, {"attr6", true}}; + const int kNumAttributes = 6; + std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"}; + int values[kNumAttributes] = {10, 20, 30, 40, 50, 60}; + std::map attributes = {{keys[0], values[0]}, {keys[1], values[1]}, + {keys[2], values[2]}, {keys[3], values[3]}, + {keys[4], values[4]}, {keys[5], values[5]}}; + FilteringExcludeAttributesProcessor attributes_processor(filter); + opentelemetry::common::KeyValueIterableView> iterable(attributes); + auto filtered_attributes = attributes_processor.process(iterable); + for (auto &e : filtered_attributes) + { + EXPECT_TRUE(filter.find(e.first) == filter.end()); + } + EXPECT_EQ(filter.size(), kNumFilterAttributes); +} + +TEST(AttributesProcessor, FilteringExcludeAllAtrributesProcessor) +{ + const int kNumFilterAttributes = 0; + std::unordered_map filter = {}; + const int kNumAttributes = 6; + std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"}; + int values[kNumAttributes] = {10, 20, 30, 40, 50, 60}; + std::map attributes = {{keys[0], values[0]}, {keys[1], values[1]}, + {keys[2], values[2]}, {keys[3], values[3]}, + {keys[4], values[4]}, {keys[5], values[5]}}; + FilteringExcludeAttributesProcessor attributes_processor(filter); + opentelemetry::common::KeyValueIterableView> iterable(attributes); + auto filtered_attributes = attributes_processor.process(iterable); + EXPECT_EQ(filter.size(), kNumFilterAttributes); +} From f0ecb7e12d698583fc6354fd8fa0bbaad0b0adc0 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Mon, 8 Sep 2025 19:21:02 +0530 Subject: [PATCH 2/5] format fix --- .../opentelemetry/sdk/metrics/view/attributes_processor.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h index c96087cef0..8bc099ebcc 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h @@ -113,7 +113,8 @@ class FilteringExcludeAttributesProcessor : AttributesProcessor : exclude_list_(std::move(exclude_list)) {} - FilteringExcludeAttributesProcessor(const std::unordered_map &exclude_list = {}) + FilteringExcludeAttributesProcessor( + const std::unordered_map &exclude_list = {}) : exclude_list_(exclude_list) {} From dc43c08dd3a1002bdd6519faa65fe60445e7012e Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Tue, 9 Sep 2025 02:49:13 +0530 Subject: [PATCH 3/5] string_view fix --- .../sdk/metrics/view/attributes_processor.h | 10 +++++----- sdk/test/metrics/attributes_processor_test.cc | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h index 8bc099ebcc..7b320d8d6a 100644 --- a/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h +++ b/sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h @@ -80,7 +80,7 @@ class FilteringAttributesProcessor : public AttributesProcessor MetricAttributes result; attributes.ForEachKeyValue( [&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept { - if (allowed_attribute_keys_.find(key.data()) != allowed_attribute_keys_.end()) + if (allowed_attribute_keys_.find(std::string(key)) != allowed_attribute_keys_.end()) { result.SetAttribute(key, value); return true; @@ -94,7 +94,7 @@ class FilteringAttributesProcessor : public AttributesProcessor bool isPresent(nostd::string_view key) const noexcept override { - return (allowed_attribute_keys_.find(key.data()) != allowed_attribute_keys_.end()); + return (allowed_attribute_keys_.find(std::string(key)) != allowed_attribute_keys_.end()); } private: @@ -106,7 +106,7 @@ class FilteringAttributesProcessor : public AttributesProcessor * present in the exclude list */ -class FilteringExcludeAttributesProcessor : AttributesProcessor +class FilteringExcludeAttributesProcessor : public AttributesProcessor { public: FilteringExcludeAttributesProcessor(std::unordered_map &&exclude_list = {}) @@ -124,7 +124,7 @@ class FilteringExcludeAttributesProcessor : AttributesProcessor MetricAttributes result; attributes.ForEachKeyValue( [&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept { - if (exclude_list_.find(key.data()) == exclude_list_.end()) + if (exclude_list_.find(std::string(key)) == exclude_list_.end()) { result.SetAttribute(key, value); return true; @@ -138,7 +138,7 @@ class FilteringExcludeAttributesProcessor : AttributesProcessor bool isPresent(nostd::string_view key) const noexcept override { - return (exclude_list_.find(key.data()) == exclude_list_.end()); + return (exclude_list_.find(std::string(key)) == exclude_list_.end()); } private: diff --git a/sdk/test/metrics/attributes_processor_test.cc b/sdk/test/metrics/attributes_processor_test.cc index aff40b4124..c889f8d0c1 100644 --- a/sdk/test/metrics/attributes_processor_test.cc +++ b/sdk/test/metrics/attributes_processor_test.cc @@ -73,7 +73,7 @@ TEST(AttributesProcessor, FilteringExcludeAttributesProcessor) EXPECT_EQ(filter.size(), kNumFilterAttributes); } -TEST(AttributesProcessor, FilteringExcludeAllAtrributesProcessor) +TEST(AttributesProcessor, FilteringExcludeAllAttributesProcessor) { const int kNumFilterAttributes = 0; std::unordered_map filter = {}; From 09abd1755626ad7b3c04d07180ce5af932bcde1c Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Thu, 11 Sep 2025 01:24:49 +0530 Subject: [PATCH 4/5] test changes --- sdk/test/metrics/attributes_processor_test.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/sdk/test/metrics/attributes_processor_test.cc b/sdk/test/metrics/attributes_processor_test.cc index c889f8d0c1..39f9223149 100644 --- a/sdk/test/metrics/attributes_processor_test.cc +++ b/sdk/test/metrics/attributes_processor_test.cc @@ -57,12 +57,13 @@ TEST(AttributesProcessor, FilteringExcludeAttributesProcessor) const int kNumFilterAttributes = 3; std::unordered_map filter = { {"attr2", true}, {"attr4", true}, {"attr6", true}}; - const int kNumAttributes = 6; - std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"}; - int values[kNumAttributes] = {10, 20, 30, 40, 50, 60}; - std::map attributes = {{keys[0], values[0]}, {keys[1], values[1]}, - {keys[2], values[2]}, {keys[3], values[3]}, - {keys[4], values[4]}, {keys[5], values[5]}}; + const int kNumAttributes = 7; + std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", + "attr5", "attr6", "attr7"}; + int values[kNumAttributes] = {10, 20, 30, 40, 50, 60, 70}; + std::map attributes = { + {keys[0], values[0]}, {keys[1], values[1]}, {keys[2], values[2]}, {keys[3], values[3]}, + {keys[4], values[4]}, {keys[5], values[5]}, {keys[6], values[6]}}; FilteringExcludeAttributesProcessor attributes_processor(filter); opentelemetry::common::KeyValueIterableView> iterable(attributes); auto filtered_attributes = attributes_processor.process(iterable); @@ -70,7 +71,8 @@ TEST(AttributesProcessor, FilteringExcludeAttributesProcessor) { EXPECT_TRUE(filter.find(e.first) == filter.end()); } - EXPECT_EQ(filter.size(), kNumFilterAttributes); + int expected_filtered_attributes_size = 4; + EXPECT_EQ(filtered_attributes.size(), expected_filtered_attributes_size); } TEST(AttributesProcessor, FilteringExcludeAllAttributesProcessor) @@ -86,5 +88,5 @@ TEST(AttributesProcessor, FilteringExcludeAllAttributesProcessor) FilteringExcludeAttributesProcessor attributes_processor(filter); opentelemetry::common::KeyValueIterableView> iterable(attributes); auto filtered_attributes = attributes_processor.process(iterable); - EXPECT_EQ(filter.size(), kNumFilterAttributes); + EXPECT_EQ(filtered_attributes.size(), kNumAttributes); } From c74134e4de413ce1de3b6c1e0de412b1ef98b8e9 Mon Sep 17 00:00:00 2001 From: nikhilbhatia08 Date: Thu, 11 Sep 2025 02:24:59 +0530 Subject: [PATCH 5/5] removal of unused vars --- sdk/test/metrics/attributes_processor_test.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/sdk/test/metrics/attributes_processor_test.cc b/sdk/test/metrics/attributes_processor_test.cc index 39f9223149..8de97cc6df 100644 --- a/sdk/test/metrics/attributes_processor_test.cc +++ b/sdk/test/metrics/attributes_processor_test.cc @@ -54,7 +54,6 @@ TEST(AttributesProcessor, FilteringAllAttributesProcessor) TEST(AttributesProcessor, FilteringExcludeAttributesProcessor) { - const int kNumFilterAttributes = 3; std::unordered_map filter = { {"attr2", true}, {"attr4", true}, {"attr6", true}}; const int kNumAttributes = 7; @@ -77,7 +76,6 @@ TEST(AttributesProcessor, FilteringExcludeAttributesProcessor) TEST(AttributesProcessor, FilteringExcludeAllAttributesProcessor) { - const int kNumFilterAttributes = 0; std::unordered_map filter = {}; const int kNumAttributes = 6; std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"};