Chromium Code Reviews| Index: components/metrics/metrics_log.cc |
| =================================================================== |
| --- components/metrics/metrics_log.cc (revision 275440) |
| +++ components/metrics/metrics_log.cc (working copy) |
| @@ -10,9 +10,10 @@ |
| #include "base/base64.h" |
| #include "base/basictypes.h" |
| -#include "base/bind.h" |
| #include "base/cpu.h" |
| #include "base/memory/scoped_ptr.h" |
| +#include "base/metrics/histogram.h" |
| +#include "base/metrics/histogram_samples.h" |
| #include "base/prefs/pref_registry_simple.h" |
| #include "base/prefs/pref_service.h" |
| #include "base/sha1.h" |
| @@ -21,10 +22,13 @@ |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/sys_info.h" |
| #include "base/time/time.h" |
| +#include "components/metrics/metrics_hashes.h" |
| #include "components/metrics/metrics_pref_names.h" |
| #include "components/metrics/metrics_provider.h" |
| #include "components/metrics/metrics_service_client.h" |
| +#include "components/metrics/proto/histogram_event.pb.h" |
| #include "components/metrics/proto/system_profile.pb.h" |
| +#include "components/metrics/proto/user_action_event.pb.h" |
| #include "components/variations/active_field_trials.h" |
| #if defined(OS_ANDROID) |
| @@ -38,13 +42,20 @@ |
| extern "C" IMAGE_DOS_HEADER __ImageBase; |
| #endif |
| -using metrics::MetricsLogBase; |
| +using base::SampleCountIterator; |
| +using metrics::HistogramEventProto; |
| using metrics::ProfilerEventProto; |
| using metrics::SystemProfileProto; |
| +using metrics::UserActionEventProto; |
| typedef variations::ActiveGroupId ActiveGroupId; |
| namespace { |
| +// Any id less than 16 bytes is considered to be a testing id. |
| +bool IsTestingID(const std::string& id) { |
| + return id.size() < 16; |
| +} |
| + |
| // Returns the date at which the current metrics client ID was created as |
| // a string containing seconds since the epoch, or "0" if none was found. |
| std::string GetMetricsEnabledDate(PrefService* pref) { |
| @@ -87,17 +98,25 @@ |
| LogType log_type, |
| metrics::MetricsServiceClient* client, |
| PrefService* local_state) |
| - : MetricsLogBase(client_id, |
| - session_id, |
| - log_type, |
| - client->GetVersionString()), |
| + : locked_(false), |
| + log_type_(log_type), |
| client_(client), |
| creation_time_(base::TimeTicks::Now()), |
| local_state_(local_state) { |
| - uma_proto()->mutable_system_profile()->set_channel(client_->GetChannel()); |
| + if (IsTestingID(client_id)) |
| + uma_proto_.set_client_id(0); |
| + else |
| + uma_proto_.set_client_id(Hash(client_id)); |
|
Ilya Sherman
2014/06/06 19:37:01
Optional nit: I prefer to leave blank lines after
Alexei Svitkine (slow)
2014/06/06 21:45:42
Done.
|
| + uma_proto_.set_session_id(session_id); |
| + |
| + SystemProfileProto* system_profile = uma_proto_.mutable_system_profile(); |
| + system_profile->set_build_timestamp(GetBuildTime()); |
| + system_profile->set_app_version(client_->GetVersionString()); |
| + system_profile->set_channel(client_->GetChannel()); |
| } |
| -MetricsLog::~MetricsLog() {} |
| +MetricsLog::~MetricsLog() { |
| +} |
| // static |
| void MetricsLog::RegisterPrefs(PrefRegistrySimple* registry) { |
| @@ -118,6 +137,82 @@ |
| std::string()); |
| } |
| +// static |
| +uint64 MetricsLog::Hash(const std::string& value) { |
| + uint64 hash = metrics::HashMetricName(value); |
| + |
| + // The following log is VERY helpful when folks add some named histogram into |
| + // the code, but forgot to update the descriptive list of histograms. When |
| + // that happens, all we get to see (server side) is a hash of the histogram |
| + // name. We can then use this logging to find out what histogram name was |
| + // being hashed to a given MD5 value by just running the version of Chromium |
| + // in question with --enable-logging. |
| + DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]"; |
| + |
| + return hash; |
| +} |
| + |
| +// static |
| +int64 MetricsLog::GetBuildTime() { |
| + static int64 integral_build_time = 0; |
| + if (!integral_build_time) { |
| + base::Time time; |
| + static const char kDateTime[] = __DATE__ " " __TIME__ " GMT"; |
| + bool result = base::Time::FromString(kDateTime, &time); |
| + DCHECK(result); |
| + integral_build_time = static_cast<int64>(time.ToTimeT()); |
| + } |
| + return integral_build_time; |
| +} |
| + |
| +// static |
| +int64 MetricsLog::GetCurrentTime() { |
| + return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds(); |
| +} |
| + |
| +void MetricsLog::RecordUserAction(const std::string& key) { |
| + DCHECK(!locked_); |
| + |
| + UserActionEventProto* user_action = uma_proto_.add_user_action_event(); |
| + user_action->set_name_hash(Hash(key)); |
| + user_action->set_time(GetCurrentTime()); |
| +} |
| + |
| +void MetricsLog::RecordHistogramDelta(const std::string& histogram_name, |
| + const base::HistogramSamples& snapshot) { |
| + DCHECK(!locked_); |
| + DCHECK_NE(0, snapshot.TotalCount()); |
| + |
| + // We will ignore the MAX_INT/infinite value in the last element of range[]. |
| + |
| + HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event(); |
| + histogram_proto->set_name_hash(Hash(histogram_name)); |
| + histogram_proto->set_sum(snapshot.sum()); |
| + |
| + for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done(); |
| + it->Next()) { |
| + base::Histogram::Sample min; |
| + base::Histogram::Sample max; |
| + base::Histogram::Count count; |
| + it->Get(&min, &max, &count); |
| + HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket(); |
| + bucket->set_min(min); |
| + bucket->set_max(max); |
| + bucket->set_count(count); |
| + } |
| + |
| + // Omit fields to save space (see rules in histogram_event.proto comments). |
| + for (int i = 0; i < histogram_proto->bucket_size(); ++i) { |
| + HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); |
| + if (i + 1 < histogram_proto->bucket_size() && |
| + bucket->max() == histogram_proto->bucket(i + 1).min()) { |
| + bucket->clear_max(); |
| + } else if (bucket->max() == bucket->min() + 1) { |
| + bucket->clear_min(); |
| + } |
| + } |
| +} |
| + |
| void MetricsLog::RecordStabilityMetrics( |
| const std::vector<metrics::MetricsProvider*>& metrics_providers, |
| base::TimeDelta incremental_uptime, |
| @@ -331,3 +426,12 @@ |
| system_profile->ParseFromString(serialied_system_profile); |
| } |
| +void MetricsLog::CloseLog() { |
| + DCHECK(!locked_); |
|
Ilya Sherman
2014/06/06 19:37:01
Optional nit: Maybe we should rename "locked_" to
Alexei Svitkine (slow)
2014/06/06 21:45:42
Done.
|
| + locked_ = true; |
| +} |
| + |
| +void MetricsLog::GetEncodedLog(std::string* encoded_log) { |
| + DCHECK(locked_); |
| + uma_proto_.SerializeToString(encoded_log); |
| +} |