From ed5a385af21573041f9c4b1926001900528c5414 Mon Sep 17 00:00:00 2001 From: Tej Singh Date: Sun, 4 Oct 2020 01:26:42 -0700 Subject: [PATCH] Determine update status for gauge metrics Test: statsd_test Bug: 162323123 Change-Id: I75e2858853719078542d4c80996c766bf5e65e67 --- .../parsing_utils/config_update_utils.cpp | 23 +++- .../config_update_utils_test.cpp | 129 ++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) diff --git a/cmds/statsd/src/metrics/parsing_utils/config_update_utils.cpp b/cmds/statsd/src/metrics/parsing_utils/config_update_utils.cpp index 5ff69753a4a38..8477dba4abe52 100644 --- a/cmds/statsd/src/metrics/parsing_utils/config_update_utils.cpp +++ b/cmds/statsd/src/metrics/parsing_utils/config_update_utils.cpp @@ -531,7 +531,28 @@ bool determineAllMetricUpdateStatuses(const StatsdConfig& config, return false; } } - // TODO: determine update status for count, gauge, value, duration metrics. + + for (int i = 0; i < config.gauge_metric_size(); i++, metricIndex++) { + const GaugeMetric& metric = config.gauge_metric(i); + set conditionDependencies; + if (metric.has_condition()) { + conditionDependencies.insert(metric.condition()); + } + set matcherDependencies; + matcherDependencies.insert(metric.what()); + if (metric.has_trigger_event()) { + matcherDependencies.insert(metric.trigger_event()); + } + if (!determineMetricUpdateStatus( + config, metric, metric.id(), METRIC_TYPE_GAUGE, matcherDependencies, + conditionDependencies, ::google::protobuf::RepeatedField(), + metric.links(), oldMetricProducerMap, oldMetricProducers, metricToActivationMap, + replacedMatchers, replacedConditions, replacedStates, + metricsToUpdate[metricIndex])) { + return false; + } + } + // TODO: determine update status for value, duration metrics. return true; } diff --git a/cmds/statsd/tests/metrics/parsing_utils/config_update_utils_test.cpp b/cmds/statsd/tests/metrics/parsing_utils/config_update_utils_test.cpp index 9e67587859115..70e4dea77fe48 100644 --- a/cmds/statsd/tests/metrics/parsing_utils/config_update_utils_test.cpp +++ b/cmds/statsd/tests/metrics/parsing_utils/config_update_utils_test.cpp @@ -1266,6 +1266,135 @@ TEST_F(ConfigUpdateTest, TestCountMetricStateChanged) { EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); } +TEST_F(ConfigUpdateTest, TestGaugeMetricPreserve) { + StatsdConfig config; + AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher(); + *config.add_atom_matcher() = startMatcher; + AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher(); + *config.add_atom_matcher() = stopMatcher; + AtomMatcher whatMatcher = CreateScreenBrightnessChangedAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + Predicate predicate = CreateScreenIsOnPredicate(); + *config.add_predicate() = predicate; + + GaugeMetric* metric = config.add_gauge_metric(); + metric->set_id(12345); + metric->set_what(whatMatcher.id()); + metric->set_condition(predicate.id()); + metric->mutable_gauge_fields_filter()->set_include_all(true); + + EXPECT_TRUE(initConfig(config)); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses(config, oldMetricProducerMap, oldMetricProducers, + metricToActivationMap, + /*replacedMatchers*/ {}, /*replacedConditions=*/{}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_PRESERVE); +} + +TEST_F(ConfigUpdateTest, TestGaugeMetricDefinitionChange) { + StatsdConfig config; + AtomMatcher whatMatcher = CreateScreenBrightnessChangedAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + GaugeMetric* metric = config.add_gauge_metric(); + metric->set_id(12345); + metric->set_what(whatMatcher.id()); + metric->mutable_gauge_fields_filter()->set_include_all(true); + + EXPECT_TRUE(initConfig(config)); + + // Change split bucket on app upgrade, which should change the proto, causing replacement. + metric->set_split_bucket_for_app_upgrade(false); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses(config, oldMetricProducerMap, oldMetricProducers, + metricToActivationMap, + /*replacedMatchers*/ {}, /*replacedConditions=*/{}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); +} + +TEST_F(ConfigUpdateTest, TestGaugeMetricWhatChanged) { + StatsdConfig config; + AtomMatcher whatMatcher = CreateScreenBrightnessChangedAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + GaugeMetric* metric = config.add_gauge_metric(); + metric->set_id(12345); + metric->set_what(whatMatcher.id()); + metric->mutable_gauge_fields_filter()->set_include_all(true); + + EXPECT_TRUE(initConfig(config)); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses( + config, oldMetricProducerMap, oldMetricProducers, metricToActivationMap, + /*replacedMatchers*/ {whatMatcher.id()}, /*replacedConditions=*/{}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); +} + +TEST_F(ConfigUpdateTest, TestGaugeMetricConditionChanged) { + StatsdConfig config; + AtomMatcher startMatcher = CreateScreenTurnedOnAtomMatcher(); + *config.add_atom_matcher() = startMatcher; + AtomMatcher stopMatcher = CreateScreenTurnedOffAtomMatcher(); + *config.add_atom_matcher() = stopMatcher; + AtomMatcher whatMatcher = CreateScreenBrightnessChangedAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + Predicate predicate = CreateScreenIsOnPredicate(); + *config.add_predicate() = predicate; + + GaugeMetric* metric = config.add_gauge_metric(); + metric->set_id(12345); + metric->set_what(whatMatcher.id()); + metric->set_condition(predicate.id()); + metric->mutable_gauge_fields_filter()->set_include_all(true); + + EXPECT_TRUE(initConfig(config)); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses( + config, oldMetricProducerMap, oldMetricProducers, metricToActivationMap, + /*replacedMatchers*/ {}, /*replacedConditions=*/{predicate.id()}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); +} + +TEST_F(ConfigUpdateTest, TestGaugeMetricTriggerEventChanged) { + StatsdConfig config; + AtomMatcher triggerEvent = CreateScreenTurnedOnAtomMatcher(); + *config.add_atom_matcher() = triggerEvent; + AtomMatcher whatMatcher = CreateTemperatureAtomMatcher(); + *config.add_atom_matcher() = whatMatcher; + + GaugeMetric* metric = config.add_gauge_metric(); + metric->set_id(12345); + metric->set_what(whatMatcher.id()); + metric->set_trigger_event(triggerEvent.id()); + metric->mutable_gauge_fields_filter()->set_include_all(true); + metric->set_sampling_type(GaugeMetric::FIRST_N_SAMPLES); + + // Create an initial config. + EXPECT_TRUE(initConfig(config)); + + unordered_map metricToActivationMap; + vector metricsToUpdate(1, UPDATE_UNKNOWN); + EXPECT_TRUE(determineAllMetricUpdateStatuses( + config, oldMetricProducerMap, oldMetricProducers, metricToActivationMap, + /*replacedMatchers*/ {triggerEvent.id()}, /*replacedConditions=*/{}, + /*replacedStates=*/{}, metricsToUpdate)); + EXPECT_EQ(metricsToUpdate[0], UPDATE_REPLACE); +} + TEST_F(ConfigUpdateTest, TestUpdateEventMetrics) { StatsdConfig config;