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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/feature_list.h" | 9 #include "base/feature_list.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/metrics/metrics_log_uploader.h" | 15 #include "components/metrics/metrics_log_uploader.h" |
| 16 #include "components/metrics/metrics_service_client.h" | 16 #include "components/metrics/metrics_service_client.h" |
| 17 #include "components/metrics/proto/ukm/report.pb.h" | 17 #include "components/metrics/proto/ukm/report.pb.h" |
| 18 #include "components/metrics/proto/ukm/source.pb.h" | |
| 18 #include "components/prefs/pref_registry_simple.h" | 19 #include "components/prefs/pref_registry_simple.h" |
| 19 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
| 20 #include "components/ukm/metrics_reporting_scheduler.h" | 21 #include "components/ukm/metrics_reporting_scheduler.h" |
| 21 #include "components/ukm/persisted_logs_metrics_impl.h" | 22 #include "components/ukm/persisted_logs_metrics_impl.h" |
| 22 #include "components/ukm/ukm_pref_names.h" | 23 #include "components/ukm/ukm_pref_names.h" |
| 24 #include "components/ukm/ukm_source.h" | |
| 23 #include "components/variations/variations_associated_data.h" | 25 #include "components/variations/variations_associated_data.h" |
| 24 | 26 |
| 25 namespace ukm { | 27 namespace ukm { |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 const base::Feature kUkmFeature = {"Ukm", base::FEATURE_DISABLED_BY_DEFAULT}; | 31 const base::Feature kUkmFeature = {"Ukm", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 30 | 32 |
| 31 const char kMimeType[] = "application/vnd.chrome.ukm"; | 33 const char kMimeType[] = "application/vnd.chrome.ukm"; |
| 32 | 34 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 52 // This ensures that a reasonable amount of history will be stored even if there | 54 // This ensures that a reasonable amount of history will be stored even if there |
| 53 // is a long series of very small logs. | 55 // is a long series of very small logs. |
| 54 const int kMinPersistedBytes = 300000; | 56 const int kMinPersistedBytes = 300000; |
| 55 | 57 |
| 56 // If an upload fails, and the transmission was over this byte count, then we | 58 // If an upload fails, and the transmission was over this byte count, then we |
| 57 // will discard the log, and not try to retransmit it. We also don't persist | 59 // will discard the log, and not try to retransmit it. We also don't persist |
| 58 // the log to the prefs for transmission during the next chrome session if this | 60 // the log to the prefs for transmission during the next chrome session if this |
| 59 // limit is exceeded. | 61 // limit is exceeded. |
| 60 const size_t kMaxLogRetransmitSize = 100 * 1024; | 62 const size_t kMaxLogRetransmitSize = 100 * 1024; |
| 61 | 63 |
| 64 // Maximum number of Sources we'll keep around before dropping new ones. | |
| 65 const size_t kMaxSourcesLength = 100; | |
| 66 | |
| 62 std::string GetServerUrl() { | 67 std::string GetServerUrl() { |
| 63 std::string server_url = variations::GetVariationParamValue( | 68 std::string server_url = variations::GetVariationParamValue( |
| 64 kUkmRolloutFieldTrialName, kUkmRolloutServerUrlParam); | 69 kUkmRolloutFieldTrialName, kUkmRolloutServerUrlParam); |
| 65 if (!server_url.empty()) | 70 if (!server_url.empty()) |
| 66 return server_url; | 71 return server_url; |
| 67 return kDefaultServerUrl; | 72 return kDefaultServerUrl; |
| 68 } | 73 } |
| 69 | 74 |
| 70 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { | 75 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { |
| 71 uint64_t client_id = pref_service->GetInteger(prefs::kUkmClientId); | 76 uint64_t client_id = pref_service->GetInteger(prefs::kUkmClientId); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 } | 173 } |
| 169 StartScheduledUpload(); | 174 StartScheduledUpload(); |
| 170 } | 175 } |
| 171 | 176 |
| 172 void UkmService::BuildAndStoreLog() { | 177 void UkmService::BuildAndStoreLog() { |
| 173 DCHECK(thread_checker_.CalledOnValidThread()); | 178 DCHECK(thread_checker_.CalledOnValidThread()); |
| 174 DVLOG(1) << "UkmService::BuildAndStoreLog"; | 179 DVLOG(1) << "UkmService::BuildAndStoreLog"; |
| 175 Report report; | 180 Report report; |
| 176 report.set_client_id(client_id_); | 181 report.set_client_id(client_id_); |
| 177 // TODO(holte): Populate system_profile. | 182 // TODO(holte): Populate system_profile. |
| 178 // TODO(zhenw): Populate sources. | 183 |
| 184 for (auto& source : sources_) { | |
|
Steven Holte
2017/01/09 18:23:39
It seems like we probably want to drop Sources we'
oystein (OOO til 10th of July)
2017/01/24 19:21:41
Done.
| |
| 185 Source* proto_source = report.add_sources(); | |
| 186 source->PopulateProto(proto_source); | |
| 187 } | |
| 188 | |
| 179 std::string serialized_log; | 189 std::string serialized_log; |
| 180 report.SerializeToString(&serialized_log); | 190 report.SerializeToString(&serialized_log); |
| 181 persisted_logs_.StoreLog(serialized_log); | 191 persisted_logs_.StoreLog(serialized_log); |
| 182 } | 192 } |
| 183 | 193 |
| 184 void UkmService::StartScheduledUpload() { | 194 void UkmService::StartScheduledUpload() { |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); | 195 DCHECK(thread_checker_.CalledOnValidThread()); |
| 186 DCHECK(!log_upload_in_progress_); | 196 DCHECK(!log_upload_in_progress_); |
| 187 persisted_logs_.StageLog(); | 197 persisted_logs_.StageLog(); |
| 188 // TODO(holte): Handle data usage on cellular, etc. | 198 // TODO(holte): Handle data usage on cellular, etc. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 // Store the updated list to disk now that the removed log is uploaded. | 235 // Store the updated list to disk now that the removed log is uploaded. |
| 226 persisted_logs_.SerializeLogs(); | 236 persisted_logs_.SerializeLogs(); |
| 227 } | 237 } |
| 228 | 238 |
| 229 // Error 400 indicates a problem with the log, not with the server, so | 239 // Error 400 indicates a problem with the log, not with the server, so |
| 230 // don't consider that a sign that the server is in trouble. | 240 // don't consider that a sign that the server is in trouble. |
| 231 bool server_is_healthy = upload_succeeded || response_code == 400; | 241 bool server_is_healthy = upload_succeeded || response_code == 400; |
| 232 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); | 242 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); |
| 233 } | 243 } |
| 234 | 244 |
| 245 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { | |
| 246 if (sources_.size() >= kMaxSourcesLength) | |
| 247 return; | |
| 248 | |
| 249 sources_.push_back(std::move(source)); | |
| 250 } | |
| 251 | |
| 235 } // namespace ukm | 252 } // namespace ukm |
| OLD | NEW |