Skip to content

Commit

Permalink
add test cases for dyncamically register stats
Browse files Browse the repository at this point in the history
  • Loading branch information
jievince committed Dec 22, 2021
1 parent 88f823e commit 5a51953
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/common/stats/StatsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ void StatsManager::addValue(const CounterId& id, VT value) {

auto& sm = get();
if (!id.valid()) {
LOG(FATAL) << "Invalid counter id";
// The counter is not registered
return;
}
std::string index = id.index();
bool isHisto = id.isHisto();
Expand Down
33 changes: 33 additions & 0 deletions src/common/stats/test/StatsManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ TEST(StatsManager, ReadAllTest) {
StatsManager::addValue(statId2, 1);
StatsManager::addValue(statId2, 2);

// Dynamically register some metrics
auto statId3 = StatsManager::counterWithLabels(statId1, {{"space", "test"}});
auto statId4 = StatsManager::histoWithLabels(statId2, {{"space", "test"}});
StatsManager::addValue(statId3, 1);
StatsManager::addValue(statId3, 2);
StatsManager::addValue(statId4, 1);
StatsManager::addValue(statId4, 2);

auto stats = folly::dynamic::array();
StatsManager::readAllValue(stats);

Expand All @@ -128,6 +136,31 @@ TEST(StatsManager, ReadAllTest) {
EXPECT_FALSE(counterExists(stats, "stat04.count.60", val));
EXPECT_FALSE(counterExists(stats, "stat04.avg.3600", val));
EXPECT_FALSE(counterExists(stats, "stat04.p75.5", val));

EXPECT_TRUE(counterExists(stats, "space_test_stat04.sum.5", val));
EXPECT_EQ(3, val);
EXPECT_TRUE(counterExists(stats, "space_test_stat04.p95.5", val));
EXPECT_EQ(2, val);
EXPECT_TRUE(counterExists(stats, "space_test_stat04.p99.3600", val));
EXPECT_EQ(2, val);

EXPECT_TRUE(counterExists(stats, "space_test_stat03.sum.3600", val));
EXPECT_EQ(3, val);

EXPECT_FALSE(counterExists(stats, "space_test_stat03.count.600", val));
EXPECT_FALSE(counterExists(stats, "space_test_stat03.avg.3600", val));

EXPECT_FALSE(counterExists(stats, "space_test_stat04.rate.600", val));
EXPECT_FALSE(counterExists(stats, "space_test_stat04.count.60", val));
EXPECT_FALSE(counterExists(stats, "space_test_stat04.avg.3600", val));
EXPECT_FALSE(counterExists(stats, "space_test_stat04.p75.5", val));
// Remove the dynamically registered metrics
StatsManager::removeCounterWithLabels(statId1, {{"space", "test"}});
StatsManager::removeHistoWithLabels(statId2, {{"space", "test"}});
auto stats2 = folly::dynamic::array();
StatsManager::readAllValue(stats2);
EXPECT_FALSE(counterExists(stats2, "space_test_stat03.sum.3600", val));
EXPECT_FALSE(counterExists(stats2, "space_test_stat04.p95.5", val));
}

} // namespace stats
Expand Down

0 comments on commit 5a51953

Please sign in to comment.