Skip to content

Commit 35ed30b

Browse files
rayztobz
authored andcommitted
add tobz reduce per-metric overhead change
1 parent 030fb96 commit 35ed30b

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

  • bin/agent-data-plane/src/components/tag_filterlist

bin/agent-data-plane/src/components/tag_filterlist/mod.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,12 @@ impl Transform for TagFilterlist {
230230

231231
#[inline]
232232
fn should_keep_tag(tag: &Tag, is_exclude: bool, names: &HashSet<String, FoldHashState>) -> bool {
233-
is_exclude != names.contains(tag.name())
233+
is_exclude != names.contains(tag.as_borrowed().name())
234234
}
235235

236236
#[inline]
237-
fn count_removed_tags(tags: &TagSet, is_exclude: bool, names: &HashSet<String, FoldHashState>) -> usize {
238-
tags.into_iter()
239-
.filter(|tag| !should_keep_tag(tag, is_exclude, names))
240-
.count()
237+
fn has_removable_tags(tags: &TagSet, is_exclude: bool, names: &HashSet<String, FoldHashState>) -> bool {
238+
tags.into_iter().any(|tag| !should_keep_tag(tag, is_exclude, names))
241239
}
242240

243241
/// Filter the tags of a distribution metric according to the compiled filter table.
@@ -251,20 +249,26 @@ pub fn filter_metric_tags(metric: &mut Metric, filters: &CompiledFilters) -> Fil
251249
return FilterMetricTagsOutcome::RuleMiss;
252250
};
253251

254-
let removed_tags = count_removed_tags(metric.context().tags(), *is_exclude, tag_names);
255-
let removed_origin_tags = count_removed_tags(metric.context().origin_tags(), *is_exclude, tag_names);
256-
let total_removed = removed_tags + removed_origin_tags;
252+
let is_exclude = *is_exclude;
257253

258-
if total_removed == 0 {
254+
let has_tag_removals = has_removable_tags(metric.context().tags(), is_exclude, tag_names);
255+
let has_origin_removals = has_removable_tags(metric.context().origin_tags(), is_exclude, tag_names);
256+
257+
if !has_tag_removals && !has_origin_removals {
259258
return FilterMetricTagsOutcome::NoChange;
260259
}
261260

261+
let mut total_removed = 0;
262262
metric.context_mut().with_tag_sets_mut(|tags, origin_tags| {
263-
if removed_tags > 0 {
264-
tags.retain(|tag| should_keep_tag(tag, *is_exclude, tag_names));
263+
if has_tag_removals {
264+
let before = tags.len();
265+
tags.retain(|tag| should_keep_tag(tag, is_exclude, tag_names));
266+
total_removed += before - tags.len();
265267
}
266-
if removed_origin_tags > 0 {
267-
origin_tags.retain(|tag| should_keep_tag(tag, *is_exclude, tag_names));
268+
if has_origin_removals {
269+
let before = origin_tags.len();
270+
origin_tags.retain(|tag| should_keep_tag(tag, is_exclude, tag_names));
271+
total_removed += before - origin_tags.len();
268272
}
269273
});
270274

0 commit comments

Comments
 (0)