OLD | NEW |
| (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 // 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 COMPONENTS_METRICS_METRICS_LOG_BASE_H_ | |
9 #define COMPONENTS_METRICS_METRICS_LOG_BASE_H_ | |
10 | |
11 #include <string> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/metrics/histogram.h" | |
15 #include "base/time/time.h" | |
16 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h" | |
17 | |
18 namespace base { | |
19 class HistogramSamples; | |
20 } // namespace base | |
21 | |
22 namespace metrics { | |
23 | |
24 // This class provides base functionality for logging metrics data. | |
25 class MetricsLogBase { | |
26 public: | |
27 enum LogType { | |
28 INITIAL_STABILITY_LOG, // The initial log containing stability stats. | |
29 ONGOING_LOG, // Subsequent logs in a session. | |
30 }; | |
31 | |
32 // Creates a new metrics log of the specified type. | |
33 // client_id is the identifier for this profile on this installation | |
34 // session_id is an integer that's incremented on each application launch | |
35 MetricsLogBase(const std::string& client_id, | |
36 int session_id, | |
37 LogType log_type, | |
38 const std::string& version_string); | |
39 virtual ~MetricsLogBase(); | |
40 | |
41 // Computes the MD5 hash of the given string, and returns the first 8 bytes of | |
42 // the hash. | |
43 static uint64 Hash(const std::string& value); | |
44 | |
45 // Get the GMT buildtime for the current binary, expressed in seconds since | |
46 // January 1, 1970 GMT. | |
47 // The value is used to identify when a new build is run, so that previous | |
48 // reliability stats, from other builds, can be abandoned. | |
49 static int64 GetBuildTime(); | |
50 | |
51 // Convenience function to return the current time at a resolution in seconds. | |
52 // This wraps base::TimeTicks, and hence provides an abstract time that is | |
53 // always incrementing for use in measuring time durations. | |
54 static int64 GetCurrentTime(); | |
55 | |
56 // Records a user-initiated action. | |
57 void RecordUserAction(const std::string& key); | |
58 | |
59 // Record any changes in a given histogram for transmission. | |
60 void RecordHistogramDelta(const std::string& histogram_name, | |
61 const base::HistogramSamples& snapshot); | |
62 | |
63 // Stop writing to this record and generate the encoded representation. | |
64 // None of the Record* methods can be called after this is called. | |
65 void CloseLog(); | |
66 | |
67 // Fills |encoded_log| with the serialized protobuf representation of the | |
68 // record. Must only be called after CloseLog() has been called. | |
69 void GetEncodedLog(std::string* encoded_log); | |
70 | |
71 int num_events() const { | |
72 return uma_proto_.omnibox_event_size() + | |
73 uma_proto_.user_action_event_size(); | |
74 } | |
75 | |
76 LogType log_type() const { return log_type_; } | |
77 | |
78 protected: | |
79 bool locked() const { return locked_; } | |
80 | |
81 metrics::ChromeUserMetricsExtension* uma_proto() { return &uma_proto_; } | |
82 const metrics::ChromeUserMetricsExtension* uma_proto() const { | |
83 return &uma_proto_; | |
84 } | |
85 | |
86 private: | |
87 // locked_ is true when record has been packed up for sending, and should | |
88 // no longer be written to. It is only used for sanity checking and is | |
89 // not a real lock. | |
90 bool locked_; | |
91 | |
92 // The type of the log, i.e. initial or ongoing. | |
93 const LogType log_type_; | |
94 | |
95 // Stores the protocol buffer representation for this log. | |
96 metrics::ChromeUserMetricsExtension uma_proto_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(MetricsLogBase); | |
99 }; | |
100 | |
101 } // namespace metrics | |
102 | |
103 #endif // COMPONENTS_METRICS_METRICS_LOG_BASE_H_ | |
OLD | NEW |