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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/reporting/metrics/one_shot_collector.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "components/reporting/client/report_queue.h"
#include "components/reporting/metrics/collector_base.h"
#include "components/reporting/metrics/metric_report_queue.h"
#include "components/reporting/metrics/metric_reporting_controller.h"
#include "components/reporting/metrics/reporting_settings.h"
#include "components/reporting/metrics/sampler.h"
#include "components/reporting/proto/synced/record_constants.pb.h"
namespace reporting {
OneShotCollector::OneShotCollector(
Sampler* sampler,
MetricReportQueue* metric_report_queue,
ReportingSettings* reporting_settings,
const std::string& setting_path,
bool setting_enabled_default_value,
ReportQueue::EnqueueCallback on_data_reported)
: OneShotCollector(sampler,
metric_report_queue,
reporting_settings,
setting_path,
setting_enabled_default_value,
/*init_delay=*/base::TimeDelta(),
std::move(on_data_reported)) {}
OneShotCollector::OneShotCollector(
Sampler* sampler,
MetricReportQueue* metric_report_queue,
ReportingSettings* reporting_settings,
const std::string& setting_path,
bool setting_enabled_default_value,
base::TimeDelta init_delay,
ReportQueue::EnqueueCallback on_data_reported)
: CollectorBase(sampler),
metric_report_queue_(metric_report_queue),
on_data_reported_(std::move(on_data_reported)) {
reporting_controller_ = std::make_unique<MetricReportingController>(
reporting_settings, setting_path, setting_enabled_default_value);
if (init_delay.is_zero()) {
SetReportingControllerCb();
return;
}
CHECK(base::SequencedTaskRunner::HasCurrentDefault());
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&OneShotCollector::SetReportingControllerCb,
weak_ptr_factory_.GetWeakPtr()),
init_delay);
}
OneShotCollector::~OneShotCollector() = default;
void OneShotCollector::Collect(bool is_event_driven) {
CheckOnSequence();
if (!is_event_driven) {
if (data_collected_) {
return;
}
// TODO(b/260093529): Should this be set for event driven telemetry
// collection as well?
data_collected_ = true;
}
CollectorBase::Collect(is_event_driven);
}
bool OneShotCollector::CanCollect() const {
return reporting_controller_->IsEnabled();
}
void OneShotCollector::OnMetricDataCollected(
bool is_event_driven,
std::optional<MetricData> metric_data) {
CheckOnSequence();
CHECK(is_event_driven || on_data_reported_);
CHECK(metric_report_queue_);
if (!metric_data.has_value()) {
base::UmaHistogramExactLinear(
OneShotCollector::kNoMetricDataMetricsName,
static_cast<int>(metric_report_queue_->GetDestination()),
Destination_ARRAYSIZE);
return;
}
metric_data->set_timestamp_ms(
base::Time::Now().InMillisecondsSinceUnixEpoch());
if (is_event_driven) {
CHECK(metric_data->has_telemetry_data());
metric_data->mutable_telemetry_data()->set_is_event_driven(is_event_driven);
}
metric_report_queue_->Enqueue(
std::move(metric_data.value()),
is_event_driven ? base::DoNothing() : std::move(on_data_reported_));
}
void OneShotCollector::SetReportingControllerCb() {
CheckOnSequence();
reporting_controller_->SetSettingUpdateCb(
base::BindRepeating(&OneShotCollector::Collect, base::Unretained(this),
/*is_event_driven=*/false));
}
} // namespace reporting
|