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

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

Issue 239093004: Move part of metrics from chrome/common to components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unittest that broke after rebase. Created 6 years, 8 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 (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) { return id.size() < 16; }
Ilya Sherman 2014/04/15 23:00:40 nit: This should retain its previous formatting.
29 return id.size() < 16;
30 }
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 29 } // namespace
52 30
53 MetricsLogBase::MetricsLogBase(const std::string& client_id, 31 MetricsLogBase::MetricsLogBase(const std::string& client_id,
54 int session_id, 32 int session_id,
55 LogType log_type, 33 LogType log_type,
56 const std::string& version_string) 34 const std::string& version_string)
57 : num_events_(0), 35 : num_events_(0),
58 locked_(false), 36 locked_(false),
59 log_type_(log_type) { 37 log_type_(log_type) {
60 DCHECK_NE(NO_LOG, log_type); 38 DCHECK_NE(NO_LOG, log_type);
61 if (IsTestingID(client_id)) 39 if (IsTestingID(client_id))
62 uma_proto_.set_client_id(0); 40 uma_proto_.set_client_id(0);
63 else 41 else
64 uma_proto_.set_client_id(Hash(client_id)); 42 uma_proto_.set_client_id(Hash(client_id));
65 43
66 uma_proto_.set_session_id(session_id); 44 uma_proto_.set_session_id(session_id);
67 uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime()); 45 uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime());
68 uma_proto_.mutable_system_profile()->set_app_version(version_string); 46 uma_proto_.mutable_system_profile()->set_app_version(version_string);
69 uma_proto_.mutable_system_profile()->set_channel(
70 AsProtobufChannel(chrome::VersionInfo::GetChannel()));
71 } 47 }
72 48
73 MetricsLogBase::~MetricsLogBase() {} 49 MetricsLogBase::~MetricsLogBase() {}
74 50
75 // static 51 // static
76 uint64 MetricsLogBase::Hash(const std::string& value) { 52 uint64 MetricsLogBase::Hash(const std::string& value) {
77 uint64 hash = metrics::HashMetricName(value); 53 uint64 hash = metrics::HashMetricName(value);
78 54
79 // The following log is VERY helpful when folks add some named histogram into 55 // 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 56 // the code, but forgot to update the descriptive list of histograms. When
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 const HistogramSamples& snapshot) { 105 const HistogramSamples& snapshot) {
130 DCHECK(!locked_); 106 DCHECK(!locked_);
131 DCHECK_NE(0, snapshot.TotalCount()); 107 DCHECK_NE(0, snapshot.TotalCount());
132 108
133 // We will ignore the MAX_INT/infinite value in the last element of range[]. 109 // We will ignore the MAX_INT/infinite value in the last element of range[].
134 110
135 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event(); 111 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event();
136 histogram_proto->set_name_hash(Hash(histogram_name)); 112 histogram_proto->set_name_hash(Hash(histogram_name));
137 histogram_proto->set_sum(snapshot.sum()); 113 histogram_proto->set_sum(snapshot.sum());
138 114
139 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); 115 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done();
140 !it->Done();
141 it->Next()) { 116 it->Next()) {
142 HistogramBase::Sample min; 117 HistogramBase::Sample min;
143 HistogramBase::Sample max; 118 HistogramBase::Sample max;
144 HistogramBase::Count count; 119 HistogramBase::Count count;
145 it->Get(&min, &max, &count); 120 it->Get(&min, &max, &count);
146 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket(); 121 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket();
147 bucket->set_min(min); 122 bucket->set_min(min);
148 bucket->set_max(max); 123 bucket->set_max(max);
149 bucket->set_count(count); 124 bucket->set_count(count);
150 } 125 }
151 126
152 // Omit fields to save space (see rules in histogram_event.proto comments). 127 // Omit fields to save space (see rules in histogram_event.proto comments).
153 for (int i = 0; i < histogram_proto->bucket_size(); ++i) { 128 for (int i = 0; i < histogram_proto->bucket_size(); ++i) {
154 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i); 129 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i);
155 if (i + 1 < histogram_proto->bucket_size() && 130 if (i + 1 < histogram_proto->bucket_size() &&
156 bucket->max() == histogram_proto->bucket(i + 1).min()) { 131 bucket->max() == histogram_proto->bucket(i + 1).min()) {
157 bucket->clear_max(); 132 bucket->clear_max();
158 } else if (bucket->max() == bucket->min() + 1) { 133 } else if (bucket->max() == bucket->min() + 1) {
159 bucket->clear_min(); 134 bucket->clear_min();
160 } 135 }
161 } 136 }
162 } 137 }
138 } // namespace metrics
Ilya Sherman 2014/04/15 23:00:40 nit: There should be a blank line above this. (Pl
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698