Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chrome/browser/metrics/metrics_service.cc

Issue 9232071: Upload UMA data using protocol buffers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Never re-upload protobuf logs Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 //------------------------------------------------------------------------------ 5 //------------------------------------------------------------------------------
6 // Description of the life cycle of a instance of MetricsService. 6 // Description of the life cycle of a instance of MetricsService.
7 // 7 //
8 // OVERVIEW 8 // OVERVIEW
9 // 9 //
10 // A MetricsService instance is typically created at application startup. It 10 // A MetricsService instance is typically created at application startup. It
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 #include "chrome/browser/chromeos/cros/cros_library.h" 193 #include "chrome/browser/chromeos/cros/cros_library.h"
194 #include "chrome/browser/chromeos/external_metrics.h" 194 #include "chrome/browser/chromeos/external_metrics.h"
195 #include "chrome/browser/chromeos/system/statistics_provider.h" 195 #include "chrome/browser/chromeos/system/statistics_provider.h"
196 #endif 196 #endif
197 197
198 using base::Time; 198 using base::Time;
199 using content::BrowserThread; 199 using content::BrowserThread;
200 using content::ChildProcessData; 200 using content::ChildProcessData;
201 using content::PluginService; 201 using content::PluginService;
202 202
203 namespace {
204
203 // Check to see that we're being called on only one thread. 205 // Check to see that we're being called on only one thread.
204 static bool IsSingleThreaded(); 206 bool IsSingleThreaded() {
207 static base::PlatformThreadId thread_id = 0;
208 if (!thread_id)
209 thread_id = base::PlatformThread::CurrentId();
210 return base::PlatformThread::CurrentId() == thread_id;
211 }
205 212
206 static const char kMetricsType[] = "application/vnd.mozilla.metrics.bz2"; 213 const char kMetricsTypeXml[] = "application/vnd.mozilla.metrics.bz2";
214 // TODO(isherman): What should the MIME type be?
215 const char kMetricsTypeProto[] = "application/vnd.chrome.uma";
216
217 const char kServerUrlXml[] =
218 "https://clients4.google.com/firefox/metrics/collect";
219 const char kServerUrlProto[] = "https://clients4.google.com/uma/v2";
207 220
208 // The delay, in seconds, after starting recording before doing expensive 221 // The delay, in seconds, after starting recording before doing expensive
209 // initialization work. 222 // initialization work.
210 static const int kInitializationDelaySeconds = 30; 223 const int kInitializationDelaySeconds = 30;
211 224
212 // This specifies the amount of time to wait for all renderers to send their 225 // This specifies the amount of time to wait for all renderers to send their
213 // data. 226 // data.
214 static const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds. 227 const int kMaxHistogramGatheringWaitDuration = 60000; // 60 seconds.
215 228
216 // The maximum number of events in a log uploaded to the UMA server. 229 // The maximum number of events in a log uploaded to the UMA server.
217 static const int kEventLimit = 2400; 230 const int kEventLimit = 2400;
218 231
219 // If an upload fails, and the transmission was over this byte count, then we 232 // If an upload fails, and the transmission was over this byte count, then we
220 // will discard the log, and not try to retransmit it. We also don't persist 233 // will discard the log, and not try to retransmit it. We also don't persist
221 // the log to the prefs for transmission during the next chrome session if this 234 // the log to the prefs for transmission during the next chrome session if this
222 // limit is exceeded. 235 // limit is exceeded.
223 static const int kUploadLogAvoidRetransmitSize = 50000; 236 const size_t kUploadLogAvoidRetransmitSize = 50000;
224 237
225 // Interval, in minutes, between state saves. 238 // Interval, in minutes, between state saves.
226 static const int kSaveStateIntervalMinutes = 5; 239 const int kSaveStateIntervalMinutes = 5;
240
241 }
227 242
228 // static 243 // static
229 MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ = 244 MetricsService::ShutdownCleanliness MetricsService::clean_shutdown_status_ =
230 MetricsService::CLEANLY_SHUTDOWN; 245 MetricsService::CLEANLY_SHUTDOWN;
231 246
232 // This is used to quickly log stats from child process related notifications in 247 // This is used to quickly log stats from child process related notifications in
233 // MetricsService::child_stats_buffer_. The buffer's contents are transferred 248 // MetricsService::child_stats_buffer_. The buffer's contents are transferred
234 // out when Local State is periodically saved. The information is then 249 // out when Local State is periodically saved. The information is then
235 // reported to the UMA server on next launch. 250 // reported to the UMA server on next launch.
236 struct MetricsService::ChildProcessStats { 251 struct MetricsService::ChildProcessStats {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 0); 331 0);
317 #endif // OS_CHROMEOS 332 #endif // OS_CHROMEOS
318 333
319 local_state->RegisterDictionaryPref(prefs::kProfileMetrics); 334 local_state->RegisterDictionaryPref(prefs::kProfileMetrics);
320 local_state->RegisterIntegerPref(prefs::kNumBookmarksOnBookmarkBar, 0); 335 local_state->RegisterIntegerPref(prefs::kNumBookmarksOnBookmarkBar, 0);
321 local_state->RegisterIntegerPref(prefs::kNumFoldersOnBookmarkBar, 0); 336 local_state->RegisterIntegerPref(prefs::kNumFoldersOnBookmarkBar, 0);
322 local_state->RegisterIntegerPref(prefs::kNumBookmarksInOtherBookmarkFolder, 337 local_state->RegisterIntegerPref(prefs::kNumBookmarksInOtherBookmarkFolder,
323 0); 338 0);
324 local_state->RegisterIntegerPref(prefs::kNumFoldersInOtherBookmarkFolder, 0); 339 local_state->RegisterIntegerPref(prefs::kNumFoldersInOtherBookmarkFolder, 0);
325 local_state->RegisterIntegerPref(prefs::kNumKeywords, 0); 340 local_state->RegisterIntegerPref(prefs::kNumKeywords, 0);
326 local_state->RegisterListPref(prefs::kMetricsInitialLogs); 341 local_state->RegisterListPref(prefs::kMetricsInitialLogsXml);
327 local_state->RegisterListPref(prefs::kMetricsOngoingLogs); 342 local_state->RegisterListPref(prefs::kMetricsOngoingLogsXml);
343 local_state->RegisterListPref(prefs::kMetricsInitialLogsProto);
344 local_state->RegisterListPref(prefs::kMetricsOngoingLogsProto);
328 345
329 local_state->RegisterInt64Pref(prefs::kUninstallMetricsPageLoadCount, 0); 346 local_state->RegisterInt64Pref(prefs::kUninstallMetricsPageLoadCount, 0);
330 local_state->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); 347 local_state->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0);
331 local_state->RegisterInt64Pref(prefs::kUninstallMetricsInstallDate, 0); 348 local_state->RegisterInt64Pref(prefs::kUninstallMetricsInstallDate, 0);
332 local_state->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); 349 local_state->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0);
333 local_state->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); 350 local_state->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0);
334 local_state->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); 351 local_state->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0);
335 } 352 }
336 353
337 // static 354 // static
(...skipping 12 matching lines...) Expand all
350 367
351 local_state->SetInteger(prefs::kStabilityPageLoadCount, 0); 368 local_state->SetInteger(prefs::kStabilityPageLoadCount, 0);
352 local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0); 369 local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0);
353 local_state->SetInteger(prefs::kStabilityRendererHangCount, 0); 370 local_state->SetInteger(prefs::kStabilityRendererHangCount, 0);
354 371
355 local_state->SetInt64(prefs::kStabilityLaunchTimeSec, 0); 372 local_state->SetInt64(prefs::kStabilityLaunchTimeSec, 0);
356 local_state->SetInt64(prefs::kStabilityLastTimestampSec, 0); 373 local_state->SetInt64(prefs::kStabilityLastTimestampSec, 0);
357 374
358 local_state->ClearPref(prefs::kStabilityPluginStats); 375 local_state->ClearPref(prefs::kStabilityPluginStats);
359 376
360 local_state->ClearPref(prefs::kMetricsInitialLogs); 377 local_state->ClearPref(prefs::kMetricsInitialLogsXml);
361 local_state->ClearPref(prefs::kMetricsOngoingLogs); 378 local_state->ClearPref(prefs::kMetricsOngoingLogsXml);
379 local_state->ClearPref(prefs::kMetricsInitialLogsProto);
380 local_state->ClearPref(prefs::kMetricsOngoingLogsProto);
362 } 381 }
363 382
364 MetricsService::MetricsService() 383 MetricsService::MetricsService()
365 : recording_active_(false), 384 : recording_active_(false),
366 reporting_active_(false), 385 reporting_active_(false),
367 state_(INITIALIZED), 386 state_(INITIALIZED),
368 current_fetch_(NULL),
369 io_thread_(NULL), 387 io_thread_(NULL),
370 idle_since_last_transmission_(false), 388 idle_since_last_transmission_(false),
371 next_window_id_(0), 389 next_window_id_(0),
372 ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)), 390 ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)),
373 ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)), 391 ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)),
374 waiting_for_asynchronus_reporting_step_(false) { 392 waiting_for_asynchronus_reporting_step_(false) {
375 DCHECK(IsSingleThreaded()); 393 DCHECK(IsSingleThreaded());
376 InitializeMetricsState(); 394 InitializeMetricsState();
377 395
378 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload, 396 base::Closure callback = base::Bind(&MetricsService::StartScheduledUpload,
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 //------------------------------------------------------------------------------ 639 //------------------------------------------------------------------------------
622 // private methods 640 // private methods
623 //------------------------------------------------------------------------------ 641 //------------------------------------------------------------------------------
624 642
625 643
626 //------------------------------------------------------------------------------ 644 //------------------------------------------------------------------------------
627 // Initialization methods 645 // Initialization methods
628 646
629 void MetricsService::InitializeMetricsState() { 647 void MetricsService::InitializeMetricsState() {
630 #if defined(OS_POSIX) 648 #if defined(OS_POSIX)
631 server_url_ = L"https://clients4.google.com/firefox/metrics/collect"; 649 server_url_xml_ = ASCIIToUTF16(kServerUrlXml);
650 server_url_proto_ = ASCIIToUTF16(kServerUrlProto);
632 network_stats_server_ = "chrome.googleechotest.com"; 651 network_stats_server_ = "chrome.googleechotest.com";
633 #else 652 #else
634 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 653 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
635 server_url_ = dist->GetStatsServerURL(); 654 server_url_xml_ = dist->GetStatsServerURL();
655 // TODO(isherman): Hmm, do distribution channels sometimes specify other
656 // servers?
657 server_url_proto_ = ASCIIToUTF16(kServerUrlProto);
636 network_stats_server_ = dist->GetNetworkStatsServer(); 658 network_stats_server_ = dist->GetNetworkStatsServer();
637 #endif 659 #endif
638 660
639 PrefService* pref = g_browser_process->local_state(); 661 PrefService* pref = g_browser_process->local_state();
640 DCHECK(pref); 662 DCHECK(pref);
641 663
642 if ((pref->GetInt64(prefs::kStabilityStatsBuildTime) 664 if ((pref->GetInt64(prefs::kStabilityStatsBuildTime)
643 != MetricsLog::GetBuildTime()) || 665 != MetricsLog::GetBuildTime()) ||
644 (pref->GetString(prefs::kStabilityStatsVersion) 666 (pref->GetString(prefs::kStabilityStatsVersion)
645 != MetricsLog::GetVersionString())) { 667 != MetricsLog::GetVersionString())) {
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
832 // Adds to ongoing logs. 854 // Adds to ongoing logs.
833 log_manager_.current_log()->set_hardware_class(hardware_class_); 855 log_manager_.current_log()->set_hardware_class(hardware_class_);
834 856
835 // Put incremental data (histogram deltas, and realtime stats deltas) at the 857 // Put incremental data (histogram deltas, and realtime stats deltas) at the
836 // end of all log transmissions (initial log handles this separately). 858 // end of all log transmissions (initial log handles this separately).
837 // RecordIncrementalStabilityElements only exists on the derived 859 // RecordIncrementalStabilityElements only exists on the derived
838 // MetricsLog class. 860 // MetricsLog class.
839 MetricsLog* current_log = 861 MetricsLog* current_log =
840 static_cast<MetricsLog*>(log_manager_.current_log()); 862 static_cast<MetricsLog*>(log_manager_.current_log());
841 DCHECK(current_log); 863 DCHECK(current_log);
842 current_log->RecordIncrementalStabilityElements(); 864 current_log->RecordIncrementalStabilityElements(plugins_);
843 RecordCurrentHistograms(); 865 RecordCurrentHistograms();
844 866
845 log_manager_.StageCurrentLogForUpload(); 867 log_manager_.StageCurrentLogForUpload();
846 } 868 }
847 869
848 void MetricsService::PushPendingLogsToPersistentStorage() { 870 void MetricsService::PushPendingLogsToPersistentStorage() {
849 if (state_ < INITIAL_LOG_READY) 871 if (state_ < INITIAL_LOG_READY)
850 return; // We didn't and still don't have time to get plugin list etc. 872 return; // We didn't and still don't have time to get plugin list etc.
851 873
852 if (log_manager_.has_staged_log()) { 874 if (log_manager_.has_staged_log()) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 MessageLoop::current(), callback, 949 MessageLoop::current(), callback,
928 kMaxHistogramGatheringWaitDuration); 950 kMaxHistogramGatheringWaitDuration);
929 } 951 }
930 952
931 void MetricsService::OnHistogramSynchronizationDone() { 953 void MetricsService::OnHistogramSynchronizationDone() {
932 DCHECK(IsSingleThreaded()); 954 DCHECK(IsSingleThreaded());
933 955
934 // If somehow there is a fetch in progress, we return and hope things work 956 // If somehow there is a fetch in progress, we return and hope things work
935 // out. The scheduler isn't informed since if this happens, the scheduler 957 // out. The scheduler isn't informed since if this happens, the scheduler
936 // will get a response from the upload. 958 // will get a response from the upload.
937 DCHECK(!current_fetch_.get()); 959 DCHECK(!current_fetch_xml_.get());
938 if (current_fetch_.get()) 960 DCHECK(!current_fetch_proto_.get());
961 if (current_fetch_xml_.get() || current_fetch_proto_.get())
939 return; 962 return;
940 963
941 // This function should only be called as the callback from an ansynchronous 964 // This function should only be called as the callback from an ansynchronous
942 // step. 965 // step.
943 DCHECK(waiting_for_asynchronus_reporting_step_); 966 DCHECK(waiting_for_asynchronus_reporting_step_);
944 waiting_for_asynchronus_reporting_step_ = false; 967 waiting_for_asynchronus_reporting_step_ = false;
945 968
946 // If we're getting no notifications, then the log won't have much in it, and 969 // If we're getting no notifications, then the log won't have much in it, and
947 // it's possible the computer is about to go to sleep, so don't upload and 970 // it's possible the computer is about to go to sleep, so don't upload and
948 // stop the scheduler. 971 // stop the scheduler.
949 // Similarly, if logs should no longer be uploaded, stop here. 972 // Similarly, if logs should no longer be uploaded, stop here.
950 if (idle_since_last_transmission_ || 973 if (idle_since_last_transmission_ ||
951 !recording_active() || !reporting_active()) { 974 !recording_active() || !reporting_active()) {
952 scheduler_->Stop(); 975 scheduler_->Stop();
953 scheduler_->UploadCancelled(); 976 scheduler_->UploadCancelled();
954 return; 977 return;
955 } 978 }
956 979
957 MakeStagedLog(); 980 MakeStagedLog();
958 981
959 // MakeStagedLog should have prepared log text; if it didn't, skip this 982 // MakeStagedLog should have prepared log text; if it didn't, skip this
960 // upload and hope things work out next time. 983 // upload and hope things work out next time.
961 if (log_manager_.staged_log_text().empty()) { 984 if (log_manager_.staged_log_text().empty()) {
962 scheduler_->UploadCancelled(); 985 scheduler_->UploadCancelled();
963 return; 986 return;
964 } 987 }
965 988
966 PrepareFetchWithStagedLog(); 989 PrepareFetchWithStagedLog();
967 990
968 if (!current_fetch_.get()) { 991 if (!current_fetch_xml_.get()) {
992 DCHECK(!current_fetch_proto_.get());
969 // Compression failed, and log discarded :-/. 993 // Compression failed, and log discarded :-/.
970 log_manager_.DiscardStagedLog(); 994 log_manager_.DiscardStagedLog();
971 scheduler_->UploadCancelled(); 995 scheduler_->UploadCancelled();
972 // TODO(jar): If compression failed, we should have created a tiny log and 996 // TODO(jar): If compression failed, we should have created a tiny log and
973 // compressed that, so that we can signal that we're losing logs. 997 // compressed that, so that we can signal that we're losing logs.
974 return; 998 return;
975 } 999 }
1000 DCHECK(current_fetch_proto_.get() || !log_manager_.has_staged_log_proto());
jar (doing other things) 2012/02/27 20:35:34 Please add a comment that (currently) the staged l
Ilya Sherman 2012/02/28 00:23:10 Done.
976 1001
977 DCHECK(!waiting_for_asynchronus_reporting_step_); 1002 DCHECK(!waiting_for_asynchronus_reporting_step_);
978 1003
979 waiting_for_asynchronus_reporting_step_ = true; 1004 waiting_for_asynchronus_reporting_step_ = true;
980 current_fetch_->Start(); 1005 current_fetch_xml_->Start();
1006 if (current_fetch_proto_.get())
1007 current_fetch_proto_->Start();
981 1008
982 HandleIdleSinceLastTransmission(true); 1009 HandleIdleSinceLastTransmission(true);
983 } 1010 }
984 1011
985 1012
986 void MetricsService::MakeStagedLog() { 1013 void MetricsService::MakeStagedLog() {
987 if (log_manager_.has_staged_log()) 1014 if (log_manager_.has_staged_log())
988 return; 1015 return;
989 1016
990 switch (state_) { 1017 switch (state_) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1044 1071
1045 void MetricsService::StoreUnsentLogs() { 1072 void MetricsService::StoreUnsentLogs() {
1046 if (state_ < INITIAL_LOG_READY) 1073 if (state_ < INITIAL_LOG_READY)
1047 return; // We never Recalled the prior unsent logs. 1074 return; // We never Recalled the prior unsent logs.
1048 1075
1049 log_manager_.PersistUnsentLogs(); 1076 log_manager_.PersistUnsentLogs();
1050 } 1077 }
1051 1078
1052 void MetricsService::PrepareFetchWithStagedLog() { 1079 void MetricsService::PrepareFetchWithStagedLog() {
1053 DCHECK(!log_manager_.staged_log_text().empty()); 1080 DCHECK(!log_manager_.staged_log_text().empty());
1054 DCHECK(!current_fetch_.get());
1055 1081
1056 current_fetch_.reset(content::URLFetcher::Create( 1082 // Prepare the XML version.
1057 GURL(WideToUTF16(server_url_)), content::URLFetcher::POST, this)); 1083 DCHECK(!current_fetch_xml_.get());
1058 current_fetch_->SetRequestContext( 1084 current_fetch_xml_.reset(content::URLFetcher::Create(
1085 GURL(server_url_xml_), content::URLFetcher::POST, this));
jar (doing other things) 2012/02/27 20:35:34 Is this where we need to add the TODO() to avoid s
Ilya Sherman 2012/02/28 00:23:10 Yep. In fact... I'll just go ahead and strip off
1086 current_fetch_xml_->SetRequestContext(
1059 g_browser_process->system_request_context()); 1087 g_browser_process->system_request_context());
1060 current_fetch_->SetUploadData(kMetricsType, log_manager_.staged_log_text()); 1088 current_fetch_xml_->SetUploadData(kMetricsTypeXml,
1089 log_manager_.staged_log_text().xml);
1090
1091 // Prepare the protobuf version.
1092 DCHECK(!current_fetch_proto_.get());
1093 if (log_manager_.has_staged_log_proto()) {
1094 current_fetch_proto_.reset(content::URLFetcher::Create(
1095 GURL(server_url_proto_), content::URLFetcher::POST, this));
1096 current_fetch_proto_->SetRequestContext(
1097 g_browser_process->system_request_context());
1098 current_fetch_proto_->SetUploadData(kMetricsTypeProto,
1099 log_manager_.staged_log_text().proto);
1100
1101 // Discard the protobuf version of the staged log, so that we will avoid
1102 // re-uploading it even if we need to re-upload the XML version.
1103 log_manager_.DiscardStagedLogProto();
jar (doing other things) 2012/02/27 20:35:34 This might be another place to leave a TODO() trai
Ilya Sherman 2012/02/28 00:23:10 Done.
1104 }
1061 } 1105 }
1062 1106
1063 static const char* StatusToString(const net::URLRequestStatus& status) { 1107 static const char* StatusToString(const net::URLRequestStatus& status) {
1064 switch (status.status()) { 1108 switch (status.status()) {
1065 case net::URLRequestStatus::SUCCESS: 1109 case net::URLRequestStatus::SUCCESS:
1066 return "SUCCESS"; 1110 return "SUCCESS";
1067 1111
1068 case net::URLRequestStatus::IO_PENDING: 1112 case net::URLRequestStatus::IO_PENDING:
1069 return "IO_PENDING"; 1113 return "IO_PENDING";
1070 1114
1071 case net::URLRequestStatus::HANDLED_EXTERNALLY: 1115 case net::URLRequestStatus::HANDLED_EXTERNALLY:
1072 return "HANDLED_EXTERNALLY"; 1116 return "HANDLED_EXTERNALLY";
1073 1117
1074 case net::URLRequestStatus::CANCELED: 1118 case net::URLRequestStatus::CANCELED:
1075 return "CANCELED"; 1119 return "CANCELED";
1076 1120
1077 case net::URLRequestStatus::FAILED: 1121 case net::URLRequestStatus::FAILED:
1078 return "FAILED"; 1122 return "FAILED";
1079 1123
1080 default: 1124 default:
1081 NOTREACHED(); 1125 NOTREACHED();
1082 return "Unknown"; 1126 return "Unknown";
1083 } 1127 }
1084 } 1128 }
1085 1129
1130 // We need to wait for two responses: the response to the XML upload, and the
1131 // response to the protobuf upload. For now, only the XML upload's response
1132 // affects decisions like whether to retry the upload, whether to abandon the
1133 // upload because it is too large, etc. However, we still need to wait for the
1134 // protobuf upload, as we cannot reset |current_fetch_proto_| until we have
1135 // confirmation that the network request was sent; and the easiest way to do
1136 // that is to wait for the response. In case the XML upload's response arrives
1137 // first, we cache that response until the protobuf upload's response also
1138 // arrives.
1139 //
1140 // Note that if the XML upload succeeds but the protobuf upload fails, we will
1141 // not retry the protobuf upload. If the XML upload fails while the protobuf
1142 // upload succeeds, we will still avoid re-uploading the protobuf data because
1143 // we "zap" the data after the first upload attempt. This means that we might
1144 // lose protobuf uploads when XML ones succeed; but we will never duplicate any
1145 // protobuf uploads. Protobuf failures should be rare enough to where this
1146 // should be ok while we have the two pipelines running in parallel.
1086 void MetricsService::OnURLFetchComplete(const content::URLFetcher* source) { 1147 void MetricsService::OnURLFetchComplete(const content::URLFetcher* source) {
1087 DCHECK(waiting_for_asynchronus_reporting_step_); 1148 DCHECK(waiting_for_asynchronus_reporting_step_);
1149
1150 // We're not allowed to re-use the existing |URLFetcher|s, so free them here.
1151 scoped_ptr<content::URLFetcher> s;
1152 if (source == current_fetch_xml_.get()) {
1153 s.reset(current_fetch_xml_.release());
1154
1155 // Cache the XML responses, in case we still need to wait for the protobuf
1156 // response.
1157 response_code_ = source->GetResponseCode();
1158 response_status_ = StatusToString(source->GetStatus());
1159 source->GetResponseAsString(&response_data_);
1160 } else if (source == current_fetch_proto_.get()) {
1161 s.reset(current_fetch_proto_.release());
1162 } else {
1163 NOTREACHED();
1164 return;
1165 }
1166
1167 // If we're still waiting for one of the responses, keep waiting...
1168 if (current_fetch_xml_.get() || current_fetch_proto_.get())
1169 return;
1170
1171 // We should only be able to reach here once we've received responses to both
1172 // the XML and the protobuf requests. We should always have the response code
1173 // available.
1174 DCHECK_NE(response_code_, content::URLFetcher::RESPONSE_CODE_INVALID);
1088 waiting_for_asynchronus_reporting_step_ = false; 1175 waiting_for_asynchronus_reporting_step_ = false;
1089 DCHECK(current_fetch_.get()); 1176
1090 // We're not allowed to re-use it. Delete it on function exit since we use it.
1091 scoped_ptr<content::URLFetcher> s(current_fetch_.release());
1092 1177
1093 // Confirm send so that we can move on. 1178 // Confirm send so that we can move on.
1094 VLOG(1) << "METRICS RESPONSE CODE: " << source->GetResponseCode() 1179 VLOG(1) << "METRICS RESPONSE CODE: " << response_code_
1095 << " status=" << StatusToString(source->GetStatus()); 1180 << " status=" << response_status_;
1096 1181
1097 bool upload_succeeded = source->GetResponseCode() == 200; 1182 bool upload_succeeded = response_code_ == 200;
1098 1183
1099 // Provide boolean for error recovery (allow us to ignore response_code). 1184 // Provide boolean for error recovery (allow us to ignore response_code).
1100 bool discard_log = false; 1185 bool discard_log = false;
1101 1186
1102 if (!upload_succeeded && 1187 if (!upload_succeeded &&
1103 (log_manager_.staged_log_text().length() > 1188 log_manager_.staged_log_text().xml.length() >
1104 static_cast<size_t>(kUploadLogAvoidRetransmitSize))) { 1189 kUploadLogAvoidRetransmitSize) {
1105 UMA_HISTOGRAM_COUNTS( 1190 UMA_HISTOGRAM_COUNTS(
1106 "UMA.Large Rejected Log was Discarded", 1191 "UMA.Large Rejected Log was Discarded",
1107 static_cast<int>(log_manager_.staged_log_text().length())); 1192 static_cast<int>(log_manager_.staged_log_text().xml.length()));
1108 discard_log = true; 1193 discard_log = true;
1109 } else if (source->GetResponseCode() == 400) { 1194 } else if (response_code_ == 400) {
1110 // Bad syntax. Retransmission won't work. 1195 // Bad syntax. Retransmission won't work.
1111 UMA_HISTOGRAM_COUNTS("UMA.Unacceptable_Log_Discarded", state_); 1196 UMA_HISTOGRAM_COUNTS("UMA.Unacceptable_Log_Discarded", state_);
1112 discard_log = true; 1197 discard_log = true;
1113 } 1198 }
1114 1199
1115 if (!upload_succeeded && !discard_log) { 1200 if (!upload_succeeded && !discard_log) {
1116 VLOG(1) << "METRICS: transmission attempt returned a failure code: " 1201 VLOG(1) << "METRICS: transmission attempt returned a failure code: "
1117 << source->GetResponseCode() << ". Verify network connectivity"; 1202 << response_code_ << ". Verify network connectivity";
1118 LogBadResponseCode(); 1203 LogBadResponseCode();
1119 } else { // Successful receipt (or we are discarding log). 1204 } else { // Successful receipt (or we are discarding log).
1120 std::string data; 1205 VLOG(1) << "METRICS RESPONSE DATA: " << response_data_;
1121 source->GetResponseAsString(&data);
1122 VLOG(1) << "METRICS RESPONSE DATA: " << data;
1123 switch (state_) { 1206 switch (state_) {
1124 case INITIAL_LOG_READY: 1207 case INITIAL_LOG_READY:
1125 state_ = SENDING_OLD_LOGS; 1208 state_ = SENDING_OLD_LOGS;
1126 break; 1209 break;
1127 1210
1128 case SENDING_OLD_LOGS: 1211 case SENDING_OLD_LOGS:
1129 // Store the updated list to disk now that the removed log is uploaded. 1212 // Store the updated list to disk now that the removed log is uploaded.
1130 StoreUnsentLogs(); 1213 StoreUnsentLogs();
1131 break; 1214 break;
1132 1215
1133 case SENDING_CURRENT_LOGS: 1216 case SENDING_CURRENT_LOGS:
1134 break; 1217 break;
1135 1218
1136 default: 1219 default:
1137 NOTREACHED(); 1220 NOTREACHED();
1138 break; 1221 break;
1139 } 1222 }
1140 1223
1141 log_manager_.DiscardStagedLog(); 1224 log_manager_.DiscardStagedLog();
1142 1225
1143 if (log_manager_.has_unsent_logs()) 1226 if (log_manager_.has_unsent_logs())
1144 DCHECK(state_ < SENDING_CURRENT_LOGS); 1227 DCHECK(state_ < SENDING_CURRENT_LOGS);
1145 } 1228 }
1146 1229
1147 // Error 400 indicates a problem with the log, not with the server, so 1230 // Error 400 indicates a problem with the log, not with the server, so
1148 // don't consider that a sign that the server is in trouble. 1231 // don't consider that a sign that the server is in trouble.
1149 bool server_is_healthy = upload_succeeded || source->GetResponseCode() == 400; 1232 bool server_is_healthy = upload_succeeded || response_code_ == 400;
1150 1233
1151 scheduler_->UploadFinished(server_is_healthy, 1234 scheduler_->UploadFinished(server_is_healthy,
1152 log_manager_.has_unsent_logs()); 1235 log_manager_.has_unsent_logs());
1153 1236
1154 // Collect network stats if UMA upload succeeded. 1237 // Collect network stats if UMA upload succeeded.
1155 if (server_is_healthy && io_thread_) 1238 if (server_is_healthy && io_thread_)
1156 chrome_browser_net::CollectNetworkStats(network_stats_server_, io_thread_); 1239 chrome_browser_net::CollectNetworkStats(network_stats_server_, io_thread_);
1240
1241 // Reset the cached response data.
1242 response_code_ = content::URLFetcher::RESPONSE_CODE_INVALID;
1243 response_data_ = std::string();
1244 response_status_ = std::string();
1157 } 1245 }
1158 1246
1159 void MetricsService::LogBadResponseCode() { 1247 void MetricsService::LogBadResponseCode() {
1160 VLOG(1) << "Verify your metrics logs are formatted correctly. Verify server " 1248 VLOG(1) << "Verify your metrics logs are formatted correctly. Verify server "
1161 "is active at " << server_url_; 1249 "is active at " << server_url_xml_;
1162 if (!log_manager_.has_staged_log()) { 1250 if (!log_manager_.has_staged_log()) {
1163 VLOG(1) << "METRICS: Recorder shutdown during log transmission."; 1251 VLOG(1) << "METRICS: Recorder shutdown during log transmission.";
1164 } else { 1252 } else {
1165 VLOG(1) << "METRICS: transmission retry being scheduled for " 1253 VLOG(1) << "METRICS: transmission retry being scheduled for "
1166 << log_manager_.staged_log_text(); 1254 << log_manager_.staged_log_text().xml;
1167 } 1255 }
1168 } 1256 }
1169 1257
1170 void MetricsService::LogWindowChange( 1258 void MetricsService::LogWindowChange(
1171 int type, 1259 int type,
1172 const content::NotificationSource& source, 1260 const content::NotificationSource& source,
1173 const content::NotificationDetails& details) { 1261 const content::NotificationDetails& details) {
1174 int controller_id = -1; 1262 int controller_id = -1;
1175 uintptr_t window_or_tab = source.map_key(); 1263 uintptr_t window_or_tab = source.map_key();
1176 MetricsLog::WindowEventType window_type; 1264 MetricsLog::WindowEventType window_type;
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
1528 1616
1529 RecordPluginChanges(pref); 1617 RecordPluginChanges(pref);
1530 } 1618 }
1531 1619
1532 // static 1620 // static
1533 bool MetricsService::IsPluginProcess(content::ProcessType type) { 1621 bool MetricsService::IsPluginProcess(content::ProcessType type) {
1534 return (type == content::PROCESS_TYPE_PLUGIN|| 1622 return (type == content::PROCESS_TYPE_PLUGIN||
1535 type == content::PROCESS_TYPE_PPAPI_PLUGIN); 1623 type == content::PROCESS_TYPE_PPAPI_PLUGIN);
1536 } 1624 }
1537 1625
1538 static bool IsSingleThreaded() {
1539 static base::PlatformThreadId thread_id = 0;
1540 if (!thread_id)
1541 thread_id = base::PlatformThread::CurrentId();
1542 return base::PlatformThread::CurrentId() == thread_id;
1543 }
1544
1545 #if defined(OS_CHROMEOS) 1626 #if defined(OS_CHROMEOS)
1546 void MetricsService::StartExternalMetrics() { 1627 void MetricsService::StartExternalMetrics() {
1547 external_metrics_ = new chromeos::ExternalMetrics; 1628 external_metrics_ = new chromeos::ExternalMetrics;
1548 external_metrics_->Start(); 1629 external_metrics_->Start();
1549 } 1630 }
1550 #endif 1631 #endif
1551 1632
1552 // static 1633 // static
1553 bool MetricsServiceHelper::IsMetricsReportingEnabled() { 1634 bool MetricsServiceHelper::IsMetricsReportingEnabled() {
1554 bool result = false; 1635 bool result = false;
1555 const PrefService* local_state = g_browser_process->local_state(); 1636 const PrefService* local_state = g_browser_process->local_state();
1556 if (local_state) { 1637 if (local_state) {
1557 const PrefService::Preference* uma_pref = 1638 const PrefService::Preference* uma_pref =
1558 local_state->FindPreference(prefs::kMetricsReportingEnabled); 1639 local_state->FindPreference(prefs::kMetricsReportingEnabled);
1559 if (uma_pref) { 1640 if (uma_pref) {
1560 bool success = uma_pref->GetValue()->GetAsBoolean(&result); 1641 bool success = uma_pref->GetValue()->GetAsBoolean(&result);
1561 DCHECK(success); 1642 DCHECK(success);
1562 } 1643 }
1563 } 1644 }
1564 return result; 1645 return result;
1565 } 1646 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698