summaryrefslogtreecommitdiff
path: root/statsd/src/metrics/NumericValueMetricProducer.h
blob: 775fc1af8761430c1a2835bf983f61b6e92d9793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * 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.
 */

#pragma once

#include <gtest/gtest_prod.h>

#include <optional>

#include "ValueMetricProducer.h"

namespace android {
namespace os {
namespace statsd {

// TODO(b/185796344): don't use Value from FieldValue.
using ValueBases = std::vector<std::optional<Value>>;
class NumericValueMetricProducer : public ValueMetricProducer<Value, ValueBases> {
public:
    NumericValueMetricProducer(const ConfigKey& key, const ValueMetric& valueMetric,
                               const uint64_t protoHash, const PullOptions& pullOptions,
                               const BucketOptions& bucketOptions, const WhatOptions& whatOptions,
                               const ConditionOptions& conditionOptions,
                               const StateOptions& stateOptions,
                               const ActivationOptions& activationOptions,
                               const GuardrailOptions& guardrailOptions);

    // Process data pulled on bucket boundary.
    void onDataPulled(const std::vector<std::shared_ptr<LogEvent>>& allData, PullResult pullResult,
                      int64_t originalPullTimeNs) override;

    // Determine if metric needs to pull
    bool isPullNeeded() const override {
        std::lock_guard<std::mutex> lock(mMutex);
        return mIsActive && (mCondition == ConditionState::kTrue);
    }

    inline MetricType getMetricType() const override {
        return METRIC_TYPE_VALUE;
    }

protected:
private:
    void prepareFirstBucketLocked() override;

    inline optional<int64_t> getConditionIdForMetric(const StatsdConfig& config,
                                                     const int configIndex) const override {
        const ValueMetric& metric = config.value_metric(configIndex);
        return metric.has_condition() ? make_optional(metric.condition()) : nullopt;
    }

    inline int64_t getWhatAtomMatcherIdForMetric(const StatsdConfig& config,
                                                 const int configIndex) const override {
        return config.value_metric(configIndex).what();
    }

    inline ConditionLinks getConditionLinksForMetric(const StatsdConfig& config,
                                                     const int configIndex) const override {
        return config.value_metric(configIndex).links();
    }

    void onActiveStateChangedInternalLocked(const int64_t eventTimeNs,
                                            const bool isActive) override;

    // Only called when mIsActive and the event is NOT too late.
    void onConditionChangedInternalLocked(const ConditionState oldCondition,
                                          const ConditionState newCondition,
                                          const int64_t eventTimeNs) override;

    inline std::string aggregatedValueToString(const Value& value) const override {
        return value.toString();
    }

    // Mark the data as invalid.
    void invalidateCurrentBucket(const int64_t dropTimeNs, const BucketDropReason reason) override;

    // Reset diff base and mHasGlobalBase
    void resetBase();

    // Calculate previous bucket end time based on current time.
    int64_t calcPreviousBucketEndTime(const int64_t currentTimeNs);

    inline bool multipleBucketsSkipped(const int64_t numBucketsForward) const override {
        return numBucketsForward > 1 && (isPulled() || mUseDiff);
    }

    // Process events retrieved from a pull.
    void accumulateEvents(const std::vector<std::shared_ptr<LogEvent>>& allData,
                          int64_t originalPullTimeNs, int64_t eventElapsedTimeNs);

    void closeCurrentBucket(const int64_t eventTimeNs,
                            const int64_t nextBucketStartTimeNs) override;

    PastBucket<Value> buildPartialBucket(int64_t bucketEndTime,
                                         std::vector<Interval>& intervals) override;

    bool valuePassesThreshold(const Interval& interval) const;

    Value getFinalValue(const Interval& interval) const;

    void initNextSlicedBucket(int64_t nextBucketStartTimeNs) override;

    void appendToFullBucket(const bool isFullBucketReached);

    bool hitFullBucketGuardRailLocked(const MetricDimensionKey& newKey);

    inline bool canSkipLogEventLocked(
            const MetricDimensionKey& eventKey, const bool condition, int64_t eventTimeNs,
            const map<int, HashableDimensionKey>& statePrimaryKeys) const override {
        // For pushed metrics, can only skip if condition is false.
        // For pulled metrics, can only skip if metric is not diffed and condition is false or
        // unknown.
        return (!isPulled() && !condition) ||
               (isPulled() && !mUseDiff && mCondition != ConditionState::kTrue);
    }

    bool aggregateFields(const int64_t eventTimeNs, const MetricDimensionKey& eventKey,
                         const LogEvent& event, std::vector<Interval>& intervals,
                         ValueBases& bases) override;

    void pullAndMatchEventsLocked(const int64_t timestampNs) override;

    DumpProtoFields getDumpProtoFields() const override;

    void writePastBucketAggregateToProto(const int aggIndex, const Value& value,
                                         const int sampleSize,
                                         ProtoOutputStream* const protoOutput) const override;

    // Internal function to calculate the current used bytes.
    size_t byteSizeLocked() const override;

    void combineValueFields(pair<LogEvent, vector<int>>& eventValues, const LogEvent& newEvent,
                            const vector<int>& newValueIndices) const;

    const bool mUseAbsoluteValueOnReset;

    const ValueMetric::AggregationType mAggregationType;

    const bool mIncludeSampleSize;

    const bool mUseDiff;

    const ValueMetric::ValueDirection mValueDirection;

    const bool mSkipZeroDiffOutput;

    // If true, use a zero value as base to compute the diff.
    // This is used for new keys which are present in the new data but was not
    // present in the base data.
    // The default base will only be used if we have a global base.
    const bool mUseZeroDefaultBase;

    // For pulled metrics, this is always set to true whenever a pull succeeds.
    // It is set to false when a pull fails, or upon condition change to false.
    // This is used to decide if we have the right base data to compute the
    // diff against.
    bool mHasGlobalBase;

    const int64_t mMaxPullDelayNs;

    // For anomaly detection.
    std::unordered_map<MetricDimensionKey, int64_t> mCurrentFullBucket;

    FRIEND_TEST(NumericValueMetricProducerTest, TestAnomalyDetection);
    FRIEND_TEST(NumericValueMetricProducerTest, TestBaseSetOnConditionChange);
    FRIEND_TEST(NumericValueMetricProducerTest, TestBucketBoundariesOnConditionChange);
    FRIEND_TEST(NumericValueMetricProducerTest, TestBucketBoundaryNoCondition);
    FRIEND_TEST(NumericValueMetricProducerTest, TestBucketBoundaryWithCondition);
    FRIEND_TEST(NumericValueMetricProducerTest, TestBucketBoundaryWithCondition2);
    FRIEND_TEST(NumericValueMetricProducerTest, TestBucketInvalidIfGlobalBaseIsNotSet);
    FRIEND_TEST(NumericValueMetricProducerTest, TestCalcPreviousBucketEndTime);
    FRIEND_TEST(NumericValueMetricProducerTest, TestDataIsNotUpdatedWhenNoConditionChanged);
    FRIEND_TEST(NumericValueMetricProducerTest, TestEmptyDataResetsBase_onBucketBoundary);
    FRIEND_TEST(NumericValueMetricProducerTest, TestEmptyDataResetsBase_onConditionChanged);
    FRIEND_TEST(NumericValueMetricProducerTest, TestEmptyDataResetsBase_onDataPulled);
    FRIEND_TEST(NumericValueMetricProducerTest, TestEventsWithNonSlicedCondition);
    FRIEND_TEST(NumericValueMetricProducerTest, TestFirstBucket);
    FRIEND_TEST(NumericValueMetricProducerTest, TestLateOnDataPulledWithDiff);
    FRIEND_TEST(NumericValueMetricProducerTest, TestLateOnDataPulledWithoutDiff);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPartialResetOnBucketBoundaries);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledData_noDiff_bucketBoundaryFalse);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledData_noDiff_bucketBoundaryTrue);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledData_noDiff_withFailure);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledData_noDiff_withMultipleConditionChanges);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledData_noDiff_withoutCondition);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledEventsNoCondition);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledEventsTakeAbsoluteValueOnReset);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledEventsTakeZeroOnReset);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledEventsWithFiltering);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPulledWithAppUpgradeDisabled);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPushedAggregateAvg);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPushedAggregateMax);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPushedAggregateMin);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPushedAggregateSum);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPushedEventsWithCondition);
    FRIEND_TEST(NumericValueMetricProducerTest, TestPushedEventsWithoutCondition);
    FRIEND_TEST(NumericValueMetricProducerTest, TestResetBaseOnPullDelayExceeded);
    FRIEND_TEST(NumericValueMetricProducerTest, TestResetBaseOnPullFailAfterConditionChange);
    FRIEND_TEST(NumericValueMetricProducerTest,
                TestResetBaseOnPullFailAfterConditionChange_EndOfBucket);
    FRIEND_TEST(NumericValueMetricProducerTest, TestResetBaseOnPullFailBeforeConditionChange);
    FRIEND_TEST(NumericValueMetricProducerTest, TestResetBaseOnPullTooLate);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSampleSize);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSkipZeroDiffOutput);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSkipZeroDiffOutputMultiValue);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedState);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithMap);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithPrimaryField_WithDimensions);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithCondition);
    FRIEND_TEST(NumericValueMetricProducerTest, TestTrimUnusedDimensionKey);
    FRIEND_TEST(NumericValueMetricProducerTest, TestUseZeroDefaultBase);
    FRIEND_TEST(NumericValueMetricProducerTest, TestUseZeroDefaultBaseWithPullFailures);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithMultipleDimensions);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithMissingDataInStateChange);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithDataMissingInConditionChange);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithMissingDataThenFlushBucket);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithNoPullOnBucketBoundary);
    FRIEND_TEST(NumericValueMetricProducerTest, TestSlicedStateWithConditionFalseMultipleBuckets);
    FRIEND_TEST(NumericValueMetricProducerTest,
                TestSlicedStateWithMultipleDimensionsMissingDataInPull);
    FRIEND_TEST(NumericValueMetricProducerTest, TestUploadThreshold);

    FRIEND_TEST(NumericValueMetricProducerTest_BucketDrop, TestInvalidBucketWhenOneConditionFailed);
    FRIEND_TEST(NumericValueMetricProducerTest_BucketDrop, TestInvalidBucketWhenInitialPullFailed);
    FRIEND_TEST(NumericValueMetricProducerTest_BucketDrop, TestInvalidBucketWhenLastPullFailed);
    FRIEND_TEST(NumericValueMetricProducerTest_BucketDrop, TestInvalidBucketWhenGuardRailHit);
    FRIEND_TEST(NumericValueMetricProducerTest_BucketDrop,
                TestInvalidBucketWhenDumpReportRequested);
    FRIEND_TEST(NumericValueMetricProducerTest_BucketDrop,
                TestInvalidBucketWhenAccumulateEventWrongBucket);
    FRIEND_TEST(NumericValueMetricProducerTest_BucketDrop,
                TestInvalidBucketWhenMultipleBucketsSkipped);

    FRIEND_TEST(NumericValueMetricProducerTest_PartialBucket, TestBucketBoundariesOnPartialBucket);
    FRIEND_TEST(NumericValueMetricProducerTest_PartialBucket,
                TestFullBucketResetWhenLastBucketInvalid);
    FRIEND_TEST(NumericValueMetricProducerTest_PartialBucket, TestPartialBucketCreated);
    FRIEND_TEST(NumericValueMetricProducerTest_PartialBucket, TestPushedEvents);
    FRIEND_TEST(NumericValueMetricProducerTest_PartialBucket, TestPulledValue);
    FRIEND_TEST(NumericValueMetricProducerTest_PartialBucket, TestPulledValueWhileConditionFalse);

    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestAlarmLatePullWhileConditionTrue);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestAlarmLatePullWithConditionChanged);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestAlarmLatePullWhileConditionFalse);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestLatePullOnConditionChangeFalse);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestLatePullOnConditionChangeTrue);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection, TestAlarmLatePullNoCondition);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestAlarmLatePullNoConditionWithSkipped);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestThresholdNotDefinedNoUpload);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection, TestThresholdDefinedZero);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestThresholdUploadPassWhenEqual);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection,
                TestThresholdUploadPassWhenGreater);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection, TestThresholdUploadSkip);
    FRIEND_TEST(NumericValueMetricProducerTest_ConditionCorrection, TestLateStateChangeSlicedAtoms);

    FRIEND_TEST(NumericValueMetricProducerTest, TestSubsetDimensions);

    FRIEND_TEST(ConfigUpdateTest, TestUpdateValueMetrics);

    FRIEND_TEST(MetricsManagerUtilDimLimitTest, TestDimLimit);

    FRIEND_TEST(ConfigUpdateDimLimitTest, TestDimLimit);

    friend class NumericValueMetricProducerTestHelper;
};

}  // namespace statsd
}  // namespace os
}  // namespace android