Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 56fbe680 authored by Tej Singh's avatar Tej Singh
Browse files

Track replaced metrics during config update

This is needed so that we can replace alerts that depend on replaced
metrics.

Test: atest statsd_test
Bug: 162323547
Change-Id: I44a721675b6aa3cc7f28565da14ff23275cdb98f
parent 890f24a6
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -643,7 +643,7 @@ bool updateMetrics(const ConfigKey& key, const StatsdConfig& config, const int64
                   set<int64_t>& noReportMetricIds,
                   unordered_map<int, vector<int>>& activationAtomTrackerToMetricMap,
                   unordered_map<int, vector<int>>& deactivationAtomTrackerToMetricMap,
                   vector<int>& metricsWithActivation) {
                   vector<int>& metricsWithActivation, set<int64_t>& replacedMetrics) {
    sp<ConditionWizard> wizard = new ConditionWizard(allConditionTrackers);
    sp<EventMatcherWizard> matcherWizard = new EventMatcherWizard(allAtomMatchingTrackers);
    const int allMetricsCount = config.count_metric_size() + config.duration_metric_size() +
@@ -689,6 +689,8 @@ bool updateMetrics(const ConfigKey& key, const StatsdConfig& config, const int64
                break;
            }
            case UPDATE_REPLACE:
                replacedMetrics.insert(metric.id());
                [[fallthrough]];  // Intentionally fallthrough to create the new metric producer.
            case UPDATE_NEW: {
                producer = createCountMetricProducerAndUpdateMetadata(
                        key, config, timeBaseNs, currentTimeNs, metric, metricIndex,
@@ -726,6 +728,8 @@ bool updateMetrics(const ConfigKey& key, const StatsdConfig& config, const int64
                break;
            }
            case UPDATE_REPLACE:
                replacedMetrics.insert(metric.id());
                [[fallthrough]];  // Intentionally fallthrough to create the new metric producer.
            case UPDATE_NEW: {
                producer = createDurationMetricProducerAndUpdateMetadata(
                        key, config, timeBaseNs, currentTimeNs, metric, metricIndex,
@@ -763,6 +767,8 @@ bool updateMetrics(const ConfigKey& key, const StatsdConfig& config, const int64
                break;
            }
            case UPDATE_REPLACE:
                replacedMetrics.insert(metric.id());
                [[fallthrough]];  // Intentionally fallthrough to create the new metric producer.
            case UPDATE_NEW: {
                producer = createEventMetricProducerAndUpdateMetadata(
                        key, config, timeBaseNs, metric, metricIndex, allAtomMatchingTrackers,
@@ -800,6 +806,8 @@ bool updateMetrics(const ConfigKey& key, const StatsdConfig& config, const int64
                break;
            }
            case UPDATE_REPLACE:
                replacedMetrics.insert(metric.id());
                [[fallthrough]];  // Intentionally fallthrough to create the new metric producer.
            case UPDATE_NEW: {
                producer = createValueMetricProducerAndUpdateMetadata(
                        key, config, timeBaseNs, currentTimeNs, pullerManager, metric, metricIndex,
@@ -838,6 +846,8 @@ bool updateMetrics(const ConfigKey& key, const StatsdConfig& config, const int64
                break;
            }
            case UPDATE_REPLACE:
                replacedMetrics.insert(metric.id());
                [[fallthrough]];  // Intentionally fallthrough to create the new metric producer.
            case UPDATE_NEW: {
                producer = createGaugeMetricProducerAndUpdateMetadata(
                        key, config, timeBaseNs, currentTimeNs, pullerManager, metric, metricIndex,
@@ -859,7 +869,6 @@ bool updateMetrics(const ConfigKey& key, const StatsdConfig& config, const int64
        }
        newMetricProducers.push_back(producer.value());
    }
    // TODO: perform update for value metric.

    const set<int> atomsAllowedFromAnyUid(config.whitelisted_atom_ids().begin(),
                                          config.whitelisted_atom_ids().end());
@@ -910,6 +919,7 @@ bool updateStatsdConfig(const ConfigKey& key, const StatsdConfig& config, const
                        set<int64_t>& noReportMetricIds) {
    set<int64_t> replacedMatchers;
    set<int64_t> replacedConditions;
    set<int64_t> replacedMetrics;
    vector<ConditionState> conditionCache;
    unordered_map<int64_t, int> stateAtomIdMap;
    unordered_map<int64_t, unordered_map<int, int64_t>> allStateGroupMaps;
@@ -951,7 +961,7 @@ bool updateStatsdConfig(const ConfigKey& key, const StatsdConfig& config, const
                       replacedStates, oldMetricProducerMap, oldMetricProducers,
                       newMetricProducerMap, newMetricProducers, conditionToMetricMap,
                       trackerToMetricMap, noReportMetricIds, activationTrackerToMetricMap,
                       deactivationTrackerToMetricMap, metricsWithActivation)) {
                       deactivationTrackerToMetricMap, metricsWithActivation, replacedMetrics)) {
        ALOGE("initMetricProducers failed");
        return false;
    }
+1 −1
Original line number Diff line number Diff line
@@ -187,7 +187,7 @@ bool updateMetrics(
        std::set<int64_t>& noReportMetricIds,
        std::unordered_map<int, std::vector<int>>& activationAtomTrackerToMetricMap,
        std::unordered_map<int, std::vector<int>>& deactivationAtomTrackerToMetricMap,
        std::vector<int>& metricsWithActivation);
        std::vector<int>& metricsWithActivation, std::set<int64_t>& replacedMetrics);

// Updates the existing MetricsManager from a new StatsdConfig.
// Parameters are the members of MetricsManager. See MetricsManager for declaration.
+21 −8
Original line number Diff line number Diff line
@@ -1812,6 +1812,7 @@ TEST_F(ConfigUpdateTest, TestUpdateEventMetrics) {
    unordered_map<int, vector<int>> activationAtomTrackerToMetricMap;
    unordered_map<int, vector<int>> deactivationAtomTrackerToMetricMap;
    vector<int> metricsWithActivation;
    set<int64_t> replacedMetrics;
    EXPECT_TRUE(updateMetrics(
            key, newConfig, /*timeBaseNs=*/123, /*currentTimeNs=*/12345, new StatsPullerManager(),
            oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap, replacedMatchers,
@@ -1820,13 +1821,14 @@ TEST_F(ConfigUpdateTest, TestUpdateEventMetrics) {
            /*replacedStates=*/{}, oldMetricProducerMap, oldMetricProducers, newMetricProducerMap,
            newMetricProducers, conditionToMetricMap, trackerToMetricMap, noReportMetricIds,
            activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap,
            metricsWithActivation));
            metricsWithActivation, replacedMetrics));

    unordered_map<int64_t, int> expectedMetricProducerMap = {
            {event1Id, event1Index}, {event2Id, event2Index}, {event3Id, event3Index},
            {event4Id, event4Index}, {event6Id, event6Index},
    };
    EXPECT_THAT(newMetricProducerMap, ContainerEq(expectedMetricProducerMap));
    EXPECT_EQ(replacedMetrics, set<int64_t>({event2Id, event3Id, event4Id}));

    // Make sure preserved metrics are the same.
    ASSERT_EQ(newMetricProducers.size(), 5);
@@ -2041,6 +2043,7 @@ TEST_F(ConfigUpdateTest, TestUpdateCountMetrics) {
    unordered_map<int, vector<int>> activationAtomTrackerToMetricMap;
    unordered_map<int, vector<int>> deactivationAtomTrackerToMetricMap;
    vector<int> metricsWithActivation;
    set<int64_t> replacedMetrics;
    EXPECT_TRUE(updateMetrics(
            key, newConfig, /*timeBaseNs=*/123, /*currentTimeNs=*/12345, new StatsPullerManager(),
            oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap, replacedMatchers,
@@ -2049,13 +2052,14 @@ TEST_F(ConfigUpdateTest, TestUpdateCountMetrics) {
            oldMetricProducerMap, oldMetricProducers, newMetricProducerMap, newMetricProducers,
            conditionToMetricMap, trackerToMetricMap, noReportMetricIds,
            activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap,
            metricsWithActivation));
            metricsWithActivation, replacedMetrics));

    unordered_map<int64_t, int> expectedMetricProducerMap = {
            {count1Id, count1Index}, {count2Id, count2Index}, {count3Id, count3Index},
            {count4Id, count4Index}, {count6Id, count6Index},
    };
    EXPECT_THAT(newMetricProducerMap, ContainerEq(expectedMetricProducerMap));
    EXPECT_EQ(replacedMetrics, set<int64_t>({count2Id, count3Id, count4Id}));

    // Make sure preserved metrics are the same.
    ASSERT_EQ(newMetricProducers.size(), 5);
@@ -2250,6 +2254,7 @@ TEST_F(ConfigUpdateTest, TestUpdateGaugeMetrics) {
    unordered_map<int, vector<int>> activationAtomTrackerToMetricMap;
    unordered_map<int, vector<int>> deactivationAtomTrackerToMetricMap;
    vector<int> metricsWithActivation;
    set<int64_t> replacedMetrics;
    EXPECT_TRUE(updateMetrics(
            key, newConfig, /*timeBaseNs=*/123, /*currentTimeNs=*/12345, new StatsPullerManager(),
            oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap, replacedMatchers,
@@ -2258,13 +2263,14 @@ TEST_F(ConfigUpdateTest, TestUpdateGaugeMetrics) {
            /*replacedStates=*/{}, oldMetricProducerMap, oldMetricProducers, newMetricProducerMap,
            newMetricProducers, conditionToMetricMap, trackerToMetricMap, noReportMetricIds,
            activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap,
            metricsWithActivation));
            metricsWithActivation, replacedMetrics));

    unordered_map<int64_t, int> expectedMetricProducerMap = {
            {gauge1Id, gauge1Index}, {gauge2Id, gauge2Index}, {gauge3Id, gauge3Index},
            {gauge4Id, gauge4Index}, {gauge6Id, gauge6Index},
    };
    EXPECT_THAT(newMetricProducerMap, ContainerEq(expectedMetricProducerMap));
    EXPECT_EQ(replacedMetrics, set<int64_t>({gauge2Id, gauge3Id, gauge4Id}));

    // Make sure preserved metrics are the same.
    ASSERT_EQ(newMetricProducers.size(), 5);
@@ -2567,6 +2573,7 @@ TEST_F(ConfigUpdateTest, TestUpdateDurationMetrics) {
    unordered_map<int, vector<int>> activationAtomTrackerToMetricMap;
    unordered_map<int, vector<int>> deactivationAtomTrackerToMetricMap;
    vector<int> metricsWithActivation;
    set<int64_t> replacedMetrics;
    EXPECT_TRUE(updateMetrics(
            key, newConfig, /*timeBaseNs=*/123, /*currentTimeNs=*/12345, new StatsPullerManager(),
            oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap, /*replacedMatchers=*/{},
@@ -2575,7 +2582,7 @@ TEST_F(ConfigUpdateTest, TestUpdateDurationMetrics) {
            oldMetricProducerMap, oldMetricProducers, newMetricProducerMap, newMetricProducers,
            conditionToMetricMap, trackerToMetricMap, noReportMetricIds,
            activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap,
            metricsWithActivation));
            metricsWithActivation, replacedMetrics));

    unordered_map<int64_t, int> expectedMetricProducerMap = {
            {duration1Id, duration1Index}, {duration2Id, duration2Index},
@@ -2583,7 +2590,7 @@ TEST_F(ConfigUpdateTest, TestUpdateDurationMetrics) {
            {duration6Id, duration6Index},
    };
    EXPECT_THAT(newMetricProducerMap, ContainerEq(expectedMetricProducerMap));

    EXPECT_EQ(replacedMetrics, set<int64_t>({duration2Id, duration3Id, duration4Id}));
    // Make sure preserved metrics are the same.
    ASSERT_EQ(newMetricProducers.size(), 5);
    EXPECT_EQ(oldMetricProducers[oldMetricProducerMap.at(duration1Id)],
@@ -2831,6 +2838,7 @@ TEST_F(ConfigUpdateTest, TestUpdateValueMetrics) {
    unordered_map<int, vector<int>> activationAtomTrackerToMetricMap;
    unordered_map<int, vector<int>> deactivationAtomTrackerToMetricMap;
    vector<int> metricsWithActivation;
    set<int64_t> replacedMetrics;
    EXPECT_TRUE(updateMetrics(
            key, newConfig, /*timeBaseNs=*/123, /*currentTimeNs=*/12345, new StatsPullerManager(),
            oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap, /*replacedMatchers=*/{},
@@ -2839,13 +2847,14 @@ TEST_F(ConfigUpdateTest, TestUpdateValueMetrics) {
            oldMetricProducerMap, oldMetricProducers, newMetricProducerMap, newMetricProducers,
            conditionToMetricMap, trackerToMetricMap, noReportMetricIds,
            activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap,
            metricsWithActivation));
            metricsWithActivation, replacedMetrics));

    unordered_map<int64_t, int> expectedMetricProducerMap = {
            {value1Id, value1Index}, {value2Id, value2Index}, {value3Id, value3Index},
            {value4Id, value4Index}, {value6Id, value6Index},
    };
    EXPECT_THAT(newMetricProducerMap, ContainerEq(expectedMetricProducerMap));
    EXPECT_EQ(replacedMetrics, set<int64_t>({value2Id, value3Id, value4Id}));

    // Make sure preserved metrics are the same.
    ASSERT_EQ(newMetricProducers.size(), 5);
@@ -3005,6 +3014,7 @@ TEST_F(ConfigUpdateTest, TestUpdateMetricActivations) {
    unordered_map<int, vector<int>> activationAtomTrackerToMetricMap;
    unordered_map<int, vector<int>> deactivationAtomTrackerToMetricMap;
    vector<int> metricsWithActivation;
    set<int64_t> replacedMetrics;
    EXPECT_TRUE(updateMetrics(
            key, config, /*timeBaseNs=*/123, /*currentTimeNs=*/12345, new StatsPullerManager(),
            oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap, replacedMatchers,
@@ -3013,7 +3023,7 @@ TEST_F(ConfigUpdateTest, TestUpdateMetricActivations) {
            /*replacedStates=*/{}, oldMetricProducerMap, oldMetricProducers, newMetricProducerMap,
            newMetricProducers, conditionToMetricMap, trackerToMetricMap, noReportMetricIds,
            activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap,
            metricsWithActivation));
            metricsWithActivation, replacedMetrics));

    // Verify event activation/deactivation maps.
    ASSERT_EQ(activationAtomTrackerToMetricMap.size(), 3);
@@ -3157,6 +3167,7 @@ TEST_F(ConfigUpdateTest, TestUpdateMetricsMultipleTypes) {
    unordered_map<int, vector<int>> activationAtomTrackerToMetricMap;
    unordered_map<int, vector<int>> deactivationAtomTrackerToMetricMap;
    vector<int> metricsWithActivation;
    set<int64_t> replacedMetrics;
    EXPECT_TRUE(updateMetrics(
            key, newConfig, /*timeBaseNs=*/123, /*currentTimeNs=*/12345, new StatsPullerManager(),
            oldAtomMatchingTrackerMap, newAtomMatchingTrackerMap, replacedMatchers,
@@ -3165,7 +3176,7 @@ TEST_F(ConfigUpdateTest, TestUpdateMetricsMultipleTypes) {
            /*replacedStates=*/{}, oldMetricProducerMap, oldMetricProducers, newMetricProducerMap,
            newMetricProducers, conditionToMetricMap, trackerToMetricMap, noReportMetricIds,
            activationAtomTrackerToMetricMap, deactivationAtomTrackerToMetricMap,
            metricsWithActivation));
            metricsWithActivation, replacedMetrics));

    unordered_map<int64_t, int> expectedMetricProducerMap = {
            {countMetricId, countMetricIndex}, {durationMetricId, durationMetricIndex},
@@ -3174,6 +3185,8 @@ TEST_F(ConfigUpdateTest, TestUpdateMetricsMultipleTypes) {
    };
    EXPECT_THAT(newMetricProducerMap, ContainerEq(expectedMetricProducerMap));

    EXPECT_EQ(replacedMetrics, set<int64_t>({eventMetricId, gaugeMetricId}));

    // Make sure preserved metrics are the same.
    ASSERT_EQ(newMetricProducers.size(), 5);
    EXPECT_EQ(oldMetricProducers[oldMetricProducerMap.at(countMetricId)],