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/metrics/metrics_service_client.h"
15 #include "components/prefs/pref_registry_simple.h"
16 #include "components/ukm/persisted_logs_metrics_impl.h"
17 #include "components/ukm/ukm_pref_names.h"
18 #include "components/ukm/ukm_service.h"
24 // The UKM server's URL.
25 constexpr char kMimeType[] = "application/vnd.chrome.ukm";
27 // The number of UKM logs that will be stored in PersistedLogs before logs
28 // start being dropped.
29 constexpr int kMinPersistedLogs = 8;
31 // The number of bytes UKM logs that will be stored in PersistedLogs before
32 // logs start being dropped.
33 // This ensures that a reasonable amount of history will be stored even if there
34 // is a long series of very small logs.
35 constexpr int kMinPersistedBytes = 300000;
37 // If an upload fails, and the transmission was over this byte count, then we
38 // will discard the log, and not try to retransmit it. We also don't persist
39 // the log to the prefs for transmission during the next chrome session if this
41 constexpr size_t kMaxLogRetransmitSize = 100 * 1024;
43 std::string GetServerUrl() {
44 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm";
45 std::string server_url =
46 base::GetFieldTrialParamValueByFeature(kUkmFeature, "ServerUrl");
47 if (!server_url.empty())
49 return kDefaultServerUrl;
55 void UkmReportingService::RegisterPrefs(PrefRegistrySimple* registry) {
56 registry->RegisterListPref(prefs::kUkmPersistedLogs);
57 // Base class already registered by MetricsReportingService::RegisterPrefs
58 // ReportingService::RegisterPrefs(registry);
61 UkmReportingService::UkmReportingService(metrics::MetricsServiceClient* client,
62 PrefService* local_state)
63 : ReportingService(client, local_state, kMaxLogRetransmitSize),
64 persisted_logs_(std::make_unique<ukm::PersistedLogsMetricsImpl>(),
66 prefs::kUkmPersistedLogs,
69 kMaxLogRetransmitSize,
70 client->GetUploadSigningKey()) {}
72 UkmReportingService::~UkmReportingService() {}
74 metrics::LogStore* UkmReportingService::log_store() {
75 return &persisted_logs_;
78 std::string UkmReportingService::GetUploadUrl() const {
79 return GetServerUrl();
82 std::string UkmReportingService::GetInsecureUploadUrl() const {
86 base::StringPiece UkmReportingService::upload_mime_type() const {
90 metrics::MetricsLogUploader::MetricServiceType
91 UkmReportingService::service_type() const {
92 return metrics::MetricsLogUploader::UKM;
95 void UkmReportingService::LogCellularConstraint(bool upload_canceled) {
96 UMA_HISTOGRAM_BOOLEAN("UKM.LogUpload.Canceled.CellularConstraint",
100 void UkmReportingService::LogResponseOrErrorCode(int response_code,
103 // |was_https| is ignored since all UKM logs are received over HTTPS.
104 base::UmaHistogramSparse("UKM.LogUpload.ResponseOrErrorCode",
105 response_code >= 0 ? response_code : error_code);
108 void UkmReportingService::LogSuccess(size_t log_size) {
109 UMA_HISTOGRAM_COUNTS_10000("UKM.LogSize.OnSuccess", log_size / 1024);
112 void UkmReportingService::LogLargeRejection(size_t log_size) {}
114 } // namespace metrics