1/ Change all "name" to id in statsD.
2/ Handle Subscription for alert.
3/ Support no_report_metric
Bug: 69522276
Test: all statsd unit tests passed.
Change-Id: I851b235f2d149b8602b0cad632d5bf541962f40a
diff --git a/cmds/statsd/tests/ConfigManager_test.cpp b/cmds/statsd/tests/ConfigManager_test.cpp
index 3d923e2..3eac5d2 100644
--- a/cmds/statsd/tests/ConfigManager_test.cpp
+++ b/cmds/statsd/tests/ConfigManager_test.cpp
@@ -14,6 +14,7 @@
#include "src/config/ConfigManager.h"
#include "src/metrics/MetricsManager.h"
+#include "statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -31,7 +32,7 @@
namespace statsd {
static ostream& operator<<(ostream& os, const StatsdConfig& config) {
- return os << "StatsdConfig{name=" << config.name().c_str() << "}";
+ return os << "StatsdConfig{id=" << config.id() << "}";
}
} // namespace statsd
@@ -50,19 +51,21 @@
/**
* Validate that the ConfigKey is the one we wanted.
*/
-MATCHER_P2(ConfigKeyEq, uid, name, "") {
- return arg.GetUid() == uid && arg.GetName() == name;
+MATCHER_P2(ConfigKeyEq, uid, id, "") {
+ return arg.GetUid() == uid && (long long)arg.GetId() == (long long)id;
}
/**
* Validate that the StatsdConfig is the one we wanted.
*/
-MATCHER_P(StatsdConfigEq, name, "") {
- return arg.name() == name;
+MATCHER_P(StatsdConfigEq, id, 0) {
+ return (long long)arg.id() == (long long)id;
}
+const int64_t testConfigId = 12345;
+
TEST(ConfigManagerTest, TestFakeConfig) {
- auto metricsManager = std::make_unique<MetricsManager>(ConfigKey(0, "test"),
+ auto metricsManager = std::make_unique<MetricsManager>(ConfigKey(0, testConfigId),
build_fake_config(), 1000, new UidMap());
EXPECT_TRUE(metricsManager->isConfigValid());
}
@@ -77,13 +80,13 @@
manager->AddListener(listener);
StatsdConfig config91;
- config91.set_name("91");
+ config91.set_id(91);
StatsdConfig config92;
- config92.set_name("92");
+ config92.set_id(92);
StatsdConfig config93;
- config93.set_name("93");
+ config93.set_id(93);
StatsdConfig config94;
- config94.set_name("94");
+ config94.set_id(94);
{
InSequence s;
@@ -91,42 +94,46 @@
manager->Startup();
// Add another one
- EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq("91")))
+ EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("zzz")),
+ StatsdConfigEq(91)))
.RetiresOnSaturation();
- manager->UpdateConfig(ConfigKey(1, "zzz"), config91);
+ manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config91);
// Update It
- EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "zzz"), StatsdConfigEq("92")))
+ EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("zzz")),
+ StatsdConfigEq(92)))
.RetiresOnSaturation();
- manager->UpdateConfig(ConfigKey(1, "zzz"), config92);
+ manager->UpdateConfig(ConfigKey(1, StringToId("zzz")), config92);
// Add one with the same uid but a different name
- EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, "yyy"), StatsdConfigEq("93")))
+ EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(1, StringToId("yyy")),
+ StatsdConfigEq(93)))
.RetiresOnSaturation();
- manager->UpdateConfig(ConfigKey(1, "yyy"), config93);
+ manager->UpdateConfig(ConfigKey(1, StringToId("yyy")), config93);
// Add one with the same name but a different uid
- EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(2, "zzz"), StatsdConfigEq("94")))
+ EXPECT_CALL(*(listener.get()), OnConfigUpdated(ConfigKeyEq(2, StringToId("zzz")),
+ StatsdConfigEq(94)))
.RetiresOnSaturation();
- manager->UpdateConfig(ConfigKey(2, "zzz"), config94);
+ manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config94);
// Remove (1,yyy)
- EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, "yyy")))
+ EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("yyy"))))
.RetiresOnSaturation();
- manager->RemoveConfig(ConfigKey(1, "yyy"));
+ manager->RemoveConfig(ConfigKey(1, StringToId("yyy")));
// Remove (2,zzz)
- EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "zzz")))
+ EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))))
.RetiresOnSaturation();
- manager->RemoveConfig(ConfigKey(2, "zzz"));
+ manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
// Remove (1,zzz)
- EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, "zzz")))
+ EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(1, StringToId("zzz"))))
.RetiresOnSaturation();
- manager->RemoveConfig(ConfigKey(1, "zzz"));
+ manager->RemoveConfig(ConfigKey(1, StringToId("zzz")));
// Remove (2,zzz) again and we shouldn't get the callback
- manager->RemoveConfig(ConfigKey(2, "zzz"));
+ manager->RemoveConfig(ConfigKey(2, StringToId("zzz")));
}
}
@@ -142,16 +149,16 @@
StatsdConfig config;
EXPECT_CALL(*(listener.get()), OnConfigUpdated(_, _)).Times(5);
- EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "xxx")));
- EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "yyy")));
- EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, "zzz")));
+ EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("xxx"))));
+ EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("yyy"))));
+ EXPECT_CALL(*(listener.get()), OnConfigRemoved(ConfigKeyEq(2, StringToId("zzz"))));
manager->Startup();
- manager->UpdateConfig(ConfigKey(1, "aaa"), config);
- manager->UpdateConfig(ConfigKey(2, "xxx"), config);
- manager->UpdateConfig(ConfigKey(2, "yyy"), config);
- manager->UpdateConfig(ConfigKey(2, "zzz"), config);
- manager->UpdateConfig(ConfigKey(3, "bbb"), config);
+ manager->UpdateConfig(ConfigKey(1, StringToId("aaa")), config);
+ manager->UpdateConfig(ConfigKey(2, StringToId("xxx")), config);
+ manager->UpdateConfig(ConfigKey(2, StringToId("yyy")), config);
+ manager->UpdateConfig(ConfigKey(2, StringToId("zzz")), config);
+ manager->UpdateConfig(ConfigKey(3, StringToId("bbb")), config);
manager->RemoveConfigs(2);
}
diff --git a/cmds/statsd/tests/MetricsManager_test.cpp b/cmds/statsd/tests/MetricsManager_test.cpp
index 6cd31dd..96ee9c5 100644
--- a/cmds/statsd/tests/MetricsManager_test.cpp
+++ b/cmds/statsd/tests/MetricsManager_test.cpp
@@ -21,6 +21,7 @@
#include "src/metrics/MetricProducer.h"
#include "src/metrics/ValueMetricProducer.h"
#include "src/metrics/metrics_manager_util.h"
+#include "statsd_test_util.h"
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
@@ -40,16 +41,16 @@
// TODO: ADD MORE TEST CASES.
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
const long timeBaseSec = 1000;
StatsdConfig buildGoodConfig() {
StatsdConfig config;
- config.set_name("12345");
+ config.set_id(12345);
AtomMatcher* eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_IS_ON");
+ eventMatcher->set_id(StringToId("SCREEN_IS_ON"));
SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/);
@@ -59,7 +60,7 @@
2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_IS_OFF");
+ eventMatcher->set_id(StringToId("SCREEN_IS_OFF"));
simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/);
@@ -69,23 +70,25 @@
1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_ON_OR_OFF");
+ eventMatcher->set_id(StringToId("SCREEN_ON_OR_OFF"));
AtomMatcher_Combination* combination = eventMatcher->mutable_combination();
combination->set_operation(LogicalOperation::OR);
- combination->add_matcher("SCREEN_IS_ON");
- combination->add_matcher("SCREEN_IS_OFF");
+ combination->add_matcher(StringToId("SCREEN_IS_ON"));
+ combination->add_matcher(StringToId("SCREEN_IS_OFF"));
CountMetric* metric = config.add_count_metric();
- metric->set_name("3");
- metric->set_what("SCREEN_IS_ON");
+ metric->set_id(3);
+ metric->set_what(StringToId("SCREEN_IS_ON"));
metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
metric->mutable_dimensions()->set_field(2 /*SCREEN_STATE_CHANGE*/);
metric->mutable_dimensions()->add_child()->set_field(1);
+ config.add_no_report_metric(3);
+
auto alert = config.add_alert();
- alert->set_name("3");
- alert->set_metric_name("3");
+ alert->set_id(3);
+ alert->set_metric_id(3);
alert->set_number_of_buckets(10);
alert->set_refractory_period_secs(100);
alert->set_trigger_if_sum_gt(100);
@@ -94,10 +97,10 @@
StatsdConfig buildCircleMatchers() {
StatsdConfig config;
- config.set_name("12345");
+ config.set_id(12345);
AtomMatcher* eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_IS_ON");
+ eventMatcher->set_id(StringToId("SCREEN_IS_ON"));
SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/);
@@ -107,34 +110,34 @@
2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_ON_OR_OFF");
+ eventMatcher->set_id(StringToId("SCREEN_ON_OR_OFF"));
AtomMatcher_Combination* combination = eventMatcher->mutable_combination();
combination->set_operation(LogicalOperation::OR);
- combination->add_matcher("SCREEN_IS_ON");
+ combination->add_matcher(StringToId("SCREEN_IS_ON"));
// Circle dependency
- combination->add_matcher("SCREEN_ON_OR_OFF");
+ combination->add_matcher(StringToId("SCREEN_ON_OR_OFF"));
return config;
}
StatsdConfig buildAlertWithUnknownMetric() {
StatsdConfig config;
- config.set_name("12345");
+ config.set_id(12345);
AtomMatcher* eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_IS_ON");
+ eventMatcher->set_id(StringToId("SCREEN_IS_ON"));
CountMetric* metric = config.add_count_metric();
- metric->set_name("3");
- metric->set_what("SCREEN_IS_ON");
+ metric->set_id(3);
+ metric->set_what(StringToId("SCREEN_IS_ON"));
metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
metric->mutable_dimensions()->set_field(2 /*SCREEN_STATE_CHANGE*/);
metric->mutable_dimensions()->add_child()->set_field(1);
auto alert = config.add_alert();
- alert->set_name("3");
- alert->set_metric_name("2");
+ alert->set_id(3);
+ alert->set_metric_id(2);
alert->set_number_of_buckets(10);
alert->set_refractory_period_secs(100);
alert->set_trigger_if_sum_gt(100);
@@ -143,10 +146,10 @@
StatsdConfig buildMissingMatchers() {
StatsdConfig config;
- config.set_name("12345");
+ config.set_id(12345);
AtomMatcher* eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_IS_ON");
+ eventMatcher->set_id(StringToId("SCREEN_IS_ON"));
SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/);
@@ -156,29 +159,29 @@
2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_ON_OR_OFF");
+ eventMatcher->set_id(StringToId("SCREEN_ON_OR_OFF"));
AtomMatcher_Combination* combination = eventMatcher->mutable_combination();
combination->set_operation(LogicalOperation::OR);
- combination->add_matcher("SCREEN_IS_ON");
+ combination->add_matcher(StringToId("SCREEN_IS_ON"));
// undefined matcher
- combination->add_matcher("ABC");
+ combination->add_matcher(StringToId("ABC"));
return config;
}
StatsdConfig buildMissingPredicate() {
StatsdConfig config;
- config.set_name("12345");
+ config.set_id(12345);
CountMetric* metric = config.add_count_metric();
- metric->set_name("3");
- metric->set_what("SCREEN_EVENT");
+ metric->set_id(3);
+ metric->set_what(StringToId("SCREEN_EVENT"));
metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
- metric->set_condition("SOME_CONDITION");
+ metric->set_condition(StringToId("SOME_CONDITION"));
AtomMatcher* eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_EVENT");
+ eventMatcher->set_id(StringToId("SCREEN_EVENT"));
SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(2);
@@ -188,37 +191,37 @@
StatsdConfig buildDimensionMetricsWithMultiTags() {
StatsdConfig config;
- config.set_name("12345");
+ config.set_id(12345);
AtomMatcher* eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("BATTERY_VERY_LOW");
+ eventMatcher->set_id(StringToId("BATTERY_VERY_LOW"));
SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(2);
eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("BATTERY_VERY_VERY_LOW");
+ eventMatcher->set_id(StringToId("BATTERY_VERY_VERY_LOW"));
simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(3);
eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("BATTERY_LOW");
+ eventMatcher->set_id(StringToId("BATTERY_LOW"));
AtomMatcher_Combination* combination = eventMatcher->mutable_combination();
combination->set_operation(LogicalOperation::OR);
- combination->add_matcher("BATTERY_VERY_LOW");
- combination->add_matcher("BATTERY_VERY_VERY_LOW");
+ combination->add_matcher(StringToId("BATTERY_VERY_LOW"));
+ combination->add_matcher(StringToId("BATTERY_VERY_VERY_LOW"));
// Count process state changes, slice by uid, while SCREEN_IS_OFF
CountMetric* metric = config.add_count_metric();
- metric->set_name("3");
- metric->set_what("BATTERY_LOW");
+ metric->set_id(3);
+ metric->set_what(StringToId("BATTERY_LOW"));
metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
// This case is interesting. We want to dimension across two atoms.
metric->mutable_dimensions()->add_child()->set_field(1);
auto alert = config.add_alert();
- alert->set_name("3");
- alert->set_metric_name("3");
+ alert->set_id(103);
+ alert->set_metric_id(3);
alert->set_number_of_buckets(10);
alert->set_refractory_period_secs(100);
alert->set_trigger_if_sum_gt(100);
@@ -227,10 +230,10 @@
StatsdConfig buildCirclePredicates() {
StatsdConfig config;
- config.set_name("12345");
+ config.set_id(12345);
AtomMatcher* eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_IS_ON");
+ eventMatcher->set_id(StringToId("SCREEN_IS_ON"));
SimpleAtomMatcher* simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/);
@@ -240,7 +243,7 @@
2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
eventMatcher = config.add_atom_matcher();
- eventMatcher->set_name("SCREEN_IS_OFF");
+ eventMatcher->set_id(StringToId("SCREEN_IS_OFF"));
simpleAtomMatcher = eventMatcher->mutable_simple_atom_matcher();
simpleAtomMatcher->set_atom_id(2 /*SCREEN_STATE_CHANGE*/);
@@ -250,18 +253,18 @@
1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
auto condition = config.add_predicate();
- condition->set_name("SCREEN_IS_ON");
+ condition->set_id(StringToId("SCREEN_IS_ON"));
SimplePredicate* simplePredicate = condition->mutable_simple_predicate();
- simplePredicate->set_start("SCREEN_IS_ON");
- simplePredicate->set_stop("SCREEN_IS_OFF");
+ simplePredicate->set_start(StringToId("SCREEN_IS_ON"));
+ simplePredicate->set_stop(StringToId("SCREEN_IS_OFF"));
condition = config.add_predicate();
- condition->set_name("SCREEN_IS_EITHER_ON_OFF");
+ condition->set_id(StringToId("SCREEN_IS_EITHER_ON_OFF"));
Predicate_Combination* combination = condition->mutable_combination();
combination->set_operation(LogicalOperation::OR);
- combination->add_predicate("SCREEN_IS_ON");
- combination->add_predicate("SCREEN_IS_EITHER_ON_OFF");
+ combination->add_predicate(StringToId("SCREEN_IS_ON"));
+ combination->add_predicate(StringToId("SCREEN_IS_EITHER_ON_OFF"));
return config;
}
@@ -277,12 +280,15 @@
unordered_map<int, std::vector<int>> conditionToMetricMap;
unordered_map<int, std::vector<int>> trackerToMetricMap;
unordered_map<int, std::vector<int>> trackerToConditionMap;
+ std::set<int64_t> noReportMetricIds;
EXPECT_TRUE(initStatsdConfig(kConfigKey, config, uidMap, timeBaseSec, allTagIds, allAtomMatchers,
allConditionTrackers, allMetricProducers, allAnomalyTrackers,
- conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
+ conditionToMetricMap, trackerToMetricMap, trackerToConditionMap,
+ noReportMetricIds));
EXPECT_EQ(1u, allMetricProducers.size());
EXPECT_EQ(1u, allAnomalyTrackers.size());
+ EXPECT_EQ(1u, noReportMetricIds.size());
}
TEST(MetricsManagerTest, TestDimensionMetricsWithMultiTags) {
@@ -296,10 +302,12 @@
unordered_map<int, std::vector<int>> conditionToMetricMap;
unordered_map<int, std::vector<int>> trackerToMetricMap;
unordered_map<int, std::vector<int>> trackerToConditionMap;
+ std::set<int64_t> noReportMetricIds;
EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, timeBaseSec, allTagIds, allAtomMatchers,
allConditionTrackers, allMetricProducers, allAnomalyTrackers,
- conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
+ conditionToMetricMap, trackerToMetricMap, trackerToConditionMap,
+ noReportMetricIds));
}
TEST(MetricsManagerTest, TestCircleLogMatcherDependency) {
@@ -313,10 +321,12 @@
unordered_map<int, std::vector<int>> conditionToMetricMap;
unordered_map<int, std::vector<int>> trackerToMetricMap;
unordered_map<int, std::vector<int>> trackerToConditionMap;
+ std::set<int64_t> noReportMetricIds;
EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, timeBaseSec, allTagIds, allAtomMatchers,
allConditionTrackers, allMetricProducers, allAnomalyTrackers,
- conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
+ conditionToMetricMap, trackerToMetricMap, trackerToConditionMap,
+ noReportMetricIds));
}
TEST(MetricsManagerTest, TestMissingMatchers) {
@@ -330,9 +340,11 @@
unordered_map<int, std::vector<int>> conditionToMetricMap;
unordered_map<int, std::vector<int>> trackerToMetricMap;
unordered_map<int, std::vector<int>> trackerToConditionMap;
+ std::set<int64_t> noReportMetricIds;
EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, timeBaseSec, allTagIds, allAtomMatchers,
allConditionTrackers, allMetricProducers, allAnomalyTrackers,
- conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
+ conditionToMetricMap, trackerToMetricMap, trackerToConditionMap,
+ noReportMetricIds));
}
TEST(MetricsManagerTest, TestMissingPredicate) {
@@ -346,9 +358,11 @@
unordered_map<int, std::vector<int>> conditionToMetricMap;
unordered_map<int, std::vector<int>> trackerToMetricMap;
unordered_map<int, std::vector<int>> trackerToConditionMap;
+ std::set<int64_t> noReportMetricIds;
EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, timeBaseSec, allTagIds, allAtomMatchers,
allConditionTrackers, allMetricProducers, allAnomalyTrackers,
- conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
+ conditionToMetricMap, trackerToMetricMap, trackerToConditionMap,
+ noReportMetricIds));
}
TEST(MetricsManagerTest, TestCirclePredicateDependency) {
@@ -362,10 +376,12 @@
unordered_map<int, std::vector<int>> conditionToMetricMap;
unordered_map<int, std::vector<int>> trackerToMetricMap;
unordered_map<int, std::vector<int>> trackerToConditionMap;
+ std::set<int64_t> noReportMetricIds;
EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, timeBaseSec, allTagIds, allAtomMatchers,
allConditionTrackers, allMetricProducers, allAnomalyTrackers,
- conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
+ conditionToMetricMap, trackerToMetricMap, trackerToConditionMap,
+ noReportMetricIds));
}
TEST(MetricsManagerTest, testAlertWithUnknownMetric) {
@@ -379,10 +395,12 @@
unordered_map<int, std::vector<int>> conditionToMetricMap;
unordered_map<int, std::vector<int>> trackerToMetricMap;
unordered_map<int, std::vector<int>> trackerToConditionMap;
+ std::set<int64_t> noReportMetricIds;
EXPECT_FALSE(initStatsdConfig(kConfigKey, config, uidMap, timeBaseSec, allTagIds, allAtomMatchers,
allConditionTrackers, allMetricProducers, allAnomalyTrackers,
- conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
+ conditionToMetricMap, trackerToMetricMap, trackerToConditionMap,
+ noReportMetricIds));
}
#else
diff --git a/cmds/statsd/tests/StatsLogProcessor_test.cpp b/cmds/statsd/tests/StatsLogProcessor_test.cpp
index a27bed5..5d053e2 100644
--- a/cmds/statsd/tests/StatsLogProcessor_test.cpp
+++ b/cmds/statsd/tests/StatsLogProcessor_test.cpp
@@ -41,7 +41,7 @@
*/
class MockMetricsManager : public MetricsManager {
public:
- MockMetricsManager() : MetricsManager(ConfigKey(1, "key"), StatsdConfig(), 1000, new UidMap()) {
+ MockMetricsManager() : MetricsManager(ConfigKey(1, 12345), StatsdConfig(), 1000, new UidMap()) {
}
MOCK_METHOD0(byteSize, size_t());
@@ -56,7 +56,7 @@
MockMetricsManager mockMetricsManager;
- ConfigKey key(100, "key");
+ ConfigKey key(100, 12345);
// Expect only the first flush to trigger a check for byte size since the last two are
// rate-limited.
EXPECT_CALL(mockMetricsManager, byteSize()).Times(1);
@@ -74,7 +74,7 @@
MockMetricsManager mockMetricsManager;
- ConfigKey key(100, "key");
+ ConfigKey key(100, 12345);
EXPECT_CALL(mockMetricsManager, byteSize())
.Times(2)
.WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * .95)));
@@ -98,7 +98,7 @@
MockMetricsManager mockMetricsManager;
- ConfigKey key(100, "key");
+ ConfigKey key(100, 12345);
EXPECT_CALL(mockMetricsManager, byteSize())
.Times(1)
.WillRepeatedly(Return(int(StatsdStats::kMaxMetricsBytesPerConfig * 1.2)));
diff --git a/cmds/statsd/tests/UidMap_test.cpp b/cmds/statsd/tests/UidMap_test.cpp
index 8a394f7..945af27 100644
--- a/cmds/statsd/tests/UidMap_test.cpp
+++ b/cmds/statsd/tests/UidMap_test.cpp
@@ -18,6 +18,7 @@
#include "guardrail/StatsdStats.h"
#include "logd/LogEvent.h"
#include "statslog.h"
+#include "statsd_test_util.h"
#include <gtest/gtest.h>
@@ -156,8 +157,8 @@
TEST(UidMapTest, TestClearingOutput) {
UidMap m;
- ConfigKey config1(1, "config1");
- ConfigKey config2(1, "config2");
+ ConfigKey config1(1, StringToId("config1"));
+ ConfigKey config2(1, StringToId("config2"));
m.OnConfigUpdated(config1);
@@ -211,7 +212,7 @@
TEST(UidMapTest, TestMemoryComputed) {
UidMap m;
- ConfigKey config1(1, "config1");
+ ConfigKey config1(1, StringToId("config1"));
m.OnConfigUpdated(config1);
size_t startBytes = m.mBytesUsed;
@@ -241,7 +242,7 @@
UidMap m;
string buf;
- ConfigKey config1(1, "config1");
+ ConfigKey config1(1, StringToId("config1"));
m.OnConfigUpdated(config1);
size_t startBytes = m.mBytesUsed;
diff --git a/cmds/statsd/tests/anomaly/AnomalyTracker_test.cpp b/cmds/statsd/tests/anomaly/AnomalyTracker_test.cpp
index da872ad..751180d 100644
--- a/cmds/statsd/tests/anomaly/AnomalyTracker_test.cpp
+++ b/cmds/statsd/tests/anomaly/AnomalyTracker_test.cpp
@@ -31,7 +31,7 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
HashableDimensionKey getMockDimensionKey(int key, string value) {
DimensionsValue dimensionsValue;
diff --git a/cmds/statsd/tests/condition/SimpleConditionTracker_test.cpp b/cmds/statsd/tests/condition/SimpleConditionTracker_test.cpp
index 705804f..819f2be 100644
--- a/cmds/statsd/tests/condition/SimpleConditionTracker_test.cpp
+++ b/cmds/statsd/tests/condition/SimpleConditionTracker_test.cpp
@@ -11,7 +11,9 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
+
#include "src/condition/SimpleConditionTracker.h"
+#include "tests/statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -29,7 +31,7 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
const int ATTRIBUTION_NODE_FIELD_ID = 1;
const int ATTRIBUTION_UID_FIELD_ID = 1;
@@ -38,9 +40,9 @@
SimplePredicate getWakeLockHeldCondition(bool countNesting, bool defaultFalse,
bool outputSlicedUid, Position position) {
SimplePredicate simplePredicate;
- simplePredicate.set_start("WAKE_LOCK_ACQUIRE");
- simplePredicate.set_stop("WAKE_LOCK_RELEASE");
- simplePredicate.set_stop_all("RELEASE_ALL");
+ simplePredicate.set_start(StringToId("WAKE_LOCK_ACQUIRE"));
+ simplePredicate.set_stop(StringToId("WAKE_LOCK_RELEASE"));
+ simplePredicate.set_stop_all(StringToId("RELEASE_ALL"));
if (outputSlicedUid) {
simplePredicate.mutable_dimensions()->set_field(TAG_ID);
simplePredicate.mutable_dimensions()->add_child()->set_field(ATTRIBUTION_NODE_FIELD_ID);
@@ -73,10 +75,10 @@
event->init();
}
-std::map<string, std::vector<HashableDimensionKey>> getWakeLockQueryKey(
+std::map<int64_t, std::vector<HashableDimensionKey>> getWakeLockQueryKey(
const Position position,
const std::vector<int> &uids, const string& conditionName) {
- std::map<string, std::vector<HashableDimensionKey>> outputKeyMap;
+ std::map<int64_t, std::vector<HashableDimensionKey>> outputKeyMap;
std::vector<int> uid_indexes;
switch(position) {
case Position::FIRST:
@@ -96,28 +98,29 @@
for (const int idx : uid_indexes) {
DimensionsValue dimensionsValue;
dimensionsValue.set_field(TAG_ID);
- dimensionsValue.mutable_value_tuple()->add_dimensions_value()->set_field(ATTRIBUTION_NODE_FIELD_ID);
+ dimensionsValue.mutable_value_tuple()->add_dimensions_value()->set_field(
+ ATTRIBUTION_NODE_FIELD_ID);
dimensionsValue.mutable_value_tuple()->mutable_dimensions_value(0)
->mutable_value_tuple()->add_dimensions_value()->set_field(ATTRIBUTION_NODE_FIELD_ID);
dimensionsValue.mutable_value_tuple()->mutable_dimensions_value(0)
->mutable_value_tuple()->mutable_dimensions_value(0)->set_value_int(uids[idx]);
- outputKeyMap[conditionName].push_back(HashableDimensionKey(dimensionsValue));
+ outputKeyMap[StringToId(conditionName)].push_back(HashableDimensionKey(dimensionsValue));
}
return outputKeyMap;
}
TEST(SimpleConditionTrackerTest, TestNonSlicedCondition) {
SimplePredicate simplePredicate;
- simplePredicate.set_start("SCREEN_TURNED_ON");
- simplePredicate.set_stop("SCREEN_TURNED_OFF");
+ simplePredicate.set_start(StringToId("SCREEN_TURNED_ON"));
+ simplePredicate.set_stop(StringToId("SCREEN_TURNED_OFF"));
simplePredicate.set_count_nesting(false);
simplePredicate.set_initial_value(SimplePredicate_InitialValue_UNKNOWN);
- unordered_map<string, int> trackerNameIndexMap;
- trackerNameIndexMap["SCREEN_TURNED_ON"] = 0;
- trackerNameIndexMap["SCREEN_TURNED_OFF"] = 1;
+ unordered_map<int64_t, int> trackerNameIndexMap;
+ trackerNameIndexMap[StringToId("SCREEN_TURNED_ON")] = 0;
+ trackerNameIndexMap[StringToId("SCREEN_TURNED_OFF")] = 1;
- SimpleConditionTracker conditionTracker(kConfigKey, "SCREEN_IS_ON", 0 /*tracker index*/,
+ SimpleConditionTracker conditionTracker(kConfigKey, StringToId("SCREEN_IS_ON"), 0 /*tracker index*/,
simplePredicate, trackerNameIndexMap);
LogEvent event(1 /*tagId*/, 0 /*timestamp*/);
@@ -191,15 +194,15 @@
TEST(SimpleConditionTrackerTest, TestNonSlicedConditionNestCounting) {
SimplePredicate simplePredicate;
- simplePredicate.set_start("SCREEN_TURNED_ON");
- simplePredicate.set_stop("SCREEN_TURNED_OFF");
+ simplePredicate.set_start(StringToId("SCREEN_TURNED_ON"));
+ simplePredicate.set_stop(StringToId("SCREEN_TURNED_OFF"));
simplePredicate.set_count_nesting(true);
- unordered_map<string, int> trackerNameIndexMap;
- trackerNameIndexMap["SCREEN_TURNED_ON"] = 0;
- trackerNameIndexMap["SCREEN_TURNED_OFF"] = 1;
+ unordered_map<int64_t, int> trackerNameIndexMap;
+ trackerNameIndexMap[StringToId("SCREEN_TURNED_ON")] = 0;
+ trackerNameIndexMap[StringToId("SCREEN_TURNED_OFF")] = 1;
- SimpleConditionTracker conditionTracker(kConfigKey, "SCREEN_IS_ON",
+ SimpleConditionTracker conditionTracker(kConfigKey, StringToId("SCREEN_IS_ON"),
0 /*condition tracker index*/, simplePredicate,
trackerNameIndexMap);
@@ -267,12 +270,12 @@
position);
string conditionName = "WL_HELD_BY_UID2";
- unordered_map<string, int> trackerNameIndexMap;
- trackerNameIndexMap["WAKE_LOCK_ACQUIRE"] = 0;
- trackerNameIndexMap["WAKE_LOCK_RELEASE"] = 1;
- trackerNameIndexMap["RELEASE_ALL"] = 2;
+ unordered_map<int64_t, int> trackerNameIndexMap;
+ trackerNameIndexMap[StringToId("WAKE_LOCK_ACQUIRE")] = 0;
+ trackerNameIndexMap[StringToId("WAKE_LOCK_RELEASE")] = 1;
+ trackerNameIndexMap[StringToId("RELEASE_ALL")] = 2;
- SimpleConditionTracker conditionTracker(kConfigKey, conditionName,
+ SimpleConditionTracker conditionTracker(kConfigKey, StringToId(conditionName),
0 /*condition tracker index*/, simplePredicate,
trackerNameIndexMap);
std::vector<int> uids = {111, 222, 333};
@@ -371,12 +374,12 @@
Position::ANY /* position */);
string conditionName = "WL_HELD";
- unordered_map<string, int> trackerNameIndexMap;
- trackerNameIndexMap["WAKE_LOCK_ACQUIRE"] = 0;
- trackerNameIndexMap["WAKE_LOCK_RELEASE"] = 1;
- trackerNameIndexMap["RELEASE_ALL"] = 2;
+ unordered_map<int64_t, int> trackerNameIndexMap;
+ trackerNameIndexMap[StringToId("WAKE_LOCK_ACQUIRE")] = 0;
+ trackerNameIndexMap[StringToId("WAKE_LOCK_RELEASE")] = 1;
+ trackerNameIndexMap[StringToId("RELEASE_ALL")] = 2;
- SimpleConditionTracker conditionTracker(kConfigKey, conditionName,
+ SimpleConditionTracker conditionTracker(kConfigKey, StringToId(conditionName),
0 /*condition tracker index*/, simplePredicate,
trackerNameIndexMap);
@@ -461,12 +464,12 @@
position);
string conditionName = "WL_HELD_BY_UID3";
- unordered_map<string, int> trackerNameIndexMap;
- trackerNameIndexMap["WAKE_LOCK_ACQUIRE"] = 0;
- trackerNameIndexMap["WAKE_LOCK_RELEASE"] = 1;
- trackerNameIndexMap["RELEASE_ALL"] = 2;
+ unordered_map<int64_t, int> trackerNameIndexMap;
+ trackerNameIndexMap[StringToId("WAKE_LOCK_ACQUIRE")] = 0;
+ trackerNameIndexMap[StringToId("WAKE_LOCK_RELEASE")] = 1;
+ trackerNameIndexMap[StringToId("RELEASE_ALL")] = 2;
- SimpleConditionTracker conditionTracker(kConfigKey, conditionName,
+ SimpleConditionTracker conditionTracker(kConfigKey, StringToId(conditionName),
0 /*condition tracker index*/, simplePredicate,
trackerNameIndexMap);
diff --git a/cmds/statsd/tests/e2e/MetricConditionLink_e2e_test.cpp b/cmds/statsd/tests/e2e/MetricConditionLink_e2e_test.cpp
index c747016..b56b817 100644
--- a/cmds/statsd/tests/e2e/MetricConditionLink_e2e_test.cpp
+++ b/cmds/statsd/tests/e2e/MetricConditionLink_e2e_test.cpp
@@ -55,16 +55,16 @@
*config.add_predicate() = isInBackgroundPredicate;
auto combinationPredicate = config.add_predicate();
- combinationPredicate->set_name("combinationPredicate");
+ combinationPredicate->set_id(StringToId("combinationPredicate"));
combinationPredicate->mutable_combination()->set_operation(LogicalOperation::AND);
addPredicateToPredicateCombination(screenIsOffPredicate, combinationPredicate);
addPredicateToPredicateCombination(isSyncingPredicate, combinationPredicate);
addPredicateToPredicateCombination(isInBackgroundPredicate, combinationPredicate);
auto countMetric = config.add_count_metric();
- countMetric->set_name("AppCrashes");
- countMetric->set_what(appCrashMatcher.name());
- countMetric->set_condition(combinationPredicate->name());
+ countMetric->set_id(StringToId("AppCrashes"));
+ countMetric->set_what(appCrashMatcher.id());
+ countMetric->set_condition(combinationPredicate->id());
// The metric is dimensioning by uid only.
*countMetric->mutable_dimensions() =
CreateDimensions(android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED, {1});
@@ -72,7 +72,7 @@
// Links between crash atom and condition of app is in syncing.
auto links = countMetric->add_links();
- links->set_condition(isSyncingPredicate.name());
+ links->set_condition(isSyncingPredicate.id());
auto dimensionWhat = links->mutable_dimensions_in_what();
dimensionWhat->set_field(android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
dimensionWhat->add_child()->set_field(1); // uid field.
@@ -82,7 +82,7 @@
// Links between crash atom and condition of app is in background.
links = countMetric->add_links();
- links->set_condition(isInBackgroundPredicate.name());
+ links->set_condition(isInBackgroundPredicate.id());
dimensionWhat = links->mutable_dimensions_in_what();
dimensionWhat->set_field(android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
dimensionWhat->add_child()->set_field(1); // uid field.
diff --git a/cmds/statsd/tests/e2e/WakelockDuration_e2e_test.cpp b/cmds/statsd/tests/e2e/WakelockDuration_e2e_test.cpp
index 8d7b2d5..ecdb002 100644
--- a/cmds/statsd/tests/e2e/WakelockDuration_e2e_test.cpp
+++ b/cmds/statsd/tests/e2e/WakelockDuration_e2e_test.cpp
@@ -43,9 +43,9 @@
*config.add_predicate() = holdingWakelockPredicate;
auto durationMetric = config.add_duration_metric();
- durationMetric->set_name("WakelockDuration");
- durationMetric->set_what(holdingWakelockPredicate.name());
- durationMetric->set_condition(screenIsOffPredicate.name());
+ durationMetric->set_id(StringToId("WakelockDuration"));
+ durationMetric->set_what(holdingWakelockPredicate.id());
+ durationMetric->set_condition(screenIsOffPredicate.id());
durationMetric->set_aggregation_type(aggregationType);
// The metric is dimensioning by first attribution node and only by uid.
*durationMetric->mutable_dimensions() =
diff --git a/cmds/statsd/tests/guardrail/StatsdStats_test.cpp b/cmds/statsd/tests/guardrail/StatsdStats_test.cpp
index 7658044..a134300 100644
--- a/cmds/statsd/tests/guardrail/StatsdStats_test.cpp
+++ b/cmds/statsd/tests/guardrail/StatsdStats_test.cpp
@@ -14,6 +14,7 @@
#include "src/guardrail/StatsdStats.h"
#include "statslog.h"
+#include "tests/statsd_test_util.h"
#include <gtest/gtest.h>
#include <vector>
@@ -28,8 +29,7 @@
TEST(StatsdStatsTest, TestValidConfigAdd) {
StatsdStats stats;
- string name = "StatsdTest";
- ConfigKey key(0, name);
+ ConfigKey key(0, 12345);
const int metricsCount = 10;
const int conditionsCount = 20;
const int matchersCount = 30;
@@ -45,7 +45,7 @@
EXPECT_EQ(1, report.config_stats_size());
const auto& configReport = report.config_stats(0);
EXPECT_EQ(0, configReport.uid());
- EXPECT_EQ(name, configReport.name());
+ EXPECT_EQ(12345, configReport.id());
EXPECT_EQ(metricsCount, configReport.metric_count());
EXPECT_EQ(conditionsCount, configReport.condition_count());
EXPECT_EQ(matchersCount, configReport.matcher_count());
@@ -56,8 +56,7 @@
TEST(StatsdStatsTest, TestInvalidConfigAdd) {
StatsdStats stats;
- string name = "StatsdTest";
- ConfigKey key(0, name);
+ ConfigKey key(0, 12345);
const int metricsCount = 10;
const int conditionsCount = 20;
const int matchersCount = 30;
@@ -78,8 +77,7 @@
TEST(StatsdStatsTest, TestConfigRemove) {
StatsdStats stats;
- string name = "StatsdTest";
- ConfigKey key(0, name);
+ ConfigKey key(0, 12345);
const int metricsCount = 10;
const int conditionsCount = 20;
const int matchersCount = 30;
@@ -105,22 +103,22 @@
TEST(StatsdStatsTest, TestSubStats) {
StatsdStats stats;
- ConfigKey key(0, "test");
+ ConfigKey key(0, 12345);
stats.noteConfigReceived(key, 2, 3, 4, 5, true);
- stats.noteMatcherMatched(key, "matcher1");
- stats.noteMatcherMatched(key, "matcher1");
- stats.noteMatcherMatched(key, "matcher2");
+ stats.noteMatcherMatched(key, StringToId("matcher1"));
+ stats.noteMatcherMatched(key, StringToId("matcher1"));
+ stats.noteMatcherMatched(key, StringToId("matcher2"));
- stats.noteConditionDimensionSize(key, "condition1", 250);
- stats.noteConditionDimensionSize(key, "condition1", 240);
+ stats.noteConditionDimensionSize(key, StringToId("condition1"), 250);
+ stats.noteConditionDimensionSize(key, StringToId("condition1"), 240);
- stats.noteMetricDimensionSize(key, "metric1", 201);
- stats.noteMetricDimensionSize(key, "metric1", 202);
+ stats.noteMetricDimensionSize(key, StringToId("metric1"), 201);
+ stats.noteMetricDimensionSize(key, StringToId("metric1"), 202);
- stats.noteAnomalyDeclared(key, "alert1");
- stats.noteAnomalyDeclared(key, "alert1");
- stats.noteAnomalyDeclared(key, "alert2");
+ stats.noteAnomalyDeclared(key, StringToId("alert1"));
+ stats.noteAnomalyDeclared(key, StringToId("alert1"));
+ stats.noteAnomalyDeclared(key, StringToId("alert2"));
// broadcast-> 2
stats.noteBroadcastSent(key);
@@ -147,39 +145,39 @@
EXPECT_EQ(2, configReport.matcher_stats_size());
// matcher1 is the first in the list
- if (!configReport.matcher_stats(0).name().compare("matcher1")) {
+ if (configReport.matcher_stats(0).id() == StringToId("matcher1")) {
EXPECT_EQ(2, configReport.matcher_stats(0).matched_times());
EXPECT_EQ(1, configReport.matcher_stats(1).matched_times());
- EXPECT_EQ("matcher2", configReport.matcher_stats(1).name());
+ EXPECT_EQ(StringToId("matcher2"), configReport.matcher_stats(1).id());
} else {
// matcher1 is the second in the list.
EXPECT_EQ(1, configReport.matcher_stats(0).matched_times());
- EXPECT_EQ("matcher2", configReport.matcher_stats(0).name());
+ EXPECT_EQ(StringToId("matcher2"), configReport.matcher_stats(0).id());
EXPECT_EQ(2, configReport.matcher_stats(1).matched_times());
- EXPECT_EQ("matcher1", configReport.matcher_stats(1).name());
+ EXPECT_EQ(StringToId("matcher1"), configReport.matcher_stats(1).id());
}
EXPECT_EQ(2, configReport.alert_stats_size());
- bool alert1first = !configReport.alert_stats(0).name().compare("alert1");
- EXPECT_EQ("alert1", configReport.alert_stats(alert1first ? 0 : 1).name());
+ bool alert1first = configReport.alert_stats(0).id() == StringToId("alert1");
+ EXPECT_EQ(StringToId("alert1"), configReport.alert_stats(alert1first ? 0 : 1).id());
EXPECT_EQ(2, configReport.alert_stats(alert1first ? 0 : 1).alerted_times());
- EXPECT_EQ("alert2", configReport.alert_stats(alert1first ? 1 : 0).name());
+ EXPECT_EQ(StringToId("alert2"), configReport.alert_stats(alert1first ? 1 : 0).id());
EXPECT_EQ(1, configReport.alert_stats(alert1first ? 1 : 0).alerted_times());
EXPECT_EQ(1, configReport.condition_stats_size());
- EXPECT_EQ("condition1", configReport.condition_stats(0).name());
+ EXPECT_EQ(StringToId("condition1"), configReport.condition_stats(0).id());
EXPECT_EQ(250, configReport.condition_stats(0).max_tuple_counts());
EXPECT_EQ(1, configReport.metric_stats_size());
- EXPECT_EQ("metric1", configReport.metric_stats(0).name());
+ EXPECT_EQ(StringToId("metric1"), configReport.metric_stats(0).id());
EXPECT_EQ(202, configReport.metric_stats(0).max_tuple_counts());
// after resetting the stats, some new events come
- stats.noteMatcherMatched(key, "matcher99");
- stats.noteConditionDimensionSize(key, "condition99", 300);
- stats.noteMetricDimensionSize(key, "metric99", 270);
- stats.noteAnomalyDeclared(key, "alert99");
+ stats.noteMatcherMatched(key, StringToId("matcher99"));
+ stats.noteConditionDimensionSize(key, StringToId("condition99"), 300);
+ stats.noteMetricDimensionSize(key, StringToId("metric99tion99"), 270);
+ stats.noteAnomalyDeclared(key, StringToId("alert99"));
// now the config stats should only contain the stats about the new event.
stats.dumpStats(&output, false);
@@ -188,19 +186,19 @@
EXPECT_EQ(1, report.config_stats_size());
const auto& configReport2 = report.config_stats(0);
EXPECT_EQ(1, configReport2.matcher_stats_size());
- EXPECT_EQ("matcher99", configReport2.matcher_stats(0).name());
+ EXPECT_EQ(StringToId("matcher99"), configReport2.matcher_stats(0).id());
EXPECT_EQ(1, configReport2.matcher_stats(0).matched_times());
EXPECT_EQ(1, configReport2.condition_stats_size());
- EXPECT_EQ("condition99", configReport2.condition_stats(0).name());
+ EXPECT_EQ(StringToId("condition99"), configReport2.condition_stats(0).id());
EXPECT_EQ(300, configReport2.condition_stats(0).max_tuple_counts());
EXPECT_EQ(1, configReport2.metric_stats_size());
- EXPECT_EQ("metric99", configReport2.metric_stats(0).name());
+ EXPECT_EQ(StringToId("metric99tion99"), configReport2.metric_stats(0).id());
EXPECT_EQ(270, configReport2.metric_stats(0).max_tuple_counts());
EXPECT_EQ(1, configReport2.alert_stats_size());
- EXPECT_EQ("alert99", configReport2.alert_stats(0).name());
+ EXPECT_EQ(StringToId("alert99"), configReport2.alert_stats(0).id());
EXPECT_EQ(1, configReport2.alert_stats(0).alerted_times());
}
@@ -260,7 +258,7 @@
for (int i = 0; i < StatsdStats::kMaxTimestampCount; i++) {
timestamps.push_back(i);
}
- ConfigKey key(0, "test");
+ ConfigKey key(0, 12345);
stats.noteConfigReceived(key, 2, 3, 4, 5, true);
for (int i = 0; i < StatsdStats::kMaxTimestampCount; i++) {
diff --git a/cmds/statsd/tests/metrics/CountMetricProducer_test.cpp b/cmds/statsd/tests/metrics/CountMetricProducer_test.cpp
index 6e114a6..4c2d472 100644
--- a/cmds/statsd/tests/metrics/CountMetricProducer_test.cpp
+++ b/cmds/statsd/tests/metrics/CountMetricProducer_test.cpp
@@ -15,6 +15,7 @@
#include "src/metrics/CountMetricProducer.h"
#include "src/dimension.h"
#include "metrics_test_helper.h"
+#include "tests/statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -33,7 +34,7 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
TEST(CountMetricProducerTest, TestNonDimensionalEvents) {
int64_t bucketStartTimeNs = 10000000000;
@@ -43,7 +44,7 @@
int tagId = 1;
CountMetric metric;
- metric.set_name("1");
+ metric.set_id(1);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
LogEvent event1(tagId, bucketStartTimeNs + 1);
@@ -100,9 +101,9 @@
int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
CountMetric metric;
- metric.set_name("1");
+ metric.set_id(1);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
- metric.set_condition("SCREEN_ON");
+ metric.set_condition(StringToId("SCREEN_ON"));
LogEvent event1(1, bucketStartTimeNs + 1);
LogEvent event2(1, bucketStartTimeNs + 10);
@@ -142,11 +143,11 @@
int conditionTagId = 2;
CountMetric metric;
- metric.set_name("1");
+ metric.set_id(1);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
- metric.set_condition("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON");
+ metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON"));
MetricConditionLink* link = metric.add_links();
- link->set_condition("APP_IN_BACKGROUND_PER_UID");
+ link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID"));
*link->mutable_dimensions_in_what() = buildSimpleAtomFieldMatcher(tagId, 1);
*link->mutable_dimensions_in_condition() = buildSimpleAtomFieldMatcher(conditionTagId, 2);
@@ -154,13 +155,15 @@
event1.write("111"); // uid
event1.init();
ConditionKey key1;
- key1["APP_IN_BACKGROUND_PER_UID"] = {getMockedDimensionKey(conditionTagId, 2, "111")};
+ key1[StringToId("APP_IN_BACKGROUND_PER_UID")] =
+ {getMockedDimensionKey(conditionTagId, 2, "111")};
LogEvent event2(tagId, bucketStartTimeNs + 10);
event2.write("222"); // uid
event2.init();
ConditionKey key2;
- key2["APP_IN_BACKGROUND_PER_UID"] = {getMockedDimensionKey(conditionTagId, 2, "222")};
+ key2[StringToId("APP_IN_BACKGROUND_PER_UID")] =
+ {getMockedDimensionKey(conditionTagId, 2, "222")};
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
EXPECT_CALL(*wizard, query(_, key1)).WillOnce(Return(ConditionState::kFalse));
@@ -189,8 +192,8 @@
TEST(CountMetricProducerTest, TestAnomalyDetection) {
Alert alert;
- alert.set_name("alert");
- alert.set_metric_name("1");
+ alert.set_id(11);
+ alert.set_metric_id(1);
alert.set_trigger_if_sum_gt(2);
alert.set_number_of_buckets(2);
alert.set_refractory_period_secs(1);
@@ -201,7 +204,7 @@
int64_t bucket3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs;
CountMetric metric;
- metric.set_name("1");
+ metric.set_id(1);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
diff --git a/cmds/statsd/tests/metrics/DurationMetricProducer_test.cpp b/cmds/statsd/tests/metrics/DurationMetricProducer_test.cpp
index 8ee94c7..a4213de 100644
--- a/cmds/statsd/tests/metrics/DurationMetricProducer_test.cpp
+++ b/cmds/statsd/tests/metrics/DurationMetricProducer_test.cpp
@@ -36,7 +36,7 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
TEST(DurationMetricTrackerTest, TestNoCondition) {
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
@@ -44,7 +44,7 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
DurationMetric metric;
- metric.set_name("1");
+ metric.set_id(1);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
@@ -79,7 +79,7 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
DurationMetric metric;
- metric.set_name("1");
+ metric.set_id(1);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
diff --git a/cmds/statsd/tests/metrics/EventMetricProducer_test.cpp b/cmds/statsd/tests/metrics/EventMetricProducer_test.cpp
index 0ba1c2f..7171de9 100644
--- a/cmds/statsd/tests/metrics/EventMetricProducer_test.cpp
+++ b/cmds/statsd/tests/metrics/EventMetricProducer_test.cpp
@@ -15,6 +15,7 @@
#include "src/metrics/EventMetricProducer.h"
#include "src/dimension.h"
#include "metrics_test_helper.h"
+#include "tests/statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -33,7 +34,7 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
TEST(EventMetricProducerTest, TestNoCondition) {
uint64_t bucketStartTimeNs = 10000000000;
@@ -41,7 +42,7 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
EventMetric metric;
- metric.set_name("1");
+ metric.set_id(1);
LogEvent event1(1 /*tag id*/, bucketStartTimeNs + 1);
LogEvent event2(1 /*tag id*/, bucketStartTimeNs + 2);
@@ -64,8 +65,8 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
EventMetric metric;
- metric.set_name("1");
- metric.set_condition("SCREEN_ON");
+ metric.set_id(1);
+ metric.set_condition(StringToId("SCREEN_ON"));
LogEvent event1(1, bucketStartTimeNs + 1);
LogEvent event2(1, bucketStartTimeNs + 10);
@@ -93,10 +94,10 @@
int conditionTagId = 2;
EventMetric metric;
- metric.set_name("1");
- metric.set_condition("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON");
+ metric.set_id(1);
+ metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON"));
MetricConditionLink* link = metric.add_links();
- link->set_condition("APP_IN_BACKGROUND_PER_UID");
+ link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID"));
*link->mutable_dimensions_in_what() = buildSimpleAtomFieldMatcher(tagId, 1);
*link->mutable_dimensions_in_condition() = buildSimpleAtomFieldMatcher(conditionTagId, 2);
@@ -104,13 +105,13 @@
EXPECT_TRUE(event1.write("111"));
event1.init();
ConditionKey key1;
- key1["APP_IN_BACKGROUND_PER_UID"] = {getMockedDimensionKey(conditionTagId, 2, "111")};
+ key1[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "111")};
LogEvent event2(tagId, bucketStartTimeNs + 10);
EXPECT_TRUE(event2.write("222"));
event2.init();
ConditionKey key2;
- key2["APP_IN_BACKGROUND_PER_UID"] = {getMockedDimensionKey(conditionTagId, 2, "222")};
+ key2[StringToId("APP_IN_BACKGROUND_PER_UID")] = {getMockedDimensionKey(conditionTagId, 2, "222")};
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
EXPECT_CALL(*wizard, query(_, key1)).WillOnce(Return(ConditionState::kFalse));
diff --git a/cmds/statsd/tests/metrics/GaugeMetricProducer_test.cpp b/cmds/statsd/tests/metrics/GaugeMetricProducer_test.cpp
index 359851f..cde50c1 100644
--- a/cmds/statsd/tests/metrics/GaugeMetricProducer_test.cpp
+++ b/cmds/statsd/tests/metrics/GaugeMetricProducer_test.cpp
@@ -15,6 +15,7 @@
#include "src/metrics/GaugeMetricProducer.h"
#include "logd/LogEvent.h"
#include "metrics_test_helper.h"
+#include "tests/statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -34,9 +35,9 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
const int tagId = 1;
-const string metricName = "test_metric";
+const int64_t metricId = 123;
const int64_t bucketStartTimeNs = 10000000000;
const int64_t bucketSizeNs = 60 * 1000 * 1000 * 1000LL;
const int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs;
@@ -45,7 +46,7 @@
TEST(GaugeMetricProducerTest, TestNoCondition) {
GaugeMetric metric;
- metric.set_name(metricName);
+ metric.set_id(metricId);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
metric.mutable_gauge_fields_filter()->set_include_all(false);
auto gaugeFieldMatcher = metric.mutable_gauge_fields_filter()->mutable_fields();
@@ -119,12 +120,12 @@
TEST(GaugeMetricProducerTest, TestWithCondition) {
GaugeMetric metric;
- metric.set_name(metricName);
+ metric.set_id(metricId);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
auto gaugeFieldMatcher = metric.mutable_gauge_fields_filter()->mutable_fields();
gaugeFieldMatcher->set_field(tagId);
gaugeFieldMatcher->add_child()->set_field(2);
- metric.set_condition("SCREEN_ON");
+ metric.set_condition(StringToId("SCREEN_ON"));
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
@@ -186,7 +187,7 @@
EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return());
GaugeMetric metric;
- metric.set_name(metricName);
+ metric.set_id(metricId);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
auto gaugeFieldMatcher = metric.mutable_gauge_fields_filter()->mutable_fields();
gaugeFieldMatcher->set_field(tagId);
@@ -195,8 +196,8 @@
tagId, tagId, bucketStartTimeNs, pullerManager);
Alert alert;
- alert.set_name("alert");
- alert.set_metric_name(metricName);
+ alert.set_id(101);
+ alert.set_metric_id(metricId);
alert.set_trigger_if_sum_gt(25);
alert.set_number_of_buckets(2);
sp<AnomalyTracker> anomalyTracker = gaugeProducer.addAnomalyTracker(alert);
diff --git a/cmds/statsd/tests/metrics/MaxDurationTracker_test.cpp b/cmds/statsd/tests/metrics/MaxDurationTracker_test.cpp
index 7843ca0..6d32329 100644
--- a/cmds/statsd/tests/metrics/MaxDurationTracker_test.cpp
+++ b/cmds/statsd/tests/metrics/MaxDurationTracker_test.cpp
@@ -13,8 +13,9 @@
// limitations under the License.
#include "src/metrics/duration_helper/MaxDurationTracker.h"
-#include "metrics_test_helper.h"
#include "src/condition/ConditionWizard.h"
+#include "metrics_test_helper.h"
+#include "tests/statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -36,7 +37,7 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
const int TagId = 1;
@@ -50,12 +51,13 @@
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
ConditionKey conditionKey1;
- conditionKey1["condition"] = conditionKey;
+ conditionKey1[StringToId("condition")] = conditionKey;
uint64_t bucketStartTimeNs = 10000000000;
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
- MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
+ int64_t metricId = 1;
+ MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
bucketSizeNs, {});
tracker.noteStart(key1, true, bucketStartTimeNs, conditionKey1);
@@ -79,12 +81,13 @@
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
ConditionKey conditionKey1;
- conditionKey1["condition"] = conditionKey;
+ conditionKey1[StringToId("condition")] = conditionKey;
uint64_t bucketStartTimeNs = 10000000000;
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
- MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
+ int64_t metricId = 1;
+ MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
bucketSizeNs, {});
tracker.noteStart(key1, true, bucketStartTimeNs + 1, conditionKey1);
@@ -110,12 +113,13 @@
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
ConditionKey conditionKey1;
- conditionKey1["condition"] = conditionKey;
+ conditionKey1[StringToId("condition")] = conditionKey;
uint64_t bucketStartTimeNs = 10000000000;
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
- MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
+ int64_t metricId = 1;
+ MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
bucketSizeNs, {});
// The event starts.
@@ -141,12 +145,13 @@
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
ConditionKey conditionKey1;
- conditionKey1["condition"] = conditionKey;
+ conditionKey1[StringToId("condition")] = conditionKey;
uint64_t bucketStartTimeNs = 10000000000;
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
- MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, true, bucketStartTimeNs,
+ int64_t metricId = 1;
+ MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs,
bucketSizeNs, {});
// 2 starts
@@ -177,7 +182,7 @@
ConditionKey conditionKey1;
HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 2, "maps");
- conditionKey1["APP_BACKGROUND"] = conditionKey;
+ conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
EXPECT_CALL(*wizard, query(_, conditionKey1)) // #4
.WillOnce(Return(ConditionState::kFalse));
@@ -189,7 +194,8 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
int64_t durationTimeNs = 2 * 1000;
- MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, false, bucketStartTimeNs,
+ int64_t metricId = 1;
+ MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
bucketSizeNs, {});
EXPECT_TRUE(tracker.mAnomalyTrackers.empty());
@@ -206,9 +212,10 @@
}
TEST(MaxDurationTrackerTest, TestAnomalyDetection) {
+ int64_t metricId = 1;
Alert alert;
- alert.set_name("alert");
- alert.set_metric_name("metric");
+ alert.set_id(101);
+ alert.set_metric_id(metricId);
alert.set_trigger_if_sum_gt(32 * NS_PER_SEC);
alert.set_number_of_buckets(2);
alert.set_refractory_period_secs(1);
@@ -216,13 +223,13 @@
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey conditionKey1;
- conditionKey1["APP_BACKGROUND"] = conditionKey;
+ conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
uint64_t bucketStartTimeNs = 10 * NS_PER_SEC;
uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1;
uint64_t bucketSizeNs = 30 * NS_PER_SEC;
sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey);
- MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, true, bucketStartTimeNs,
+ MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs,
bucketSizeNs, {anomalyTracker});
tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
diff --git a/cmds/statsd/tests/metrics/OringDurationTracker_test.cpp b/cmds/statsd/tests/metrics/OringDurationTracker_test.cpp
index 550b059..d34c85b 100644
--- a/cmds/statsd/tests/metrics/OringDurationTracker_test.cpp
+++ b/cmds/statsd/tests/metrics/OringDurationTracker_test.cpp
@@ -13,8 +13,9 @@
// limitations under the License.
#include "src/metrics/duration_helper/OringDurationTracker.h"
-#include "metrics_test_helper.h"
#include "src/condition/ConditionWizard.h"
+#include "metrics_test_helper.h"
+#include "tests/statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -34,8 +35,9 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
const int TagId = 1;
+const int64_t metricId = 123;
const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "event");
const std::vector<HashableDimensionKey> kConditionKey1 = {getMockedDimensionKey(TagId, 1, "maps")};
@@ -46,7 +48,7 @@
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
@@ -55,7 +57,7 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
uint64_t durationTimeNs = 2 * 1000;
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, false,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false,
bucketStartTimeNs, bucketSizeNs, {});
tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1);
@@ -75,7 +77,7 @@
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
@@ -83,7 +85,7 @@
uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, true, bucketStartTimeNs,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs,
bucketSizeNs, {});
tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1);
@@ -102,7 +104,7 @@
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
@@ -110,7 +112,7 @@
uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, true, bucketStartTimeNs,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs,
bucketSizeNs, {});
tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1);
@@ -128,7 +130,7 @@
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
@@ -137,7 +139,7 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
uint64_t durationTimeNs = 2 * 1000;
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, true, bucketStartTimeNs,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs,
bucketSizeNs, {});
tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1);
@@ -163,7 +165,7 @@
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
EXPECT_CALL(*wizard, query(_, key1)) // #4
.WillOnce(Return(ConditionState::kFalse));
@@ -175,7 +177,7 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
uint64_t durationTimeNs = 2 * 1000;
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, false,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false,
bucketStartTimeNs, bucketSizeNs, {});
tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1);
@@ -194,7 +196,7 @@
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
EXPECT_CALL(*wizard, query(_, key1))
.Times(2)
@@ -208,7 +210,7 @@
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
uint64_t durationTimeNs = 2 * 1000;
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, false,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false,
bucketStartTimeNs, bucketSizeNs, {});
tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1);
@@ -229,7 +231,7 @@
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
EXPECT_CALL(*wizard, query(_, key1)) // #4
.WillOnce(Return(ConditionState::kFalse));
@@ -240,7 +242,7 @@
uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, true, bucketStartTimeNs,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs,
bucketSizeNs, {});
tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1);
@@ -260,8 +262,8 @@
TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp) {
Alert alert;
- alert.set_name("alert");
- alert.set_metric_name("1");
+ alert.set_id(101);
+ alert.set_metric_id(1);
alert.set_trigger_if_sum_gt(40 * NS_PER_SEC);
alert.set_number_of_buckets(2);
alert.set_refractory_period_secs(1);
@@ -269,13 +271,13 @@
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
uint64_t bucketStartTimeNs = 10 * NS_PER_SEC;
uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1;
uint64_t bucketSizeNs = 30 * NS_PER_SEC;
sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey);
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, true, bucketStartTimeNs,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs,
bucketSizeNs, {anomalyTracker});
// Nothing in the past bucket.
@@ -322,8 +324,8 @@
TEST(OringDurationTrackerTest, TestAnomalyDetection) {
Alert alert;
- alert.set_name("alert");
- alert.set_metric_name("1");
+ alert.set_id(101);
+ alert.set_metric_id(1);
alert.set_trigger_if_sum_gt(40 * NS_PER_SEC);
alert.set_number_of_buckets(2);
alert.set_refractory_period_secs(1);
@@ -331,13 +333,13 @@
unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
ConditionKey key1;
- key1["APP_BACKGROUND"] = kConditionKey1;
+ key1[StringToId("APP_BACKGROUND")] = kConditionKey1;
uint64_t bucketStartTimeNs = 10 * NS_PER_SEC;
uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1;
uint64_t bucketSizeNs = 30 * NS_PER_SEC;
sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey);
- OringDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, true /*nesting*/,
+ OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true /*nesting*/,
bucketStartTimeNs, bucketSizeNs, {anomalyTracker});
tracker.noteStart(DEFAULT_DIMENSION_KEY, true, eventStartTimeNs, key1);
diff --git a/cmds/statsd/tests/metrics/ValueMetricProducer_test.cpp b/cmds/statsd/tests/metrics/ValueMetricProducer_test.cpp
index 12bc834..acbfbba 100644
--- a/cmds/statsd/tests/metrics/ValueMetricProducer_test.cpp
+++ b/cmds/statsd/tests/metrics/ValueMetricProducer_test.cpp
@@ -14,6 +14,7 @@
#include "src/metrics/ValueMetricProducer.h"
#include "metrics_test_helper.h"
+#include "tests/statsd_test_util.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -34,9 +35,9 @@
namespace os {
namespace statsd {
-const ConfigKey kConfigKey(0, "test");
+const ConfigKey kConfigKey(0, 12345);
const int tagId = 1;
-const string metricName = "test_metric";
+const int64_t metricId = 123;
const int64_t bucketStartTimeNs = 10000000000;
const int64_t bucketSizeNs = 60 * 1000 * 1000 * 1000LL;
const int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs;
@@ -48,7 +49,7 @@
*/
TEST(ValueMetricProducerTest, TestNonDimensionalEvents) {
ValueMetric metric;
- metric.set_name(metricName);
+ metric.set_id(metricId);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
metric.set_value_field(2);
@@ -124,10 +125,10 @@
*/
TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition) {
ValueMetric metric;
- metric.set_name(metricName);
+ metric.set_id(metricId);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
metric.set_value_field(2);
- metric.set_condition("SCREEN_ON");
+ metric.set_condition(StringToId("SCREEN_ON"));
sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
shared_ptr<MockStatsPullerManager> pullerManager =
@@ -200,7 +201,7 @@
TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition) {
ValueMetric metric;
- metric.set_name(metricName);
+ metric.set_id(metricId);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
metric.set_value_field(2);
@@ -240,14 +241,14 @@
TEST(ValueMetricProducerTest, TestAnomalyDetection) {
Alert alert;
- alert.set_name("alert");
- alert.set_metric_name(metricName);
+ alert.set_id(101);
+ alert.set_metric_id(metricId);
alert.set_trigger_if_sum_gt(130);
alert.set_number_of_buckets(2);
alert.set_refractory_period_secs(3);
ValueMetric metric;
- metric.set_name(metricName);
+ metric.set_id(metricId);
metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
metric.set_value_field(2);
diff --git a/cmds/statsd/tests/statsd_test_util.cpp b/cmds/statsd/tests/statsd_test_util.cpp
index 39e366f..939dc1f 100644
--- a/cmds/statsd/tests/statsd_test_util.cpp
+++ b/cmds/statsd/tests/statsd_test_util.cpp
@@ -22,7 +22,7 @@
AtomMatcher CreateWakelockStateChangedAtomMatcher(const string& name,
WakelockStateChanged::State state) {
AtomMatcher atom_matcher;
- atom_matcher.set_name(name);
+ atom_matcher.set_id(StringToId(name));
auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
simple_atom_matcher->set_atom_id(android::util::WAKELOCK_STATE_CHANGED);
auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
@@ -42,7 +42,7 @@
AtomMatcher CreateScreenStateChangedAtomMatcher(
const string& name, ScreenStateChanged::State state) {
AtomMatcher atom_matcher;
- atom_matcher.set_name(name);
+ atom_matcher.set_id(StringToId(name));
auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
simple_atom_matcher->set_atom_id(android::util::SCREEN_STATE_CHANGED);
auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
@@ -62,7 +62,7 @@
AtomMatcher CreateSyncStateChangedAtomMatcher(
const string& name, SyncStateChanged::State state) {
AtomMatcher atom_matcher;
- atom_matcher.set_name(name);
+ atom_matcher.set_id(StringToId(name));
auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
simple_atom_matcher->set_atom_id(android::util::SYNC_STATE_CHANGED);
auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
@@ -82,7 +82,7 @@
AtomMatcher CreateActivityForegroundStateChangedAtomMatcher(
const string& name, ActivityForegroundStateChanged::Activity activity) {
AtomMatcher atom_matcher;
- atom_matcher.set_name(name);
+ atom_matcher.set_id(StringToId(name));
auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
simple_atom_matcher->set_atom_id(android::util::ACTIVITY_FOREGROUND_STATE_CHANGED);
auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
@@ -104,7 +104,7 @@
AtomMatcher CreateProcessLifeCycleStateChangedAtomMatcher(
const string& name, ProcessLifeCycleStateChanged::Event event) {
AtomMatcher atom_matcher;
- atom_matcher.set_name(name);
+ atom_matcher.set_id(StringToId(name));
auto simple_atom_matcher = atom_matcher.mutable_simple_atom_matcher();
simple_atom_matcher->set_atom_id(android::util::PROCESS_LIFE_CYCLE_STATE_CHANGED);
auto field_value_matcher = simple_atom_matcher->add_field_value_matcher();
@@ -121,47 +121,47 @@
Predicate CreateScreenIsOnPredicate() {
Predicate predicate;
- predicate.set_name("ScreenIsOn");
- predicate.mutable_simple_predicate()->set_start("ScreenTurnedOn");
- predicate.mutable_simple_predicate()->set_stop("ScreenTurnedOff");
+ predicate.set_id(StringToId("ScreenIsOn"));
+ predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOn"));
+ predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOff"));
return predicate;
}
Predicate CreateScreenIsOffPredicate() {
Predicate predicate;
- predicate.set_name("ScreenIsOff");
- predicate.mutable_simple_predicate()->set_start("ScreenTurnedOff");
- predicate.mutable_simple_predicate()->set_stop("ScreenTurnedOn");
+ predicate.set_id(StringToId("ScreenIsOff"));
+ predicate.mutable_simple_predicate()->set_start(StringToId("ScreenTurnedOff"));
+ predicate.mutable_simple_predicate()->set_stop(StringToId("ScreenTurnedOn"));
return predicate;
}
Predicate CreateHoldingWakelockPredicate() {
Predicate predicate;
- predicate.set_name("HoldingWakelock");
- predicate.mutable_simple_predicate()->set_start("AcquireWakelock");
- predicate.mutable_simple_predicate()->set_stop("ReleaseWakelock");
+ predicate.set_id(StringToId("HoldingWakelock"));
+ predicate.mutable_simple_predicate()->set_start(StringToId("AcquireWakelock"));
+ predicate.mutable_simple_predicate()->set_stop(StringToId("ReleaseWakelock"));
return predicate;
}
Predicate CreateIsSyncingPredicate() {
Predicate predicate;
- predicate.set_name("IsSyncing");
- predicate.mutable_simple_predicate()->set_start("SyncStart");
- predicate.mutable_simple_predicate()->set_stop("SyncEnd");
+ predicate.set_id(StringToId("IsSyncing"));
+ predicate.mutable_simple_predicate()->set_start(StringToId("SyncStart"));
+ predicate.mutable_simple_predicate()->set_stop(StringToId("SyncEnd"));
return predicate;
}
Predicate CreateIsInBackgroundPredicate() {
Predicate predicate;
- predicate.set_name("IsInBackground");
- predicate.mutable_simple_predicate()->set_start("MoveToBackground");
- predicate.mutable_simple_predicate()->set_stop("MoveToForeground");
+ predicate.set_id(StringToId("IsInBackground"));
+ predicate.mutable_simple_predicate()->set_start(StringToId("MoveToBackground"));
+ predicate.mutable_simple_predicate()->set_stop(StringToId("MoveToForeground"));
return predicate;
}
void addPredicateToPredicateCombination(const Predicate& predicate,
Predicate* combinationPredicate) {
- combinationPredicate->mutable_combination()->add_predicate(predicate.name());
+ combinationPredicate->mutable_combination()->add_predicate(predicate.id());
}
FieldMatcher CreateAttributionUidDimensions(const int atomId,
@@ -316,6 +316,9 @@
});
}
+int64_t StringToId(const string& str) {
+ return static_cast<int64_t>(std::hash<std::string>()(str));
+}
} // namespace statsd
} // namespace os
} // namespace android
\ No newline at end of file
diff --git a/cmds/statsd/tests/statsd_test_util.h b/cmds/statsd/tests/statsd_test_util.h
index 282e1b2..5e19da0 100644
--- a/cmds/statsd/tests/statsd_test_util.h
+++ b/cmds/statsd/tests/statsd_test_util.h
@@ -121,6 +121,8 @@
// Util function to sort the log events by timestamp.
void sortLogEventsByTimestamp(std::vector<std::unique_ptr<LogEvent>> *events);
+int64_t StringToId(const string& str);
+
} // namespace statsd
} // namespace os
} // namespace android
\ No newline at end of file