Skip to content

[TrackEvent] Tweak category/tags filters priority #1432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 2, 2025
Merged
5 changes: 4 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ Unreleased:
* Improved duration based slice aggregators - slices are now trimmed to the
width of the area selection.
SDK:
*
* TrackEvent priority of enabled/disabled category and tags were reorded.
The new logic ranks (in this order)
specific > pattern > single char "*" wildcard,
categories (enabled > disabled) > tags (disabled > enabled).


v50.1 - 2025-04-17:
Expand Down
39 changes: 22 additions & 17 deletions src/tracing/internal/track_event_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class TrackEventSessionObserverRegistry {
std::vector<RegisteredObserver> observers_;
};

enum class MatchType { kExact, kPattern };
enum class MatchType { kExact, kPattern, kWildcard };

bool NameMatchesPattern(const std::string& pattern,
const std::string& name,
Expand All @@ -122,6 +122,9 @@ bool NameMatchesPattern(const std::string& pattern,
size_t i = pattern.find('*');
if (i != std::string::npos) {
PERFETTO_DCHECK(i == pattern.size() - 1);
if (i == 0) {
return match_type == MatchType::kWildcard;
}
if (match_type != MatchType::kPattern)
return false;
return name.substr(0, i) == pattern.substr(0, i);
Expand Down Expand Up @@ -311,21 +314,21 @@ bool TrackEventInternal::IsCategoryEnabled(
return false;
};

// First try exact matches, then pattern matches.
const std::array<MatchType, 2> match_types = {
{MatchType::kExact, MatchType::kPattern}};
// First try exact matches, then pattern matches. Last, try the global
// wildcard.
const std::array<MatchType, 3> match_types = {
{MatchType::kExact, MatchType::kPattern, MatchType::kWildcard}};
for (auto match_type : match_types) {
// 1. Enabled categories.
if (NameMatchesPatternList(config.enabled_categories(), category.name,
match_type)) {
return true;
}

// 2. Enabled tags.
if (has_matching_tag([&](const char* tag) {
return NameMatchesPatternList(config.enabled_tags(), tag, match_type);
})) {
return true;
// 2. Disabled categories.
if (NameMatchesPatternList(config.disabled_categories(), category.name,
match_type)) {
return false;
}

// 2.5. A special case for Chrome's legacy disabled-by-default categories.
Expand All @@ -343,25 +346,27 @@ bool TrackEventInternal::IsCategoryEnabled(
}
}

// 3. Disabled categories.
if (NameMatchesPatternList(config.disabled_categories(), category.name,
match_type)) {
return false;
}

// 4. Disabled tags.
// 3. Disabled tags.
if (has_matching_tag([&](const char* tag) {
if (config.disabled_tags_size()) {
return NameMatchesPatternList(config.disabled_tags(), tag,
match_type);
} else {
} else if (config.enabled_tags_size() == 0) {
// The "slow" and "debug" tags are disabled by default.
return NameMatchesPattern(kSlowTag, tag, match_type) ||
NameMatchesPattern(kDebugTag, tag, match_type);
}
return false;
})) {
return false;
}

// 4. Enabled tags.
if (has_matching_tag([&](const char* tag) {
return NameMatchesPatternList(config.enabled_tags(), tag, match_type);
})) {
return true;
}
}

// If nothing matched, enable the category by default.
Expand Down
16 changes: 8 additions & 8 deletions src/tracing/test/api_integrationtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3876,6 +3876,7 @@ TEST_P(PerfettoApiTest, TrackEventConfig) {
TRACE_EVENT_BEGIN("cat", "SlowEvent");
TRACE_EVENT_BEGIN("cat.verbose", "DebugEvent");
TRACE_EVENT_BEGIN("test", "TagEvent");
TRACE_EVENT_BEGIN("test.verbose", "VerboseTagEvent");
TRACE_EVENT_BEGIN(TRACE_DISABLED_BY_DEFAULT("cat"), "SlowDisabledEvent");
perfetto::DynamicCategory dyn_foo{"dynamic,foo"};
TRACE_EVENT_BEGIN(dyn_foo, "DynamicGroupFooEvent");
Expand Down Expand Up @@ -4012,7 +4013,8 @@ TEST_P(PerfettoApiTest, TrackEventConfig) {
EXPECT_FALSE(TRACE_EVENT_CATEGORY_ENABLED("foo"));
EXPECT_TRUE(TRACE_EVENT_CATEGORY_ENABLED("test"));
});
EXPECT_THAT(slices, ElementsAre("B:test.TagEvent"));
EXPECT_THAT(slices, ElementsAre("B:test.TagEvent",
"B:test.verbose.VerboseTagEvent"));
}

// Enable just slow categories.
Expand Down Expand Up @@ -4062,6 +4064,7 @@ TEST_P(PerfettoApiTest, TrackEventConfig) {
"B:baz,bar,quux.MultiBar", "B:red,green,blue,foo.MultiFoo",
"B:red,green,blue,yellow.MultiNone", "B:cat.SlowEvent",
"B:cat.verbose.DebugEvent", "B:test.TagEvent",
"B:test.verbose.VerboseTagEvent",
"B:disabled-by-default-cat.SlowDisabledEvent",
"B:$dynamic,$foo.DynamicGroupFooEvent",
"B:$dynamic,$bar.DynamicGroupBarEvent"));
Expand All @@ -4079,13 +4082,12 @@ TEST_P(PerfettoApiTest, TrackEventConfig) {
}

// Disable category with a pattern.
// TODO(crbug.com/260418655): Fix once API changes are announced.
{
perfetto::protos::gen::TrackEventConfig te_cfg;
te_cfg.add_enabled_categories("*");
te_cfg.add_disabled_categories("fo*");
run_config(te_cfg, []() {
EXPECT_TRUE(TRACE_EVENT_CATEGORY_ENABLED("foo"));
EXPECT_FALSE(TRACE_EVENT_CATEGORY_ENABLED("foo"));
EXPECT_TRUE(TRACE_EVENT_CATEGORY_ENABLED("bar"));
});
}
Expand All @@ -4101,27 +4103,25 @@ TEST_P(PerfettoApiTest, TrackEventConfig) {
}

// Enable tag and disable category explicitly.
// TODO(crbug.com/260418655): Fix once API changes are announced.
{
perfetto::protos::gen::TrackEventConfig te_cfg;
te_cfg.add_disabled_categories("slow_category");
te_cfg.add_enabled_tags("slow");
te_cfg.add_disabled_categories("*");
run_config(te_cfg, []() {
EXPECT_TRUE(TRACE_EVENT_CATEGORY_ENABLED("slow_category"));
EXPECT_FALSE(TRACE_EVENT_CATEGORY_ENABLED("slow_category"));
});
}

// Enable tag and disable another.
// TODO(crbug.com/260418655): Fix once API changes are announced.
{
perfetto::protos::gen::TrackEventConfig te_cfg;
te_cfg.add_enabled_tags("tag");
te_cfg.add_disabled_tags("slow");
te_cfg.add_disabled_tags("debug");
te_cfg.add_disabled_categories("*");
run_config(te_cfg, []() {
EXPECT_TRUE(TRACE_EVENT_CATEGORY_ENABLED("test"));
EXPECT_TRUE(TRACE_EVENT_CATEGORY_ENABLED("test.verbose"));
EXPECT_FALSE(TRACE_EVENT_CATEGORY_ENABLED("test.verbose"));
});
}
}
Expand Down