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

Side by Side Diff: chrome/common/metrics_helpers.h

Issue 2744003: Preparation CL for adding crash metrics UMA counters to ChromeFrame. Basicall... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 // 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.
7
8 #ifndef CHROME_COMMON_METRICS_HELPERS_H_
9 #define CHROME_COMMON_METRICS_HELPERS_H_
10
11 #include <map>
12 #include <string>
13 #include <vector>
14
15 #include "base/basictypes.h"
16 #include "base/histogram.h"
17 #include "base/time.h"
18 #include "chrome/common/page_transition_types.h"
19
20 #include "libxml/xmlwriter.h"
21
22 class GURL;
23 class MetricsLog;
24
25 // This class provides base functionality for logging metrics data.
26 class MetricsLogBase {
27 public:
28 // Creates a new metrics log
29 // client_id is the identifier for this profile on this installation
30 // session_id is an integer that's incremented on each application launch
31 MetricsLogBase(const std::string& client_id, int session_id,
32 const std::string& version_string);
33 virtual ~MetricsLogBase();
34
35 // Records a user-initiated action.
36 void RecordUserAction(const char* key);
37
38 enum WindowEventType {
39 WINDOW_CREATE = 0,
40 WINDOW_OPEN,
41 WINDOW_CLOSE,
42 WINDOW_DESTROY
43 };
44
45 void RecordWindowEvent(WindowEventType type, int window_id, int parent_id);
46
47 // Records a page load.
48 // window_id - the index of the tab in which the load took place
49 // url - which URL was loaded
50 // origin - what kind of action initiated the load
51 // load_time - how long it took to load the page
52 void RecordLoadEvent(int window_id,
53 const GURL& url,
54 PageTransition::Type origin,
55 int session_index,
56 base::TimeDelta load_time);
57
58 // Record any changes in a given histogram for transmission.
59 void RecordHistogramDelta(const Histogram& histogram,
60 const Histogram::SampleSet& snapshot);
61
62 // Stop writing to this record and generate the encoded representation.
63 // None of the Record* methods can be called after this is called.
64 void CloseLog();
65
66 // These methods allow retrieval of the encoded representation of the
67 // record. They can only be called after CloseLog() has been called.
68 // GetEncodedLog returns false if buffer_size is less than
69 // GetEncodedLogSize();
70 int GetEncodedLogSize();
71 bool GetEncodedLog(char* buffer, int buffer_size);
72 // Returns an empty string on failure.
73 std::string GetEncodedLogString();
74
75 // Returns the amount of time in seconds that this log has been in use.
76 int GetElapsedSeconds();
77
78 int num_events() { return num_events_; }
79
80 // Creates an MD5 hash of the given value, and returns hash as a byte
81 // buffer encoded as a std::string.
82 static std::string CreateHash(const std::string& value);
83
84 // Return a base64-encoded MD5 hash of the given string.
85 static std::string CreateBase64Hash(const std::string& string);
86
87 // Get the GMT buildtime for the current binary, expressed in seconds since
88 // Januray 1, 1970 GMT.
89 // The value is used to identify when a new build is run, so that previous
90 // reliability stats, from other builds, can be abandoned.
91 static int64 GetBuildTime();
92
93 // Use |extension| in all uploaded appversions in addition to the standard
94 // version string.
95 static void set_version_extension(const std::string& extension) {
96 version_extension_ = extension;
97 }
98
99 virtual MetricsLog* AsMetricsLog() {
100 return NULL;
101 }
102
103 protected:
104 // Returns a string containing the current time.
105 // Virtual so that it can be overridden for testing.
106 virtual std::string GetCurrentTimeString();
107 // Helper class that invokes StartElement from constructor, and EndElement
108 // from destructor.
109 //
110 // Use the macro OPEN_ELEMENT_FOR_SCOPE to help avoid usage problems.
111 class ScopedElement {
112 public:
113 ScopedElement(MetricsLogBase* log, const std::string& name) : log_(log) {
114 DCHECK(log);
115 log->StartElement(name.c_str());
116 }
117
118 ScopedElement(MetricsLogBase* log, const char* name) : log_(log) {
119 DCHECK(log);
120 log->StartElement(name);
121 }
122
123 ~ScopedElement() {
124 log_->EndElement();
125 }
126
127 private:
128 MetricsLogBase* log_;
129 };
130 friend class ScopedElement;
131
132 static const char* WindowEventTypeToString(WindowEventType type);
133
134 // Convenience versions of xmlWriter functions
135 void StartElement(const char* name);
136 void EndElement();
137 void WriteAttribute(const std::string& name, const std::string& value);
138 void WriteIntAttribute(const std::string& name, int value);
139 void WriteInt64Attribute(const std::string& name, int64 value);
140
141 // Write the attributes that are common to every metrics event type.
142 void WriteCommonEventAttributes();
143
144 // An extension that is appended to the appversion in each log.
145 static std::string version_extension_;
146
147 base::Time start_time_;
148 base::Time end_time_;
149
150 std::string client_id_;
151 std::string session_id_;
152
153 // locked_ is true when record has been packed up for sending, and should
154 // no longer be written to. It is only used for sanity checking and is
155 // not a real lock.
156 bool locked_;
157
158 xmlBufferPtr buffer_;
159 xmlTextWriterPtr writer_;
160 int num_events_; // the number of events recorded in this log
161
162 DISALLOW_COPY_AND_ASSIGN(MetricsLogBase);
163 };
164
165 // This class provides base functionality for logging metrics data.
166 // TODO(ananta)
167 // Factor out more common code from chrome and chrome frame metrics service
168 // into this class.
169 class MetricsServiceBase {
170 protected:
171 MetricsServiceBase();
172 virtual ~MetricsServiceBase();
173
174 // Check to see if there is a log that needs to be, or is being, transmitted.
175 bool pending_log() const {
176 return pending_log_ || !pending_log_text_.empty();
177 }
178
179 // Compress the report log in input using bzip2, store the result in output.
180 bool Bzip2Compress(const std::string& input, std::string* output);
181
182 // Discard pending_log_, and clear pending_log_text_. Called after processing
183 // of this log is complete.
184 void DiscardPendingLog();
185
186 // Record complete list of histograms into the current log.
187 // Called when we close a log.
188 void RecordCurrentHistograms();
189
190 // Record a specific histogram .
191 void RecordHistogram(const Histogram& histogram);
192
193 // A log that we are currently transmiting, or about to try to transmit.
194 MetricsLogBase* pending_log_;
195
196 // An alternate form of pending_log_. We persistently save this text version
197 // into prefs if we can't transmit it. As a result, sometimes all we have is
198 // the text version (recalled from a previous session).
199 std::string pending_log_text_;
200
201 // The log that we are still appending to.
202 MetricsLogBase* current_log_;
203
204 // Maintain a map of histogram names to the sample stats we've sent.
205 typedef std::map<std::string, Histogram::SampleSet> LoggedSampleMap;
206
207 // For histograms, record what we've already logged (as a sample for each
208 // histogram) so that we can send only the delta with the next log.
209 LoggedSampleMap logged_samples_;
210 };
211
212 #endif // CHROME_COMMON_METRICS_HELPERS_H_
213
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698