1 // Copyright 2017 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.
5 // ReportingService specialized to report UKM metrics.
7 #include "components/ukm/ukm_reporting_service.h"
11 #include "base/metrics/field_trial_params.h"
12 #include "base/metrics/histogram_functions.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "components/prefs/pref_registry_simple.h"
15 #include "components/ukm/persisted_logs_metrics_impl.h"
16 #include "components/ukm/ukm_pref_names.h"
17 #include "components/ukm/ukm_service.h"
23 // The UKM server's URL.
24 constexpr char kMimeType[] = "application/vnd.chrome.ukm";
26 // The number of UKM logs that will be stored in PersistedLogs before logs
27 // start being dropped.
28 constexpr int kMinPersistedLogs = 8;
30 // The number of bytes UKM logs that will be stored in PersistedLogs before
31 // logs start being dropped.
32 // This ensures that a reasonable amount of history will be stored even if there
33 // is a long series of very small logs.
34 constexpr int kMinPersistedBytes = 300000;
36 // If an upload fails, and the transmission was over this byte count, then we
37 // will discard the log, and not try to retransmit it. We also don't persist
38 // the log to the prefs for transmission during the next chrome session if this
40 constexpr size_t kMaxLogRetransmitSize = 100 * 1024;
42 std::string GetServerUrl() {
43 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm";
44 std::string server_url =
45 base::GetFieldTrialParamValueByFeature(kUkmFeature, "ServerUrl");
46 if (!server_url.empty())
48 return kDefaultServerUrl;
54 void UkmReportingService::RegisterPrefs(PrefRegistrySimple* registry) {
55 registry->RegisterListPref(prefs::kUkmPersistedLogs);
56 // Base class already registered by MetricsReportingService::RegisterPrefs
57 // ReportingService::RegisterPrefs(registry);
60 UkmReportingService::UkmReportingService(metrics::MetricsServiceClient* client,
61 PrefService* local_state)
62 : ReportingService(client, local_state, kMaxLogRetransmitSize),
63 persisted_logs_(std::make_unique<ukm::PersistedLogsMetricsImpl>(),
65 prefs::kUkmPersistedLogs,
68 kMaxLogRetransmitSize) {}
70 UkmReportingService::~UkmReportingService() {}
72 metrics::LogStore* UkmReportingService::log_store() {
73 return &persisted_logs_;
76 std::string UkmReportingService::GetUploadUrl() const {
77 return GetServerUrl();
80 std::string UkmReportingService::GetInsecureUploadUrl() const {
84 base::StringPiece UkmReportingService::upload_mime_type() const {
88 metrics::MetricsLogUploader::MetricServiceType
89 UkmReportingService::service_type() const {
90 return metrics::MetricsLogUploader::UKM;
93 void UkmReportingService::LogCellularConstraint(bool upload_canceled) {
94 UMA_HISTOGRAM_BOOLEAN("UKM.LogUpload.Canceled.CellularConstraint",
98 void UkmReportingService::LogResponseOrErrorCode(int response_code,
101 // |was_https| is ignored since all UKM logs are received over HTTPS.
102 base::UmaHistogramSparse("UKM.LogUpload.ResponseOrErrorCode",
103 response_code >= 0 ? response_code : error_code);
106 void UkmReportingService::LogSuccess(size_t log_size) {
107 UMA_HISTOGRAM_COUNTS_10000("UKM.LogSize.OnSuccess", log_size / 1024);
110 void UkmReportingService::LogLargeRejection(size_t log_size) {}
112 } // namespace metrics