| OLD | NEW |
| 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 #include "chrome/common/metrics/metrics_log_base.h" | 5 #include "components/metrics/metrics_log_base.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_base.h" | 7 #include "base/metrics/histogram_base.h" |
| 8 #include "base/metrics/histogram_samples.h" | 8 #include "base/metrics/histogram_samples.h" |
| 9 #include "chrome/common/chrome_version_info.h" | |
| 10 #include "chrome/common/metrics/proto/histogram_event.pb.h" | |
| 11 #include "chrome/common/metrics/proto/system_profile.pb.h" | |
| 12 #include "chrome/common/metrics/proto/user_action_event.pb.h" | |
| 13 #include "components/metrics/metrics_hashes.h" | 9 #include "components/metrics/metrics_hashes.h" |
| 10 #include "components/metrics/proto/histogram_event.pb.h" |
| 11 #include "components/metrics/proto/system_profile.pb.h" |
| 12 #include "components/metrics/proto/user_action_event.pb.h" |
| 14 | 13 |
| 15 using base::Histogram; | 14 using base::Histogram; |
| 16 using base::HistogramBase; | 15 using base::HistogramBase; |
| 17 using base::HistogramSamples; | 16 using base::HistogramSamples; |
| 18 using base::SampleCountIterator; | 17 using base::SampleCountIterator; |
| 19 using base::Time; | 18 using base::Time; |
| 20 using base::TimeDelta; | 19 using base::TimeDelta; |
| 21 using metrics::HistogramEventProto; | 20 using metrics::HistogramEventProto; |
| 22 using metrics::SystemProfileProto; | 21 using metrics::SystemProfileProto; |
| 23 using metrics::UserActionEventProto; | 22 using metrics::UserActionEventProto; |
| 24 | 23 |
| 24 namespace metrics { |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // Any id less than 16 bytes is considered to be a testing id. | 27 // Any id less than 16 bytes is considered to be a testing id. |
| 28 bool IsTestingID(const std::string& id) { | 28 bool IsTestingID(const std::string& id) { |
| 29 return id.size() < 16; | 29 return id.size() < 16; |
| 30 } | 30 } |
| 31 | 31 |
| 32 SystemProfileProto::Channel AsProtobufChannel( | |
| 33 chrome::VersionInfo::Channel channel) { | |
| 34 switch (channel) { | |
| 35 case chrome::VersionInfo::CHANNEL_UNKNOWN: | |
| 36 return SystemProfileProto::CHANNEL_UNKNOWN; | |
| 37 case chrome::VersionInfo::CHANNEL_CANARY: | |
| 38 return SystemProfileProto::CHANNEL_CANARY; | |
| 39 case chrome::VersionInfo::CHANNEL_DEV: | |
| 40 return SystemProfileProto::CHANNEL_DEV; | |
| 41 case chrome::VersionInfo::CHANNEL_BETA: | |
| 42 return SystemProfileProto::CHANNEL_BETA; | |
| 43 case chrome::VersionInfo::CHANNEL_STABLE: | |
| 44 return SystemProfileProto::CHANNEL_STABLE; | |
| 45 default: | |
| 46 NOTREACHED(); | |
| 47 return SystemProfileProto::CHANNEL_UNKNOWN; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 } // namespace | 32 } // namespace |
| 52 | 33 |
| 53 MetricsLogBase::MetricsLogBase(const std::string& client_id, | 34 MetricsLogBase::MetricsLogBase(const std::string& client_id, |
| 54 int session_id, | 35 int session_id, |
| 55 LogType log_type, | 36 LogType log_type, |
| 56 const std::string& version_string) | 37 const std::string& version_string) |
| 57 : num_events_(0), | 38 : num_events_(0), |
| 58 locked_(false), | 39 locked_(false), |
| 59 log_type_(log_type) { | 40 log_type_(log_type) { |
| 60 DCHECK_NE(NO_LOG, log_type); | 41 DCHECK_NE(NO_LOG, log_type); |
| 61 if (IsTestingID(client_id)) | 42 if (IsTestingID(client_id)) |
| 62 uma_proto_.set_client_id(0); | 43 uma_proto_.set_client_id(0); |
| 63 else | 44 else |
| 64 uma_proto_.set_client_id(Hash(client_id)); | 45 uma_proto_.set_client_id(Hash(client_id)); |
| 65 | 46 |
| 66 uma_proto_.set_session_id(session_id); | 47 uma_proto_.set_session_id(session_id); |
| 67 uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime()); | 48 uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime()); |
| 68 uma_proto_.mutable_system_profile()->set_app_version(version_string); | 49 uma_proto_.mutable_system_profile()->set_app_version(version_string); |
| 69 uma_proto_.mutable_system_profile()->set_channel( | |
| 70 AsProtobufChannel(chrome::VersionInfo::GetChannel())); | |
| 71 } | 50 } |
| 72 | 51 |
| 73 MetricsLogBase::~MetricsLogBase() {} | 52 MetricsLogBase::~MetricsLogBase() {} |
| 74 | 53 |
| 75 // static | 54 // static |
| 76 uint64 MetricsLogBase::Hash(const std::string& value) { | 55 uint64 MetricsLogBase::Hash(const std::string& value) { |
| 77 uint64 hash = metrics::HashMetricName(value); | 56 uint64 hash = metrics::HashMetricName(value); |
| 78 | 57 |
| 79 // The following log is VERY helpful when folks add some named histogram into | 58 // The following log is VERY helpful when folks add some named histogram into |
| 80 // the code, but forgot to update the descriptive list of histograms. When | 59 // the code, but forgot to update the descriptive list of histograms. When |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 const HistogramSamples& snapshot) { | 108 const HistogramSamples& snapshot) { |
| 130 DCHECK(!locked_); | 109 DCHECK(!locked_); |
| 131 DCHECK_NE(0, snapshot.TotalCount()); | 110 DCHECK_NE(0, snapshot.TotalCount()); |
| 132 | 111 |
| 133 // We will ignore the MAX_INT/infinite value in the last element of range[]. | 112 // We will ignore the MAX_INT/infinite value in the last element of range[]. |
| 134 | 113 |
| 135 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event(); | 114 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event(); |
| 136 histogram_proto->set_name_hash(Hash(histogram_name)); | 115 histogram_proto->set_name_hash(Hash(histogram_name)); |
| 137 histogram_proto->set_sum(snapshot.sum()); | 116 histogram_proto->set_sum(snapshot.sum()); |
| 138 | 117 |
| 139 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); | 118 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done(); |
| 140 !it->Done(); | |
| 141 it->Next()) { | 119 it->Next()) { |
| 142 HistogramBase::Sample min; | 120 HistogramBase::Sample min; |
| 143 HistogramBase::Sample max; | 121 HistogramBase::Sample max; |
| 144 HistogramBase::Count count; | 122 HistogramBase::Count count; |
| 145 it->Get(&min, &max, &count); | 123 it->Get(&min, &max, &count); |
| 146 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket(); | 124 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket(); |
| 147 bucket->set_min(min); | 125 bucket->set_min(min); |
| 148 bucket->set_max(max); | 126 bucket->set_max(max); |
| 149 bucket->set_count(count); | 127 bucket->set_count(count); |
| 150 } | 128 } |
| 151 | 129 |
| 152 // Omit fields to save space (see rules in histogram_event.proto comments). | 130 // Omit fields to save space (see rules in histogram_event.proto comments). |
| 153 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { | 131 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { |
| 154 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); | 132 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); |
| 155 if (i + 1 < histogram_proto->bucket_size() && | 133 if (i + 1 < histogram_proto->bucket_size() && |
| 156 bucket->max() == histogram_proto->bucket(i + 1).min()) { | 134 bucket->max() == histogram_proto->bucket(i + 1).min()) { |
| 157 bucket->clear_max(); | 135 bucket->clear_max(); |
| 158 } else if (bucket->max() == bucket->min() + 1) { | 136 } else if (bucket->max() == bucket->min() + 1) { |
| 159 bucket->clear_min(); | 137 bucket->clear_min(); |
| 160 } | 138 } |
| 161 } | 139 } |
| 162 } | 140 } |
| 141 |
| 142 } // namespace metrics |
| OLD | NEW |