Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ukm/ukm_service.h" | 5 #include "components/ukm/ukm_service.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/feature_list.h" | 11 #include "base/feature_list.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "components/metrics/metrics_log_uploader.h" | 18 #include "components/metrics/metrics_log_uploader.h" |
| 19 #include "components/metrics/metrics_service_client.h" | 19 #include "components/metrics/metrics_service_client.h" |
| 20 #include "components/metrics/proto/ukm/report.pb.h" | 20 #include "components/metrics/proto/ukm/report.pb.h" |
| 21 #include "components/metrics/proto/ukm/source.pb.h" | |
| 21 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
| 22 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 23 #include "components/ukm/metrics_reporting_scheduler.h" | 24 #include "components/ukm/metrics_reporting_scheduler.h" |
| 24 #include "components/ukm/persisted_logs_metrics_impl.h" | 25 #include "components/ukm/persisted_logs_metrics_impl.h" |
| 25 #include "components/ukm/ukm_pref_names.h" | 26 #include "components/ukm/ukm_pref_names.h" |
| 27 #include "components/ukm/ukm_source.h" | |
| 26 #include "components/variations/variations_associated_data.h" | 28 #include "components/variations/variations_associated_data.h" |
| 27 | 29 |
| 28 namespace ukm { | 30 namespace ukm { |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 constexpr char kMimeType[] = "application/vnd.chrome.ukm"; | 34 constexpr char kMimeType[] = "application/vnd.chrome.ukm"; |
| 33 | 35 |
| 34 // The UKM server's URL. | 36 // The UKM server's URL. |
| 35 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm"; | 37 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm"; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 47 // This ensures that a reasonable amount of history will be stored even if there | 49 // This ensures that a reasonable amount of history will be stored even if there |
| 48 // is a long series of very small logs. | 50 // is a long series of very small logs. |
| 49 constexpr int kMinPersistedBytes = 300000; | 51 constexpr int kMinPersistedBytes = 300000; |
| 50 | 52 |
| 51 // If an upload fails, and the transmission was over this byte count, then we | 53 // If an upload fails, and the transmission was over this byte count, then we |
| 52 // will discard the log, and not try to retransmit it. We also don't persist | 54 // will discard the log, and not try to retransmit it. We also don't persist |
| 53 // the log to the prefs for transmission during the next chrome session if this | 55 // the log to the prefs for transmission during the next chrome session if this |
| 54 // limit is exceeded. | 56 // limit is exceeded. |
| 55 constexpr size_t kMaxLogRetransmitSize = 100 * 1024; | 57 constexpr size_t kMaxLogRetransmitSize = 100 * 1024; |
| 56 | 58 |
| 59 // Maximum number of Sources we'll keep around before dropping new ones. | |
| 60 const size_t kMaxSourcesLength = 100; | |
|
Alexei Svitkine (slow)
2017/01/25 15:35:04
Nit: Just kMaxSources
oystein (OOO til 10th of July)
2017/01/25 18:38:04
Done.
| |
| 61 | |
| 57 std::string GetServerUrl() { | 62 std::string GetServerUrl() { |
| 58 std::string server_url = | 63 std::string server_url = |
| 59 variations::GetVariationParamValueByFeature(kUkmFeature, "ServerUrl"); | 64 variations::GetVariationParamValueByFeature(kUkmFeature, "ServerUrl"); |
| 60 if (!server_url.empty()) | 65 if (!server_url.empty()) |
| 61 return server_url; | 66 return server_url; |
| 62 return kDefaultServerUrl; | 67 return kDefaultServerUrl; |
| 63 } | 68 } |
| 64 | 69 |
| 65 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { | 70 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { |
| 66 uint64_t client_id = pref_service->GetInt64(prefs::kUkmClientId); | 71 uint64_t client_id = pref_service->GetInt64(prefs::kUkmClientId); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 | 148 |
| 144 void UkmService::Flush() { | 149 void UkmService::Flush() { |
| 145 if (initialize_complete_) | 150 if (initialize_complete_) |
| 146 BuildAndStoreLog(); | 151 BuildAndStoreLog(); |
| 147 persisted_logs_.SerializeLogs(); | 152 persisted_logs_.SerializeLogs(); |
| 148 } | 153 } |
| 149 | 154 |
| 150 void UkmService::Purge() { | 155 void UkmService::Purge() { |
| 151 DVLOG(1) << "UkmService::Purge"; | 156 DVLOG(1) << "UkmService::Purge"; |
| 152 persisted_logs_.Purge(); | 157 persisted_logs_.Purge(); |
| 153 // TODO(oystein): Delete any stored sources. | 158 sources_.clear(); |
| 154 } | 159 } |
| 155 | 160 |
| 156 // static | 161 // static |
| 157 void UkmService::RegisterPrefs(PrefRegistrySimple* registry) { | 162 void UkmService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 158 registry->RegisterInt64Pref(prefs::kUkmClientId, 0); | 163 registry->RegisterInt64Pref(prefs::kUkmClientId, 0); |
| 159 registry->RegisterListPref(prefs::kUkmPersistedLogs); | 164 registry->RegisterListPref(prefs::kUkmPersistedLogs); |
| 160 } | 165 } |
| 161 | 166 |
| 162 void UkmService::StartInitTask() { | 167 void UkmService::StartInitTask() { |
| 163 DCHECK(thread_checker_.CalledOnValidThread()); | 168 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 183 } | 188 } |
| 184 StartScheduledUpload(); | 189 StartScheduledUpload(); |
| 185 } | 190 } |
| 186 | 191 |
| 187 void UkmService::BuildAndStoreLog() { | 192 void UkmService::BuildAndStoreLog() { |
| 188 DCHECK(thread_checker_.CalledOnValidThread()); | 193 DCHECK(thread_checker_.CalledOnValidThread()); |
| 189 DVLOG(1) << "UkmService::BuildAndStoreLog"; | 194 DVLOG(1) << "UkmService::BuildAndStoreLog"; |
| 190 Report report; | 195 Report report; |
| 191 report.set_client_id(client_id_); | 196 report.set_client_id(client_id_); |
| 192 // TODO(holte): Populate system_profile. | 197 // TODO(holte): Populate system_profile. |
| 193 // TODO(oystein): Populate sources. | 198 for (auto& source : sources_) { |
|
Alexei Svitkine (slow)
2017/01/25 15:35:04
Nit: const auto&
oystein (OOO til 10th of July)
2017/01/25 18:38:04
Done.
| |
| 199 Source* proto_source = report.add_sources(); | |
| 200 source->PopulateProto(proto_source); | |
| 201 } | |
| 202 sources_.clear(); | |
| 203 | |
| 194 std::string serialized_log; | 204 std::string serialized_log; |
| 195 report.SerializeToString(&serialized_log); | 205 report.SerializeToString(&serialized_log); |
| 196 persisted_logs_.StoreLog(serialized_log); | 206 persisted_logs_.StoreLog(serialized_log); |
| 197 } | 207 } |
| 198 | 208 |
| 199 void UkmService::StartScheduledUpload() { | 209 void UkmService::StartScheduledUpload() { |
| 200 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 201 DCHECK(!log_upload_in_progress_); | 211 DCHECK(!log_upload_in_progress_); |
| 202 if (!persisted_logs_.has_staged_log()) | 212 if (!persisted_logs_.has_staged_log()) |
| 203 persisted_logs_.StageLog(); | 213 persisted_logs_.StageLog(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 240 // Store the updated list to disk now that the removed log is uploaded. | 250 // Store the updated list to disk now that the removed log is uploaded. |
| 241 persisted_logs_.SerializeLogs(); | 251 persisted_logs_.SerializeLogs(); |
| 242 } | 252 } |
| 243 | 253 |
| 244 // Error 400 indicates a problem with the log, not with the server, so | 254 // Error 400 indicates a problem with the log, not with the server, so |
| 245 // don't consider that a sign that the server is in trouble. | 255 // don't consider that a sign that the server is in trouble. |
| 246 bool server_is_healthy = upload_succeeded || response_code == 400; | 256 bool server_is_healthy = upload_succeeded || response_code == 400; |
| 247 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); | 257 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); |
| 248 } | 258 } |
| 249 | 259 |
| 260 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { | |
| 261 if (sources_.size() >= kMaxSourcesLength) | |
| 262 return; | |
| 263 | |
| 264 sources_.push_back(std::move(source)); | |
| 265 } | |
| 266 | |
| 250 } // namespace ukm | 267 } // namespace ukm |
| OLD | NEW |