Chromium Code Reviews| Index: components/metrics/metrics_reporting_service.cc |
| diff --git a/components/metrics/metrics_reporting_service.cc b/components/metrics/metrics_reporting_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dbce94218fc8c7af228b453db04492c056868873 |
| --- /dev/null |
| +++ b/components/metrics/metrics_reporting_service.cc |
| @@ -0,0 +1,107 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// ReportingService specialized to report UMA metrics. |
| + |
| +#include "components/metrics/metrics_reporting_service.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/metrics/histogram_macros.h" |
| +#include "components/metrics/metrics_pref_names.h" |
| +#include "components/metrics/persisted_logs_metrics_impl.h" |
| +#include "components/metrics/url_constants.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| + |
| +namespace metrics { |
| + |
| +namespace { |
| + |
| +// If an upload fails, and the transmission was over this byte count, then we |
| +// will discard the log, and not try to retransmit it. We also don't persist |
| +// the log to the prefs for transmission during the next chrome session if this |
| +// limit is exceeded. |
| +const size_t kUploadLogAvoidRetransmitSize = 100 * 1024; |
| + |
| +enum ResponseStatus { |
| + UNKNOWN_FAILURE, |
| + SUCCESS, |
| + BAD_REQUEST, // Invalid syntax or log too large. |
| + NO_RESPONSE, |
| + NUM_RESPONSE_STATUSES |
| +}; |
| + |
| +ResponseStatus ResponseCodeToStatus(int response_code) { |
| + switch (response_code) { |
| + case -1: |
| + return NO_RESPONSE; |
| + case 200: |
| + return SUCCESS; |
| + case 400: |
| + return BAD_REQUEST; |
| + default: |
| + return UNKNOWN_FAILURE; |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +void MetricsReportingService::RegisterPrefs(PrefRegistrySimple* registry) { |
| + ReportingService::RegisterPrefs(registry); |
| + MetricsLogStore::RegisterPrefs(registry); |
| +} |
| + |
| +MetricsReportingService::MetricsReportingService(MetricsServiceClient* client, |
| + PrefService* local_state) |
| + : ReportingService(client, local_state, kUploadLogAvoidRetransmitSize), |
| + metrics_log_store_(local_state, kUploadLogAvoidRetransmitSize) {} |
| + |
| +MetricsReportingService::~MetricsReportingService() {} |
| + |
| +LogStore* MetricsReportingService::log_store() { |
| + return &metrics_log_store_; |
| +} |
| + |
| +const std::string MetricsReportingService::upload_url() { |
| + return client()->GetMetricsServerUrl(); |
| +} |
| + |
| +const std::string MetricsReportingService::upload_mime_type() { |
| + return metrics::kDefaultMetricsMimeType; |
| +} |
| + |
| +MetricsLogUploader::MetricServiceType MetricsReportingService::service_type() { |
| + return metrics::MetricsLogUploader::UMA; |
| +} |
| + |
| +void MetricsReportingService::LogActualUploadInterval( |
| + base::TimeDelta interval) { |
| + UMA_HISTOGRAM_CUSTOM_COUNTS("UMA.ActualLogUploadInterval", |
| + interval.InMinutes(), 1, |
| + base::TimeDelta::FromHours(12).InMinutes(), 50); |
| +} |
| + |
| +void MetricsReportingService::LogCellularConstraint(bool upload_canceled) { |
| + UMA_HISTOGRAM_BOOLEAN("UMA.LogUpload.Canceled.CellularConstraint", |
| + upload_canceled); |
| +} |
| + |
| +void MetricsReportingService::LogResponseCode(int response_code) { |
| + // Log a histogram to track response success vs. failure rates. |
| + UMA_HISTOGRAM_ENUMERATION("UMA.UploadResponseStatus.Protobuf", |
| + ResponseCodeToStatus(response_code), |
| + NUM_RESPONSE_STATUSES); |
| +} |
| + |
| +void MetricsReportingService::LogSuccess(size_t log_size) { |
| + UMA_HISTOGRAM_COUNTS_10000("UMA.LogSize.OnSuccess", log_size / 1024); |
| +} |
| + |
| +void MetricsReportingService::LogLargeRejection(size_t log_size) { |
| + UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", |
|
Alexei Svitkine (slow)
2017/03/01 16:06:29
Nit: We now prefer the COUNTS_1M macro which has s
Steven Holte
2017/03/04 01:35:51
Done.
|
| + static_cast<int>(log_size)); |
| +} |
| + |
| +} // namespace metrics |