xref: /aosp_15_r20/external/cronet/components/metrics/expired_histograms_checker.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_METRICS_EXPIRED_HISTOGRAMS_CHECKER_H_
6 #define COMPONENTS_METRICS_EXPIRED_HISTOGRAMS_CHECKER_H_
7 
8 #include <stdint.h>
9 #include <set>
10 
11 #include "base/containers/span.h"
12 #include "base/metrics/record_histogram_checker.h"
13 #include "base/strings/string_piece.h"
14 
15 namespace metrics {
16 
17 // ExpiredHistogramsChecker implements RecordHistogramChecker interface
18 // to avoid recording expired metrics.
19 class ExpiredHistogramsChecker final : public base::RecordHistogramChecker {
20  public:
21   // Takes a sorted array of histogram hashes in ascending order and a
22   // list of explicitly allowed histogram names as a comma-separated string.
23   // Histograms in the |allowlist_str| are logged even if their hash is in the
24   // |expired_histograms_hashes|.
25   ExpiredHistogramsChecker(base::span<const uint32_t> expired_histogram_hashes,
26                            const std::string& allowlist_str);
27 
28   ExpiredHistogramsChecker(const ExpiredHistogramsChecker&) = delete;
29   ExpiredHistogramsChecker& operator=(const ExpiredHistogramsChecker&) = delete;
30 
31   ~ExpiredHistogramsChecker() override;
32 
33   // Checks if the given |histogram_hash| corresponds to an expired histogram.
34   bool ShouldRecord(uint32_t histogram_hash) const override;
35 
36  private:
37   // Initializes the |allowlist_| array of histogram hashes that should be
38   // recorded regardless of their expiration.
39   void InitAllowlist(const std::string& allowlist_str);
40 
41   // Array of expired histogram hashes.
42   const base::span<const uint32_t> expired_histogram_hashes_;
43 
44   // Set of expired histogram hashes that should be recorded.
45   std::set<uint32_t> allowlist_;
46 };
47 
48 }  // namespace metrics
49 
50 #endif  // COMPONENTS_METRICS_EXPIRED_HISTOGRAMS_CHECKER_H_
51