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

Side by Side Diff: components/metrics/metrics_log.cc

Issue 573403002: Change UMA proto product field to be an int32. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/metrics/metrics_log.h" 5 #include "components/metrics/metrics_log.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 } 55 }
56 56
57 // Returns the date at which the current metrics client ID was created as 57 // Returns the date at which the current metrics client ID was created as
58 // a string containing seconds since the epoch, or "0" if none was found. 58 // a string containing seconds since the epoch, or "0" if none was found.
59 std::string GetMetricsEnabledDate(PrefService* pref) { 59 std::string GetMetricsEnabledDate(PrefService* pref) {
60 if (!pref) { 60 if (!pref) {
61 NOTREACHED(); 61 NOTREACHED();
62 return "0"; 62 return "0";
63 } 63 }
64 64
65 return pref->GetString(metrics::prefs::kMetricsReportingEnabledTimestamp); 65 return pref->GetString(prefs::kMetricsReportingEnabledTimestamp);
66 } 66 }
67 67
68 // Computes a SHA-1 hash of |data| and returns it as a hex string. 68 // Computes a SHA-1 hash of |data| and returns it as a hex string.
69 std::string ComputeSHA1(const std::string& data) { 69 std::string ComputeSHA1(const std::string& data) {
70 const std::string sha1 = base::SHA1HashString(data); 70 const std::string sha1 = base::SHA1HashString(data);
71 return base::HexEncode(sha1.data(), sha1.size()); 71 return base::HexEncode(sha1.data(), sha1.size());
72 } 72 }
73 73
74 void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids, 74 void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
75 SystemProfileProto* system_profile) { 75 SystemProfileProto* system_profile) {
(...skipping 11 matching lines...) Expand all
87 // timestamps. 87 // timestamps.
88 int64 RoundSecondsToHour(int64 time_in_seconds) { 88 int64 RoundSecondsToHour(int64 time_in_seconds) {
89 return 3600 * (time_in_seconds / 3600); 89 return 3600 * (time_in_seconds / 3600);
90 } 90 }
91 91
92 } // namespace 92 } // namespace
93 93
94 MetricsLog::MetricsLog(const std::string& client_id, 94 MetricsLog::MetricsLog(const std::string& client_id,
95 int session_id, 95 int session_id,
96 LogType log_type, 96 LogType log_type,
97 metrics::MetricsServiceClient* client, 97 MetricsServiceClient* client,
98 PrefService* local_state) 98 PrefService* local_state)
99 : closed_(false), 99 : closed_(false),
100 log_type_(log_type), 100 log_type_(log_type),
101 client_(client), 101 client_(client),
102 creation_time_(base::TimeTicks::Now()), 102 creation_time_(base::TimeTicks::Now()),
103 local_state_(local_state) { 103 local_state_(local_state) {
104 if (IsTestingID(client_id)) 104 if (IsTestingID(client_id))
105 uma_proto_.set_client_id(0); 105 uma_proto_.set_client_id(0);
106 else 106 else
107 uma_proto_.set_client_id(Hash(client_id)); 107 uma_proto_.set_client_id(Hash(client_id));
108 108
109 uma_proto_.set_session_id(session_id); 109 uma_proto_.set_session_id(session_id);
110 110
111 const int32 product = client_->GetProduct();
112 // Only set the product if it differs from the default value.
113 if (product != uma_proto_.product())
114 uma_proto_.set_product(product);
115
111 SystemProfileProto* system_profile = uma_proto_.mutable_system_profile(); 116 SystemProfileProto* system_profile = uma_proto_.mutable_system_profile();
112 system_profile->set_build_timestamp(GetBuildTime()); 117 system_profile->set_build_timestamp(GetBuildTime());
113 system_profile->set_app_version(client_->GetVersionString()); 118 system_profile->set_app_version(client_->GetVersionString());
114 system_profile->set_channel(client_->GetChannel()); 119 system_profile->set_channel(client_->GetChannel());
115 } 120 }
116 121
117 MetricsLog::~MetricsLog() { 122 MetricsLog::~MetricsLog() {
118 } 123 }
119 124
120 // static 125 // static
121 void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) { 126 void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) {
122 registry->RegisterIntegerPref(metrics::prefs::kStabilityLaunchCount, 0); 127 registry->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0);
123 registry->RegisterIntegerPref(metrics::prefs::kStabilityCrashCount, 0); 128 registry->RegisterIntegerPref(prefs::kStabilityCrashCount, 0);
124 registry->RegisterIntegerPref( 129 registry->RegisterIntegerPref(
125 metrics::prefs::kStabilityIncompleteSessionEndCount, 0); 130 prefs::kStabilityIncompleteSessionEndCount, 0);
Ilya Sherman 2014/09/25 18:29:55 nit: Looks like this and several other lines below
Alexei Svitkine (slow) 2014/09/26 16:51:09 Done. I've kept the changes in this CL though, sin
126 registry->RegisterIntegerPref( 131 registry->RegisterIntegerPref(
127 metrics::prefs::kStabilityBreakpadRegistrationFail, 0); 132 prefs::kStabilityBreakpadRegistrationFail, 0);
128 registry->RegisterIntegerPref( 133 registry->RegisterIntegerPref(
129 metrics::prefs::kStabilityBreakpadRegistrationSuccess, 0); 134 prefs::kStabilityBreakpadRegistrationSuccess, 0);
130 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerPresent, 0); 135 registry->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0);
131 registry->RegisterIntegerPref(metrics::prefs::kStabilityDebuggerNotPresent, 136 registry->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent,
132 0); 137 0);
133 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfile, 138 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfile,
134 std::string()); 139 std::string());
135 registry->RegisterStringPref(metrics::prefs::kStabilitySavedSystemProfileHash, 140 registry->RegisterStringPref(prefs::kStabilitySavedSystemProfileHash,
136 std::string()); 141 std::string());
137 } 142 }
138 143
139 // static 144 // static
140 uint64 MetricsLog::Hash(const std::string& value) { 145 uint64 MetricsLog::Hash(const std::string& value) {
141 uint64 hash = metrics::HashMetricName(value); 146 uint64 hash = HashMetricName(value);
142 147
143 // The following log is VERY helpful when folks add some named histogram into 148 // The following log is VERY helpful when folks add some named histogram into
144 // the code, but forgot to update the descriptive list of histograms. When 149 // the code, but forgot to update the descriptive list of histograms. When
145 // that happens, all we get to see (server side) is a hash of the histogram 150 // that happens, all we get to see (server side) is a hash of the histogram
146 // name. We can then use this logging to find out what histogram name was 151 // name. We can then use this logging to find out what histogram name was
147 // being hashed to a given MD5 value by just running the version of Chromium 152 // being hashed to a given MD5 value by just running the version of Chromium
148 // in question with --enable-logging. 153 // in question with --enable-logging.
149 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; 154 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]";
150 155
151 return hash; 156 return hash;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 if (i + 1 < histogram_proto->bucket_size() && 211 if (i + 1 < histogram_proto->bucket_size() &&
207 bucket->max() == histogram_proto->bucket(i + 1).min()) { 212 bucket->max() == histogram_proto->bucket(i + 1).min()) {
208 bucket->clear_max(); 213 bucket->clear_max();
209 } else if (bucket->max() == bucket->min() + 1) { 214 } else if (bucket->max() == bucket->min() + 1) {
210 bucket->clear_min(); 215 bucket->clear_min();
211 } 216 }
212 } 217 }
213 } 218 }
214 219
215 void MetricsLog::RecordStabilityMetrics( 220 void MetricsLog::RecordStabilityMetrics(
216 const std::vector<metrics::MetricsProvider*>& metrics_providers, 221 const std::vector<MetricsProvider*>& metrics_providers,
217 base::TimeDelta incremental_uptime, 222 base::TimeDelta incremental_uptime,
218 base::TimeDelta uptime) { 223 base::TimeDelta uptime) {
219 DCHECK(!closed_); 224 DCHECK(!closed_);
220 DCHECK(HasEnvironment()); 225 DCHECK(HasEnvironment());
221 DCHECK(!HasStabilityMetrics()); 226 DCHECK(!HasStabilityMetrics());
222 227
223 PrefService* pref = local_state_; 228 PrefService* pref = local_state_;
224 DCHECK(pref); 229 DCHECK(pref);
225 230
226 // Get stability attributes out of Local State, zeroing out stored values. 231 // Get stability attributes out of Local State, zeroing out stored values.
(...skipping 10 matching lines...) Expand all
237 242
238 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); 243 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
239 for (size_t i = 0; i < metrics_providers.size(); ++i) 244 for (size_t i = 0; i < metrics_providers.size(); ++i)
240 metrics_providers[i]->ProvideStabilityMetrics(system_profile); 245 metrics_providers[i]->ProvideStabilityMetrics(system_profile);
241 246
242 // Omit some stats unless this is the initial stability log. 247 // Omit some stats unless this is the initial stability log.
243 if (log_type() != INITIAL_STABILITY_LOG) 248 if (log_type() != INITIAL_STABILITY_LOG)
244 return; 249 return;
245 250
246 int incomplete_shutdown_count = 251 int incomplete_shutdown_count =
247 pref->GetInteger(metrics::prefs::kStabilityIncompleteSessionEndCount); 252 pref->GetInteger(prefs::kStabilityIncompleteSessionEndCount);
248 pref->SetInteger(metrics::prefs::kStabilityIncompleteSessionEndCount, 0); 253 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
249 int breakpad_registration_success_count = 254 int breakpad_registration_success_count =
250 pref->GetInteger(metrics::prefs::kStabilityBreakpadRegistrationSuccess); 255 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess);
251 pref->SetInteger(metrics::prefs::kStabilityBreakpadRegistrationSuccess, 0); 256 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
252 int breakpad_registration_failure_count = 257 int breakpad_registration_failure_count =
253 pref->GetInteger(metrics::prefs::kStabilityBreakpadRegistrationFail); 258 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail);
254 pref->SetInteger(metrics::prefs::kStabilityBreakpadRegistrationFail, 0); 259 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
255 int debugger_present_count = 260 int debugger_present_count =
256 pref->GetInteger(metrics::prefs::kStabilityDebuggerPresent); 261 pref->GetInteger(prefs::kStabilityDebuggerPresent);
257 pref->SetInteger(metrics::prefs::kStabilityDebuggerPresent, 0); 262 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
258 int debugger_not_present_count = 263 int debugger_not_present_count =
259 pref->GetInteger(metrics::prefs::kStabilityDebuggerNotPresent); 264 pref->GetInteger(prefs::kStabilityDebuggerNotPresent);
260 pref->SetInteger(metrics::prefs::kStabilityDebuggerNotPresent, 0); 265 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
261 266
262 // TODO(jar): The following are all optional, so we *could* optimize them for 267 // TODO(jar): The following are all optional, so we *could* optimize them for
263 // values of zero (and not include them). 268 // values of zero (and not include them).
264 SystemProfileProto::Stability* stability = 269 SystemProfileProto::Stability* stability =
265 system_profile->mutable_stability(); 270 system_profile->mutable_stability();
266 stability->set_incomplete_shutdown_count(incomplete_shutdown_count); 271 stability->set_incomplete_shutdown_count(incomplete_shutdown_count);
267 stability->set_breakpad_registration_success_count( 272 stability->set_breakpad_registration_success_count(
268 breakpad_registration_success_count); 273 breakpad_registration_success_count);
269 stability->set_breakpad_registration_failure_count( 274 stability->set_breakpad_registration_failure_count(
270 breakpad_registration_failure_count); 275 breakpad_registration_failure_count);
271 stability->set_debugger_present_count(debugger_present_count); 276 stability->set_debugger_present_count(debugger_present_count);
272 stability->set_debugger_not_present_count(debugger_not_present_count); 277 stability->set_debugger_not_present_count(debugger_not_present_count);
273 } 278 }
274 279
275 void MetricsLog::RecordGeneralMetrics( 280 void MetricsLog::RecordGeneralMetrics(
276 const std::vector<metrics::MetricsProvider*>& metrics_providers) { 281 const std::vector<MetricsProvider*>& metrics_providers) {
277 for (size_t i = 0; i < metrics_providers.size(); ++i) 282 for (size_t i = 0; i < metrics_providers.size(); ++i)
278 metrics_providers[i]->ProvideGeneralMetrics(uma_proto()); 283 metrics_providers[i]->ProvideGeneralMetrics(uma_proto());
279 } 284 }
280 285
281 void MetricsLog::GetFieldTrialIds( 286 void MetricsLog::GetFieldTrialIds(
282 std::vector<ActiveGroupId>* field_trial_ids) const { 287 std::vector<ActiveGroupId>* field_trial_ids) const {
283 variations::GetFieldTrialActiveGroupIds(field_trial_ids); 288 variations::GetFieldTrialActiveGroupIds(field_trial_ids);
284 } 289 }
285 290
286 bool MetricsLog::HasEnvironment() const { 291 bool MetricsLog::HasEnvironment() const {
287 return uma_proto()->system_profile().has_uma_enabled_date(); 292 return uma_proto()->system_profile().has_uma_enabled_date();
288 } 293 }
289 294
290 bool MetricsLog::HasStabilityMetrics() const { 295 bool MetricsLog::HasStabilityMetrics() const {
291 return uma_proto()->system_profile().stability().has_launch_count(); 296 return uma_proto()->system_profile().stability().has_launch_count();
292 } 297 }
293 298
294 // The server refuses data that doesn't have certain values. crashcount and 299 // The server refuses data that doesn't have certain values. crashcount and
295 // launchcount are currently "required" in the "stability" group. 300 // launchcount are currently "required" in the "stability" group.
296 // TODO(isherman): Stop writing these attributes specially once the migration to 301 // TODO(isherman): Stop writing these attributes specially once the migration to
297 // protobufs is complete. 302 // protobufs is complete.
298 void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) { 303 void MetricsLog::WriteRequiredStabilityAttributes(PrefService* pref) {
299 int launch_count = pref->GetInteger(metrics::prefs::kStabilityLaunchCount); 304 int launch_count = pref->GetInteger(prefs::kStabilityLaunchCount);
300 pref->SetInteger(metrics::prefs::kStabilityLaunchCount, 0); 305 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
301 int crash_count = pref->GetInteger(metrics::prefs::kStabilityCrashCount); 306 int crash_count = pref->GetInteger(prefs::kStabilityCrashCount);
302 pref->SetInteger(metrics::prefs::kStabilityCrashCount, 0); 307 pref->SetInteger(prefs::kStabilityCrashCount, 0);
303 308
304 SystemProfileProto::Stability* stability = 309 SystemProfileProto::Stability* stability =
305 uma_proto()->mutable_system_profile()->mutable_stability(); 310 uma_proto()->mutable_system_profile()->mutable_stability();
306 stability->set_launch_count(launch_count); 311 stability->set_launch_count(launch_count);
307 stability->set_crash_count(crash_count); 312 stability->set_crash_count(crash_count);
308 } 313 }
309 314
310 void MetricsLog::WriteRealtimeStabilityAttributes( 315 void MetricsLog::WriteRealtimeStabilityAttributes(
311 PrefService* pref, 316 PrefService* pref,
312 base::TimeDelta incremental_uptime, 317 base::TimeDelta incremental_uptime,
313 base::TimeDelta uptime) { 318 base::TimeDelta uptime) {
314 // Update the stats which are critical for real-time stability monitoring. 319 // Update the stats which are critical for real-time stability monitoring.
315 // Since these are "optional," only list ones that are non-zero, as the counts 320 // Since these are "optional," only list ones that are non-zero, as the counts
316 // are aggregated (summed) server side. 321 // are aggregated (summed) server side.
317 322
318 SystemProfileProto::Stability* stability = 323 SystemProfileProto::Stability* stability =
319 uma_proto()->mutable_system_profile()->mutable_stability(); 324 uma_proto()->mutable_system_profile()->mutable_stability();
320 325
321 const uint64 incremental_uptime_sec = incremental_uptime.InSeconds(); 326 const uint64 incremental_uptime_sec = incremental_uptime.InSeconds();
322 if (incremental_uptime_sec) 327 if (incremental_uptime_sec)
323 stability->set_incremental_uptime_sec(incremental_uptime_sec); 328 stability->set_incremental_uptime_sec(incremental_uptime_sec);
324 const uint64 uptime_sec = uptime.InSeconds(); 329 const uint64 uptime_sec = uptime.InSeconds();
325 if (uptime_sec) 330 if (uptime_sec)
326 stability->set_uptime_sec(uptime_sec); 331 stability->set_uptime_sec(uptime_sec);
327 } 332 }
328 333
329 void MetricsLog::RecordEnvironment( 334 void MetricsLog::RecordEnvironment(
330 const std::vector<metrics::MetricsProvider*>& metrics_providers, 335 const std::vector<MetricsProvider*>& metrics_providers,
331 const std::vector<variations::ActiveGroupId>& synthetic_trials, 336 const std::vector<variations::ActiveGroupId>& synthetic_trials,
332 int64 install_date) { 337 int64 install_date) {
333 DCHECK(!HasEnvironment()); 338 DCHECK(!HasEnvironment());
334 339
335 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); 340 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
336 341
337 std::string brand_code; 342 std::string brand_code;
338 if (client_->GetBrand(&brand_code)) 343 if (client_->GetBrand(&brand_code))
339 system_profile->set_brand_code(brand_code); 344 system_profile->set_brand_code(brand_code);
340 345
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 WriteFieldTrials(synthetic_trials, system_profile); 396 WriteFieldTrials(synthetic_trials, system_profile);
392 397
393 for (size_t i = 0; i < metrics_providers.size(); ++i) 398 for (size_t i = 0; i < metrics_providers.size(); ++i)
394 metrics_providers[i]->ProvideSystemProfileMetrics(system_profile); 399 metrics_providers[i]->ProvideSystemProfileMetrics(system_profile);
395 400
396 std::string serialied_system_profile; 401 std::string serialied_system_profile;
397 std::string base64_system_profile; 402 std::string base64_system_profile;
398 if (system_profile->SerializeToString(&serialied_system_profile)) { 403 if (system_profile->SerializeToString(&serialied_system_profile)) {
399 base::Base64Encode(serialied_system_profile, &base64_system_profile); 404 base::Base64Encode(serialied_system_profile, &base64_system_profile);
400 PrefService* local_state = local_state_; 405 PrefService* local_state = local_state_;
401 local_state->SetString(metrics::prefs::kStabilitySavedSystemProfile, 406 local_state->SetString(prefs::kStabilitySavedSystemProfile,
402 base64_system_profile); 407 base64_system_profile);
403 local_state->SetString(metrics::prefs::kStabilitySavedSystemProfileHash, 408 local_state->SetString(prefs::kStabilitySavedSystemProfileHash,
404 ComputeSHA1(serialied_system_profile)); 409 ComputeSHA1(serialied_system_profile));
405 } 410 }
406 } 411 }
407 412
408 bool MetricsLog::LoadSavedEnvironmentFromPrefs() { 413 bool MetricsLog::LoadSavedEnvironmentFromPrefs() {
409 PrefService* local_state = local_state_; 414 PrefService* local_state = local_state_;
410 const std::string base64_system_profile = 415 const std::string base64_system_profile =
411 local_state->GetString(metrics::prefs::kStabilitySavedSystemProfile); 416 local_state->GetString(prefs::kStabilitySavedSystemProfile);
412 if (base64_system_profile.empty()) 417 if (base64_system_profile.empty())
413 return false; 418 return false;
414 419
415 const std::string system_profile_hash = 420 const std::string system_profile_hash =
416 local_state->GetString(metrics::prefs::kStabilitySavedSystemProfileHash); 421 local_state->GetString(prefs::kStabilitySavedSystemProfileHash);
417 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfile); 422 local_state->ClearPref(prefs::kStabilitySavedSystemProfile);
418 local_state->ClearPref(metrics::prefs::kStabilitySavedSystemProfileHash); 423 local_state->ClearPref(prefs::kStabilitySavedSystemProfileHash);
419 424
420 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile(); 425 SystemProfileProto* system_profile = uma_proto()->mutable_system_profile();
421 std::string serialied_system_profile; 426 std::string serialied_system_profile;
422 return base::Base64Decode(base64_system_profile, &serialied_system_profile) && 427 return base::Base64Decode(base64_system_profile, &serialied_system_profile) &&
423 ComputeSHA1(serialied_system_profile) == system_profile_hash && 428 ComputeSHA1(serialied_system_profile) == system_profile_hash &&
424 system_profile->ParseFromString(serialied_system_profile); 429 system_profile->ParseFromString(serialied_system_profile);
425 } 430 }
426 431
427 void MetricsLog::CloseLog() { 432 void MetricsLog::CloseLog() {
428 DCHECK(!closed_); 433 DCHECK(!closed_);
429 closed_ = true; 434 closed_ = true;
430 } 435 }
431 436
432 void MetricsLog::GetEncodedLog(std::string* encoded_log) { 437 void MetricsLog::GetEncodedLog(std::string* encoded_log) {
433 DCHECK(closed_); 438 DCHECK(closed_);
434 uma_proto_.SerializeToString(encoded_log); 439 uma_proto_.SerializeToString(encoded_log);
435 } 440 }
436 441
437 } // namespace metrics 442 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698