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

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

Issue 318993002: Merge MetricsLog and MetricsLogBase. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: address nits Created 6 years, 6 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
« no previous file with comments | « components/metrics/metrics_log_base.h ('k') | components/metrics/metrics_log_base_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/metrics/metrics_log_base.h"
6
7 #include "base/metrics/histogram_base.h"
8 #include "base/metrics/histogram_samples.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"
13
14 using base::Histogram;
15 using base::HistogramBase;
16 using base::HistogramSamples;
17 using base::SampleCountIterator;
18 using base::Time;
19 using base::TimeDelta;
20 using metrics::HistogramEventProto;
21 using metrics::SystemProfileProto;
22 using metrics::UserActionEventProto;
23
24 namespace metrics {
25 namespace {
26
27 // Any id less than 16 bytes is considered to be a testing id.
28 bool IsTestingID(const std::string& id) {
29 return id.size() < 16;
30 }
31
32 } // namespace
33
34 MetricsLogBase::MetricsLogBase(const std::string& client_id,
35 int session_id,
36 LogType log_type,
37 const std::string& version_string)
38 : locked_(false),
39 log_type_(log_type) {
40 if (IsTestingID(client_id))
41 uma_proto_.set_client_id(0);
42 else
43 uma_proto_.set_client_id(Hash(client_id));
44
45 uma_proto_.set_session_id(session_id);
46 uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime());
47 uma_proto_.mutable_system_profile()->set_app_version(version_string);
48 }
49
50 MetricsLogBase::~MetricsLogBase() {}
51
52 // static
53 uint64 MetricsLogBase::Hash(const std::string& value) {
54 uint64 hash = metrics::HashMetricName(value);
55
56 // The following log is VERY helpful when folks add some named histogram into
57 // the code, but forgot to update the descriptive list of histograms. When
58 // that happens, all we get to see (server side) is a hash of the histogram
59 // name. We can then use this logging to find out what histogram name was
60 // being hashed to a given MD5 value by just running the version of Chromium
61 // in question with --enable-logging.
62 DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]";
63
64 return hash;
65 }
66
67 // static
68 int64 MetricsLogBase::GetBuildTime() {
69 static int64 integral_build_time = 0;
70 if (!integral_build_time) {
71 Time time;
72 const char* kDateTime = __DATE__ " " __TIME__ " GMT";
73 bool result = Time::FromString(kDateTime, &time);
74 DCHECK(result);
75 integral_build_time = static_cast<int64>(time.ToTimeT());
76 }
77 return integral_build_time;
78 }
79
80 // static
81 int64 MetricsLogBase::GetCurrentTime() {
82 return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds();
83 }
84
85 void MetricsLogBase::CloseLog() {
86 DCHECK(!locked_);
87 locked_ = true;
88 }
89
90 void MetricsLogBase::GetEncodedLog(std::string* encoded_log) {
91 DCHECK(locked_);
92 uma_proto_.SerializeToString(encoded_log);
93 }
94
95 void MetricsLogBase::RecordUserAction(const std::string& key) {
96 DCHECK(!locked_);
97
98 UserActionEventProto* user_action = uma_proto_.add_user_action_event();
99 user_action->set_name_hash(Hash(key));
100 user_action->set_time(GetCurrentTime());
101 }
102
103 void MetricsLogBase::RecordHistogramDelta(const std::string& histogram_name,
104 const HistogramSamples& snapshot) {
105 DCHECK(!locked_);
106 DCHECK_NE(0, snapshot.TotalCount());
107
108 // We will ignore the MAX_INT/infinite value in the last element of range[].
109
110 HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event();
111 histogram_proto->set_name_hash(Hash(histogram_name));
112 histogram_proto->set_sum(snapshot.sum());
113
114 for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done();
115 it->Next()) {
116 HistogramBase::Sample min;
117 HistogramBase::Sample max;
118 HistogramBase::Count count;
119 it->Get(&min, &max, &count);
120 HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket();
121 bucket->set_min(min);
122 bucket->set_max(max);
123 bucket->set_count(count);
124 }
125
126 // Omit fields to save space (see rules in histogram_event.proto comments).
127 for (int i = 0; i < histogram_proto->bucket_size(); ++i) {
128 HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i);
129 if (i + 1 < histogram_proto->bucket_size() &&
130 bucket->max() == histogram_proto->bucket(i + 1).min()) {
131 bucket->clear_max();
132 } else if (bucket->max() == bucket->min() + 1) {
133 bucket->clear_min();
134 }
135 }
136 }
137
138 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/metrics_log_base.h ('k') | components/metrics/metrics_log_base_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698