metric_data_collector.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_REPORTING_METRICS_METRIC_DATA_COLLECTOR_H_
  5. #define COMPONENTS_REPORTING_METRICS_METRIC_DATA_COLLECTOR_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/callback_helpers.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/sequence_checker.h"
  14. #include "base/time/time.h"
  15. #include "components/reporting/client/report_queue.h"
  16. #include "components/reporting/metrics/sampler.h"
  17. #include "components/reporting/proto/synced/metric_data.pb.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace reporting {
  20. class MetricRateController;
  21. class MetricReportQueue;
  22. class MetricReportingController;
  23. class ReportingSettings;
  24. // A base class for metric data collection and reporting.
  25. class CollectorBase {
  26. public:
  27. CollectorBase(Sampler* sampler, MetricReportQueue* metric_report_queue);
  28. CollectorBase(const CollectorBase& other) = delete;
  29. CollectorBase& operator=(const CollectorBase& other) = delete;
  30. virtual ~CollectorBase();
  31. protected:
  32. virtual void Collect();
  33. virtual void OnMetricDataCollected(
  34. absl::optional<MetricData> metric_data) = 0;
  35. virtual void ReportMetricData(
  36. MetricData metric_data,
  37. base::OnceClosure on_data_reported = base::DoNothing());
  38. SEQUENCE_CHECKER(sequence_checker_);
  39. private:
  40. const raw_ptr<Sampler> sampler_;
  41. const raw_ptr<MetricReportQueue> metric_report_queue_;
  42. base::WeakPtrFactory<CollectorBase> weak_ptr_factory_{this};
  43. };
  44. // Class to collect and report metric data only one time when the reporting
  45. // setting is enabled.
  46. class OneShotCollector : public CollectorBase {
  47. public:
  48. OneShotCollector(Sampler* sampler,
  49. MetricReportQueue* metric_report_queue,
  50. ReportingSettings* reporting_settings,
  51. const std::string& setting_path,
  52. bool setting_enabled_default_value,
  53. base::OnceClosure on_data_reported = base::DoNothing());
  54. OneShotCollector(const OneShotCollector& other) = delete;
  55. OneShotCollector& operator=(const OneShotCollector& other) = delete;
  56. ~OneShotCollector() override;
  57. protected:
  58. void Collect() override;
  59. void OnMetricDataCollected(absl::optional<MetricData> metric_data) override;
  60. private:
  61. std::unique_ptr<MetricReportingController> reporting_controller_;
  62. base::OnceClosure on_data_reported_;
  63. bool data_collected_ = false;
  64. };
  65. // Class to collect and report metric data periodically if the reporting setting
  66. // is enabled.
  67. class PeriodicCollector : public CollectorBase {
  68. public:
  69. PeriodicCollector(Sampler* sampler,
  70. MetricReportQueue* metric_report_queue,
  71. ReportingSettings* reporting_settings,
  72. const std::string& enable_setting_path,
  73. bool setting_enabled_default_value,
  74. const std::string& rate_setting_path,
  75. base::TimeDelta default_rate,
  76. int rate_unit_to_ms = 1);
  77. PeriodicCollector(const PeriodicCollector& other) = delete;
  78. PeriodicCollector& operator=(const PeriodicCollector& other) = delete;
  79. ~PeriodicCollector() override;
  80. protected:
  81. void OnMetricDataCollected(absl::optional<MetricData> metric_data) override;
  82. private:
  83. virtual void StartPeriodicCollection();
  84. virtual void StopPeriodicCollection();
  85. // `rate_controller_` should be initialized before `reporting_controller_` as
  86. // initializing `reporting_controller` will trigger `rate_controller_` call if
  87. // the setting is enabled.
  88. const std::unique_ptr<MetricRateController> rate_controller_;
  89. const std::unique_ptr<MetricReportingController> reporting_controller_;
  90. };
  91. // Interface for events detection in collected metric data.
  92. class EventDetector {
  93. public:
  94. virtual ~EventDetector() = default;
  95. // Check if there is a new event present in `current_metric_data` and return
  96. // it if found.
  97. virtual absl::optional<MetricEventType> DetectEvent(
  98. const MetricData& previous_metric_data,
  99. const MetricData& current_metric_data) = 0;
  100. };
  101. class AdditionalSamplersCollector {
  102. public:
  103. explicit AdditionalSamplersCollector(std::vector<Sampler*> samplers);
  104. AdditionalSamplersCollector(const AdditionalSamplersCollector& other) =
  105. delete;
  106. AdditionalSamplersCollector& operator=(
  107. const AdditionalSamplersCollector& other) = delete;
  108. ~AdditionalSamplersCollector();
  109. void CollectAll(OptionalMetricCallback on_all_collected_cb,
  110. MetricData metric_data) const;
  111. private:
  112. void CollectAdditionalMetricData(
  113. uint64_t sampler_index,
  114. OptionalMetricCallback on_all_collected_cb,
  115. MetricData metric_data,
  116. absl::optional<MetricData> new_metric_data) const;
  117. const std::vector<Sampler*> samplers_;
  118. SEQUENCE_CHECKER(sequence_checker_);
  119. base::WeakPtrFactory<AdditionalSamplersCollector> weak_ptr_factory_{this};
  120. };
  121. // Class to collect metric data periodically, check the collected data for
  122. // events, and report metric and event data if an event is detecetd.
  123. class PeriodicEventCollector : public PeriodicCollector {
  124. public:
  125. PeriodicEventCollector(Sampler* sampler,
  126. std::unique_ptr<EventDetector> event_detector,
  127. std::vector<Sampler*> additional_samplers,
  128. MetricReportQueue* metric_report_queue,
  129. ReportingSettings* reporting_settings,
  130. const std::string& enable_setting_path,
  131. bool setting_enabled_default_value,
  132. const std::string& rate_setting_path,
  133. base::TimeDelta default_rate,
  134. int rate_unit_to_ms = 1);
  135. PeriodicEventCollector(const PeriodicEventCollector& other) = delete;
  136. PeriodicEventCollector& operator=(const PeriodicEventCollector& other) =
  137. delete;
  138. ~PeriodicEventCollector() override;
  139. protected:
  140. void OnMetricDataCollected(absl::optional<MetricData> metric_data) override;
  141. private:
  142. void OnAdditionalMetricDataCollected(absl::optional<MetricData> metric_data);
  143. const std::unique_ptr<EventDetector> event_detector_;
  144. const std::unique_ptr<AdditionalSamplersCollector>
  145. additional_samplers_collector_;
  146. MetricData last_collected_data_;
  147. };
  148. } // namespace reporting
  149. #endif // COMPONENTS_REPORTING_METRICS_METRIC_DATA_COLLECTOR_H_