Skip to content

Commit 3db66c9

Browse files
[Metrics] New Attribute Processor for Exclude list
1 parent d660b3b commit 3db66c9

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

sdk/include/opentelemetry/sdk/metrics/view/attributes_processor.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,49 @@ class FilteringAttributesProcessor : public AttributesProcessor
101101
std::unordered_map<std::string, bool> allowed_attribute_keys_;
102102
};
103103

104+
/**
105+
* FilteringExcludeAttributeProcessor filters by exclude attribute list and drops names if they are
106+
* present in the exclude list
107+
*/
108+
109+
class FilteringExcludeAttributesProcessor : AttributesProcessor
110+
{
111+
public:
112+
FilteringExcludeAttributesProcessor(std::unordered_map<std::string, bool> &&exclude_list = {})
113+
: exclude_list_(std::move(exclude_list))
114+
{}
115+
116+
FilteringExcludeAttributesProcessor(const std::unordered_map<std::string, bool> &exclude_list = {})
117+
: exclude_list_(exclude_list)
118+
{}
119+
120+
MetricAttributes process(
121+
const opentelemetry::common::KeyValueIterable &attributes) const noexcept override
122+
{
123+
MetricAttributes result;
124+
attributes.ForEachKeyValue(
125+
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
126+
if (exclude_list_.find(key.data()) == exclude_list_.end())
127+
{
128+
result.SetAttribute(key, value);
129+
return true;
130+
}
131+
return true;
132+
});
133+
134+
result.UpdateHash();
135+
return result;
136+
}
137+
138+
bool isPresent(nostd::string_view key) const noexcept override
139+
{
140+
return (exclude_list_.find(key.data()) == exclude_list_.end());
141+
}
142+
143+
private:
144+
std::unordered_map<std::string, bool> exclude_list_;
145+
};
146+
104147
} // namespace metrics
105148
} // namespace sdk
106149
OPENTELEMETRY_END_NAMESPACE

sdk/test/metrics/attributes_processor_test.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,40 @@ TEST(AttributesProcessor, FilteringAllAttributesProcessor)
5151
auto filtered_attributes = attributes_processor.process(iterable);
5252
EXPECT_EQ(filter.size(), kNumFilterAttributes);
5353
}
54+
55+
TEST(AttributesProcessor, FilteringExcludeAttributesProcessor)
56+
{
57+
const int kNumFilterAttributes = 3;
58+
std::unordered_map<std::string, bool> filter = {
59+
{"attr2", true}, {"attr4", true}, {"attr6", true}};
60+
const int kNumAttributes = 6;
61+
std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"};
62+
int values[kNumAttributes] = {10, 20, 30, 40, 50, 60};
63+
std::map<std::string, int> attributes = {{keys[0], values[0]}, {keys[1], values[1]},
64+
{keys[2], values[2]}, {keys[3], values[3]},
65+
{keys[4], values[4]}, {keys[5], values[5]}};
66+
FilteringExcludeAttributesProcessor attributes_processor(filter);
67+
opentelemetry::common::KeyValueIterableView<std::map<std::string, int>> iterable(attributes);
68+
auto filtered_attributes = attributes_processor.process(iterable);
69+
for (auto &e : filtered_attributes)
70+
{
71+
EXPECT_TRUE(filter.find(e.first) == filter.end());
72+
}
73+
EXPECT_EQ(filter.size(), kNumFilterAttributes);
74+
}
75+
76+
TEST(AttributesProcessor, FilteringExcludeAllAtrributesProcessor)
77+
{
78+
const int kNumFilterAttributes = 0;
79+
std::unordered_map<std::string, bool> filter = {};
80+
const int kNumAttributes = 6;
81+
std::string keys[kNumAttributes] = {"attr1", "attr2", "attr3", "attr4", "attr5", "attr6"};
82+
int values[kNumAttributes] = {10, 20, 30, 40, 50, 60};
83+
std::map<std::string, int> attributes = {{keys[0], values[0]}, {keys[1], values[1]},
84+
{keys[2], values[2]}, {keys[3], values[3]},
85+
{keys[4], values[4]}, {keys[5], values[5]}};
86+
FilteringExcludeAttributesProcessor attributes_processor(filter);
87+
opentelemetry::common::KeyValueIterableView<std::map<std::string, int>> iterable(attributes);
88+
auto filtered_attributes = attributes_processor.process(iterable);
89+
EXPECT_EQ(filter.size(), kNumFilterAttributes);
90+
}

0 commit comments

Comments
 (0)