Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 5 | #include "fuchsia/base/legacymetrics_client.h" |
| 6 | |
| 7 | #include <lib/fit/function.h> |
| 8 | #include <lib/sys/cpp/component_context.h> |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 9 | #include <zircon/errors.h> |
| 10 | |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 11 | #include <algorithm> |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 12 | #include <memory> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 16 | #include "base/fuchsia/fuchsia_logging.h" |
Sharon Yang | b2ff20e | 2020-06-19 12:54:01 | [diff] [blame] | 17 | #include "base/fuchsia/process_context.h" |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 18 | #include "base/logging.h" |
| 19 | #include "base/threading/thread_task_runner_handle.h" |
| 20 | #include "base/time/time.h" |
| 21 | #include "fuchsia/base/legacymetrics_histogram_flattener.h" |
| 22 | |
| 23 | namespace cr_fuchsia { |
| 24 | |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 25 | constexpr size_t LegacyMetricsClient::kMaxBatchSize; |
| 26 | |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 27 | constexpr base::TimeDelta LegacyMetricsClient::kInitialReconnectDelay; |
| 28 | constexpr base::TimeDelta LegacyMetricsClient::kMaxReconnectDelay; |
| 29 | constexpr size_t LegacyMetricsClient::kReconnectBackoffFactor; |
| 30 | |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 31 | LegacyMetricsClient::LegacyMetricsClient() = default; |
| 32 | |
| 33 | LegacyMetricsClient::~LegacyMetricsClient() { |
| 34 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 35 | } |
| 36 | |
| 37 | void LegacyMetricsClient::Start(base::TimeDelta report_interval) { |
| 38 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 39 | DCHECK_GT(report_interval, base::TimeDelta::FromSeconds(0)); |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 40 | |
| 41 | // Start recording user events. |
| 42 | user_events_recorder_ = std::make_unique<LegacyMetricsUserActionRecorder>(); |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 43 | |
| 44 | report_interval_ = report_interval; |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 45 | ConnectAndStartReporting(); |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void LegacyMetricsClient::SetReportAdditionalMetricsCallback( |
| 49 | ReportAdditionalMetricsCallback callback) { |
| 50 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 51 | DCHECK(!metrics_recorder_) |
| 52 | << "SetReportAdditionalMetricsCallback() must be called before Start()."; |
| 53 | DCHECK(!report_additional_callback_); |
| 54 | DCHECK(callback); |
| 55 | |
| 56 | report_additional_callback_ = std::move(callback); |
| 57 | } |
| 58 | |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 59 | void LegacyMetricsClient::SetNotifyFlushCallback(NotifyFlushCallback callback) { |
| 60 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 61 | DCHECK(callback); |
| 62 | DCHECK(!metrics_recorder_) |
| 63 | << "SetNotifyFlushCallback() must be called before Start()."; |
| 64 | |
| 65 | notify_flush_callback_ = std::move(callback); |
| 66 | } |
| 67 | |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 68 | void LegacyMetricsClient::ConnectAndStartReporting() { |
| 69 | DCHECK(!metrics_recorder_) << "Trying to connect when already connected."; |
| 70 | DVLOG(1) << "Trying to connect to MetricsRecorder service."; |
| 71 | metrics_recorder_ = base::ComponentContextForProcess() |
| 72 | ->svc() |
| 73 | ->Connect<fuchsia::legacymetrics::MetricsRecorder>(); |
| 74 | metrics_recorder_.set_error_handler(fit::bind_member( |
| 75 | this, &LegacyMetricsClient::OnMetricsRecorderDisconnected)); |
| 76 | metrics_recorder_.events().OnCloseSoon = |
| 77 | fit::bind_member(this, &LegacyMetricsClient::OnCloseSoon); |
| 78 | ScheduleNextReport(); |
| 79 | } |
| 80 | |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 81 | void LegacyMetricsClient::ScheduleNextReport() { |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 82 | DCHECK(!is_flushing_); |
| 83 | |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 84 | DVLOG(1) << "Scheduling next report in " << report_interval_.InSeconds() |
| 85 | << "seconds."; |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 86 | report_timer_.Start(FROM_HERE, report_interval_, this, |
| 87 | &LegacyMetricsClient::StartReport); |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 88 | } |
| 89 | |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 90 | void LegacyMetricsClient::StartReport() { |
| 91 | if (!report_additional_callback_) { |
| 92 | Report({}); |
| 93 | return; |
| 94 | } |
| 95 | report_additional_callback_.Run( |
| 96 | base::BindOnce(&LegacyMetricsClient::Report, weak_factory_.GetWeakPtr())); |
| 97 | } |
| 98 | |
| 99 | void LegacyMetricsClient::Report( |
| 100 | std::vector<fuchsia::legacymetrics::Event> events) { |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 101 | DVLOG(1) << __func__ << " called."; |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 102 | |
Kevin Marshall | a6543055 | 2020-11-18 19:55:53 | [diff] [blame] | 103 | // The connection might have dropped while additional metrics were being |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 104 | // collected. Continue recording events and cache them locally in memory until |
| 105 | // connection is reestablished. |
Kevin Marshall | a6543055 | 2020-11-18 19:55:53 | [diff] [blame] | 106 | if (!metrics_recorder_) |
| 107 | return; |
| 108 | |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 109 | // Include histograms. |
| 110 | for (auto& histogram : GetLegacyMetricsDeltas()) { |
| 111 | fuchsia::legacymetrics::Event histogram_event; |
| 112 | histogram_event.set_histogram(std::move(histogram)); |
| 113 | events.push_back(std::move(histogram_event)); |
| 114 | } |
| 115 | |
| 116 | // Include user events. |
| 117 | if (user_events_recorder_->HasEvents()) { |
| 118 | for (auto& event : user_events_recorder_->TakeEvents()) { |
| 119 | fuchsia::legacymetrics::Event user_event; |
| 120 | user_event.set_user_action_event(std::move(event)); |
| 121 | events.push_back(std::move(user_event)); |
| 122 | } |
| 123 | } |
| 124 | |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 125 | std::move(events.begin(), events.end(), std::back_inserter(to_send_)); |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 126 | |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 127 | DrainBuffer(); |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 128 | } |
| 129 | |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 130 | void LegacyMetricsClient::DrainBuffer() { |
| 131 | DVLOG(1) << __func__ << " called."; |
| 132 | |
| 133 | if (record_ack_pending_) { |
| 134 | // There is a Record() call already inflight. When it is acknowledged, |
| 135 | // buffer draining will continue. |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 139 | if (to_send_.empty()) { |
| 140 | DVLOG(1) << "Buffer drained."; |
| 141 | |
| 142 | if (is_flushing_) { |
| 143 | metrics_recorder_.Unbind(); |
Hai Bi | d0224f9 | 2020-10-19 23:47:48 | [diff] [blame] | 144 | std::move(on_flush_complete_).Run(); |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 145 | } else { |
| 146 | ScheduleNextReport(); |
| 147 | } |
| 148 | |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | // Since ordering doesn't matter, we can efficiently drain |to_send_| by |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 153 | // repeatedly sending and truncating its tail. |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 154 | const size_t batch_size = std::min(to_send_.size(), kMaxBatchSize); |
| 155 | const size_t batch_start_idx = to_send_.size() - batch_size; |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 156 | std::vector<fuchsia::legacymetrics::Event> batch; |
| 157 | batch.resize(batch_size); |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 158 | std::move(to_send_.begin() + batch_start_idx, to_send_.end(), batch.begin()); |
| 159 | to_send_.resize(to_send_.size() - batch_size); |
Kevin Marshall | ebe60c4 | 2020-04-14 22:01:24 | [diff] [blame] | 160 | |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 161 | record_ack_pending_ = true; |
| 162 | metrics_recorder_->Record(std::move(batch), [this]() { |
| 163 | record_ack_pending_ = false; |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 164 | |
| 165 | // Reset the reconnect delay after a successful Record() call. |
| 166 | reconnect_delay_ = kInitialReconnectDelay; |
| 167 | |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 168 | DrainBuffer(); |
| 169 | }); |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void LegacyMetricsClient::OnMetricsRecorderDisconnected(zx_status_t status) { |
Wez | 1f80976 | 2020-06-11 19:08:44 | [diff] [blame] | 173 | ZX_LOG(ERROR, status) << "MetricsRecorder connection lost."; |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 174 | |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 175 | // Stop reporting metric events. |
| 176 | report_timer_.AbandonAndStop(); |
| 177 | |
| 178 | if (status == ZX_ERR_PEER_CLOSED) { |
| 179 | DVLOG(1) << "Scheduling reconnect after " << reconnect_delay_; |
| 180 | |
| 181 | // Try to reconnect with exponential backoff. |
| 182 | reconnect_timer_.Start(FROM_HERE, reconnect_delay_, this, |
| 183 | &LegacyMetricsClient::ConnectAndStartReporting); |
| 184 | |
| 185 | // Increase delay exponentially. No random jittering since we don't expect |
| 186 | // many clients overloading the service with simultaneous reconnections. |
| 187 | reconnect_delay_ = std::min(reconnect_delay_ * kReconnectBackoffFactor, |
| 188 | kMaxReconnectDelay); |
| 189 | } |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 190 | } |
| 191 | |
Hai Bi | d0224f9 | 2020-10-19 23:47:48 | [diff] [blame] | 192 | void LegacyMetricsClient::FlushAndDisconnect( |
| 193 | base::OnceClosure on_flush_complete) { |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 194 | DVLOG(1) << __func__ << " called."; |
Hai Bi | d0224f9 | 2020-10-19 23:47:48 | [diff] [blame] | 195 | DCHECK(on_flush_complete); |
| 196 | if (is_flushing_) |
| 197 | return; |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 198 | |
Hai Bi | d0224f9 | 2020-10-19 23:47:48 | [diff] [blame] | 199 | on_flush_complete_ = std::move(on_flush_complete); |
Akira Baruah | 7b75418b | 2020-12-11 01:35:09 | [diff] [blame] | 200 | report_timer_.AbandonAndStop(); |
Kevin Marshall | aa7f516 | 2020-06-04 23:32:31 | [diff] [blame] | 201 | |
| 202 | is_flushing_ = true; |
| 203 | if (notify_flush_callback_) { |
| 204 | // Defer reporting until the flush operation has finished. |
| 205 | std::move(notify_flush_callback_) |
| 206 | .Run(base::BindOnce(&LegacyMetricsClient::StartReport, |
| 207 | weak_factory_.GetWeakPtr())); |
| 208 | } else { |
| 209 | StartReport(); |
| 210 | } |
| 211 | } |
| 212 | |
Hai Bi | d0224f9 | 2020-10-19 23:47:48 | [diff] [blame] | 213 | void LegacyMetricsClient::OnCloseSoon() { |
| 214 | FlushAndDisconnect(base::DoNothing::Once()); |
| 215 | } |
| 216 | |
Kevin Marshall | 05e29bd | 2020-03-19 21:55:44 | [diff] [blame] | 217 | } // namespace cr_fuchsia |