OLD | NEW |
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 // This file defines a set of user experience metrics data recorded by | 5 // This file defines a set of user experience metrics data recorded by |
6 // the MetricsService. This is the unit of data that is sent to the server. | 6 // the MetricsService. This is the unit of data that is sent to the server. |
7 | 7 |
8 #ifndef COMPONENTS_METRICS_METRICS_LOG_H_ | 8 #ifndef COMPONENTS_METRICS_METRICS_LOG_H_ |
9 #define COMPONENTS_METRICS_METRICS_LOG_H_ | 9 #define COMPONENTS_METRICS_METRICS_LOG_H_ |
10 | 10 |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
15 #include "components/metrics/metrics_log_base.h" | 15 #include "base/time/time.h" |
| 16 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" |
16 | 17 |
17 class PrefRegistrySimple; | 18 class PrefRegistrySimple; |
18 class PrefService; | 19 class PrefService; |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 class DictionaryValue; | 22 class DictionaryValue; |
| 23 class HistogramSamples; |
22 } | 24 } |
23 | 25 |
24 namespace content { | 26 namespace content { |
25 struct WebPluginInfo; | 27 struct WebPluginInfo; |
26 } | 28 } |
27 | 29 |
28 namespace metrics { | 30 namespace metrics { |
29 class MetricsProvider; | 31 class MetricsProvider; |
30 class MetricsServiceClient; | 32 class MetricsServiceClient; |
31 } | 33 } |
32 | 34 |
33 namespace tracked_objects { | 35 namespace tracked_objects { |
34 struct ProcessDataSnapshot; | 36 struct ProcessDataSnapshot; |
35 } | 37 } |
36 | 38 |
37 namespace variations { | 39 namespace variations { |
38 struct ActiveGroupId; | 40 struct ActiveGroupId; |
39 } | 41 } |
40 | 42 |
41 class MetricsLog : public metrics::MetricsLogBase { | 43 class MetricsLog { |
42 public: | 44 public: |
| 45 enum LogType { |
| 46 INITIAL_STABILITY_LOG, // The initial log containing stability stats. |
| 47 ONGOING_LOG, // Subsequent logs in a session. |
| 48 }; |
| 49 |
43 // Creates a new metrics log of the specified type. | 50 // Creates a new metrics log of the specified type. |
44 // |client_id| is the identifier for this profile on this installation | 51 // |client_id| is the identifier for this profile on this installation |
45 // |session_id| is an integer that's incremented on each application launch | 52 // |session_id| is an integer that's incremented on each application launch |
46 // |client| is used to interact with the embedder. | 53 // |client| is used to interact with the embedder. |
47 // |local_state| is the PrefService that this instance should use. | 54 // |local_state| is the PrefService that this instance should use. |
48 // Note: |this| instance does not take ownership of the |client|, but rather | 55 // Note: |this| instance does not take ownership of the |client|, but rather |
49 // stores a weak pointer to it. The caller should ensure that the |client| is | 56 // stores a weak pointer to it. The caller should ensure that the |client| is |
50 // valid for the lifetime of this class. | 57 // valid for the lifetime of this class. |
51 MetricsLog(const std::string& client_id, | 58 MetricsLog(const std::string& client_id, |
52 int session_id, | 59 int session_id, |
53 LogType log_type, | 60 LogType log_type, |
54 metrics::MetricsServiceClient* client, | 61 metrics::MetricsServiceClient* client, |
55 PrefService* local_state); | 62 PrefService* local_state); |
56 virtual ~MetricsLog(); | 63 virtual ~MetricsLog(); |
57 | 64 |
58 // Registers local state prefs used by this class. | 65 // Registers local state prefs used by this class. |
59 static void RegisterPrefs(PrefRegistrySimple* registry); | 66 static void RegisterPrefs(PrefRegistrySimple* registry); |
60 | 67 |
| 68 // Computes the MD5 hash of the given string, and returns the first 8 bytes of |
| 69 // the hash. |
| 70 static uint64 Hash(const std::string& value); |
| 71 |
| 72 // Get the GMT buildtime for the current binary, expressed in seconds since |
| 73 // January 1, 1970 GMT. |
| 74 // The value is used to identify when a new build is run, so that previous |
| 75 // reliability stats, from other builds, can be abandoned. |
| 76 static int64 GetBuildTime(); |
| 77 |
| 78 // Convenience function to return the current time at a resolution in seconds. |
| 79 // This wraps base::TimeTicks, and hence provides an abstract time that is |
| 80 // always incrementing for use in measuring time durations. |
| 81 static int64 GetCurrentTime(); |
| 82 |
| 83 // Records a user-initiated action. |
| 84 void RecordUserAction(const std::string& key); |
| 85 |
| 86 // Record any changes in a given histogram for transmission. |
| 87 void RecordHistogramDelta(const std::string& histogram_name, |
| 88 const base::HistogramSamples& snapshot); |
| 89 |
61 // Records the current operating environment, including metrics provided by | 90 // Records the current operating environment, including metrics provided by |
62 // the specified set of |metrics_providers|. Takes the list of installed | 91 // the specified set of |metrics_providers|. Takes the list of installed |
63 // plugins, Google Update statistics, and synthetic trial IDs as parameters | 92 // plugins, Google Update statistics, and synthetic trial IDs as parameters |
64 // because those can't be obtained synchronously from the UI thread. | 93 // because those can't be obtained synchronously from the UI thread. |
65 // A synthetic trial is one that is set up dynamically by code in Chrome. For | 94 // A synthetic trial is one that is set up dynamically by code in Chrome. For |
66 // example, a pref may be mapped to a synthetic trial such that the group | 95 // example, a pref may be mapped to a synthetic trial such that the group |
67 // is determined by the pref value. | 96 // is determined by the pref value. |
68 void RecordEnvironment( | 97 void RecordEnvironment( |
69 const std::vector<metrics::MetricsProvider*>& metrics_providers, | 98 const std::vector<metrics::MetricsProvider*>& metrics_providers, |
70 const std::vector<variations::ActiveGroupId>& synthetic_trials); | 99 const std::vector<variations::ActiveGroupId>& synthetic_trials); |
(...skipping 14 matching lines...) Expand all Loading... |
85 // stats. | 114 // stats. |
86 void RecordStabilityMetrics( | 115 void RecordStabilityMetrics( |
87 const std::vector<metrics::MetricsProvider*>& metrics_providers, | 116 const std::vector<metrics::MetricsProvider*>& metrics_providers, |
88 base::TimeDelta incremental_uptime, | 117 base::TimeDelta incremental_uptime, |
89 base::TimeDelta uptime); | 118 base::TimeDelta uptime); |
90 | 119 |
91 // Records general metrics based on the specified |metrics_providers|. | 120 // Records general metrics based on the specified |metrics_providers|. |
92 void RecordGeneralMetrics( | 121 void RecordGeneralMetrics( |
93 const std::vector<metrics::MetricsProvider*>& metrics_providers); | 122 const std::vector<metrics::MetricsProvider*>& metrics_providers); |
94 | 123 |
| 124 // Stop writing to this record and generate the encoded representation. |
| 125 // None of the Record* methods can be called after this is called. |
| 126 void CloseLog(); |
| 127 |
| 128 // Fills |encoded_log| with the serialized protobuf representation of the |
| 129 // record. Must only be called after CloseLog() has been called. |
| 130 void GetEncodedLog(std::string* encoded_log); |
| 131 |
95 const base::TimeTicks& creation_time() const { | 132 const base::TimeTicks& creation_time() const { |
96 return creation_time_; | 133 return creation_time_; |
97 } | 134 } |
98 | 135 |
| 136 int num_events() const { |
| 137 return uma_proto_.omnibox_event_size() + |
| 138 uma_proto_.user_action_event_size(); |
| 139 } |
| 140 |
| 141 LogType log_type() const { return log_type_; } |
| 142 |
99 protected: | 143 protected: |
100 // Exposed for the sake of mocking in test code. | 144 // Exposed for the sake of mocking/accessing in test code. |
101 | 145 |
102 // Fills |field_trial_ids| with the list of initialized field trials name and | 146 // Fills |field_trial_ids| with the list of initialized field trials name and |
103 // group ids. | 147 // group ids. |
104 virtual void GetFieldTrialIds( | 148 virtual void GetFieldTrialIds( |
105 std::vector<variations::ActiveGroupId>* field_trial_ids) const; | 149 std::vector<variations::ActiveGroupId>* field_trial_ids) const; |
106 | 150 |
| 151 metrics::ChromeUserMetricsExtension* uma_proto() { return &uma_proto_; } |
| 152 const metrics::ChromeUserMetricsExtension* uma_proto() const { |
| 153 return &uma_proto_; |
| 154 } |
| 155 |
107 private: | 156 private: |
108 FRIEND_TEST_ALL_PREFIXES(MetricsLogTest, ChromeOSStabilityData); | |
109 | |
110 // Returns true if the environment has already been filled in by a call to | 157 // Returns true if the environment has already been filled in by a call to |
111 // RecordEnvironment() or LoadSavedEnvironmentFromPrefs(). | 158 // RecordEnvironment() or LoadSavedEnvironmentFromPrefs(). |
112 bool HasEnvironment() const; | 159 bool HasEnvironment() const; |
113 | 160 |
114 // Returns true if the stability metrics have already been filled in by a | 161 // Returns true if the stability metrics have already been filled in by a |
115 // call to RecordStabilityMetrics(). | 162 // call to RecordStabilityMetrics(). |
116 bool HasStabilityMetrics() const; | 163 bool HasStabilityMetrics() const; |
117 | 164 |
118 // Within the stability group, write required attributes. | 165 // Within the stability group, write required attributes. |
119 void WriteRequiredStabilityAttributes(PrefService* pref); | 166 void WriteRequiredStabilityAttributes(PrefService* pref); |
120 | 167 |
121 // Within the stability group, write attributes that need to be updated asap | 168 // Within the stability group, write attributes that need to be updated asap |
122 // and can't be delayed until the user decides to restart chromium. | 169 // and can't be delayed until the user decides to restart chromium. |
123 // Delaying these stats would bias metrics away from happy long lived | 170 // Delaying these stats would bias metrics away from happy long lived |
124 // chromium processes (ones that don't crash, and keep on running). | 171 // chromium processes (ones that don't crash, and keep on running). |
125 void WriteRealtimeStabilityAttributes(PrefService* pref, | 172 void WriteRealtimeStabilityAttributes(PrefService* pref, |
126 base::TimeDelta incremental_uptime, | 173 base::TimeDelta incremental_uptime, |
127 base::TimeDelta uptime); | 174 base::TimeDelta uptime); |
128 | 175 |
| 176 // closed_ is true when record has been packed up for sending, and should |
| 177 // no longer be written to. It is only used for sanity checking. |
| 178 bool closed_; |
| 179 |
| 180 // The type of the log, i.e. initial or ongoing. |
| 181 const LogType log_type_; |
| 182 |
| 183 // Stores the protocol buffer representation for this log. |
| 184 metrics::ChromeUserMetricsExtension uma_proto_; |
| 185 |
129 // Used to interact with the embedder. Weak pointer; must outlive |this| | 186 // Used to interact with the embedder. Weak pointer; must outlive |this| |
130 // instance. | 187 // instance. |
131 metrics::MetricsServiceClient* const client_; | 188 metrics::MetricsServiceClient* const client_; |
132 | 189 |
133 // The time when the current log was created. | 190 // The time when the current log was created. |
134 const base::TimeTicks creation_time_; | 191 const base::TimeTicks creation_time_; |
135 | 192 |
136 PrefService* local_state_; | 193 PrefService* local_state_; |
137 | 194 |
138 DISALLOW_COPY_AND_ASSIGN(MetricsLog); | 195 DISALLOW_COPY_AND_ASSIGN(MetricsLog); |
139 }; | 196 }; |
140 | 197 |
141 #endif // COMPONENTS_METRICS_METRICS_LOG_H_ | 198 #endif // COMPONENTS_METRICS_METRICS_LOG_H_ |
OLD | NEW |