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.
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.
8 #ifndef COMPONENTS_METRICS_METRICS_LOG_H_
9 #define COMPONENTS_METRICS_METRICS_LOG_H_
14 #include "base/basictypes.h"
15 #include "base/time/time.h"
16 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
18 class PrefRegistrySimple
;
22 class DictionaryValue
;
23 class HistogramSamples
;
30 namespace variations
{
36 class MetricsProvider
;
37 class MetricsServiceClient
;
42 INITIAL_STABILITY_LOG
, // The initial log containing stability stats.
43 ONGOING_LOG
, // Subsequent logs in a session.
46 // Creates a new metrics log of the specified type.
47 // |client_id| is the identifier for this profile on this installation
48 // |session_id| is an integer that's incremented on each application launch
49 // |client| is used to interact with the embedder.
50 // |local_state| is the PrefService that this instance should use.
51 // Note: |this| instance does not take ownership of the |client|, but rather
52 // stores a weak pointer to it. The caller should ensure that the |client| is
53 // valid for the lifetime of this class.
54 MetricsLog(const std::string
& client_id
,
57 MetricsServiceClient
* client
,
58 PrefService
* local_state
);
59 virtual ~MetricsLog();
61 // Registers local state prefs used by this class.
62 static void RegisterPrefs(PrefRegistrySimple
* registry
);
64 // Computes the MD5 hash of the given string, and returns the first 8 bytes of
66 static uint64
Hash(const std::string
& value
);
68 // Get the GMT buildtime for the current binary, expressed in seconds since
69 // January 1, 1970 GMT.
70 // The value is used to identify when a new build is run, so that previous
71 // reliability stats, from other builds, can be abandoned.
72 static int64
GetBuildTime();
74 // Convenience function to return the current time at a resolution in seconds.
75 // This wraps base::TimeTicks, and hence provides an abstract time that is
76 // always incrementing for use in measuring time durations.
77 static int64
GetCurrentTime();
79 // Records a user-initiated action.
80 void RecordUserAction(const std::string
& key
);
82 // Record any changes in a given histogram for transmission.
83 void RecordHistogramDelta(const std::string
& histogram_name
,
84 const base::HistogramSamples
& snapshot
);
86 // Records the current operating environment, including metrics provided by
87 // the specified set of |metrics_providers|. Takes the list of installed
88 // plugins, Google Update statistics, and synthetic trial IDs as parameters
89 // because those can't be obtained synchronously from the UI thread.
90 // A synthetic trial is one that is set up dynamically by code in Chrome. For
91 // example, a pref may be mapped to a synthetic trial such that the group
92 // is determined by the pref value.
93 void RecordEnvironment(
94 const std::vector
<MetricsProvider
*>& metrics_providers
,
95 const std::vector
<variations::ActiveGroupId
>& synthetic_trials
,
98 // Loads the environment proto that was saved by the last RecordEnvironment()
99 // call from prefs and clears the pref value. Returns true on success or false
100 // if there was no saved environment in prefs or it could not be decoded.
101 bool LoadSavedEnvironmentFromPrefs();
103 // Writes application stability metrics, including stability metrics provided
104 // by the specified set of |metrics_providers|. The system profile portion of
105 // the log must have already been filled in by a call to RecordEnvironment()
106 // or LoadSavedEnvironmentFromPrefs().
107 // NOTE: Has the side-effect of clearing the stability prefs..
109 // If this log is of type INITIAL_STABILITY_LOG, records additional info such
110 // as number of incomplete shutdowns as well as extra breakpad and debugger
112 void RecordStabilityMetrics(
113 const std::vector
<MetricsProvider
*>& metrics_providers
,
114 base::TimeDelta incremental_uptime
,
115 base::TimeDelta uptime
);
117 // Records general metrics based on the specified |metrics_providers|.
118 void RecordGeneralMetrics(
119 const std::vector
<MetricsProvider
*>& metrics_providers
);
121 // Stop writing to this record and generate the encoded representation.
122 // None of the Record* methods can be called after this is called.
125 // Fills |encoded_log| with the serialized protobuf representation of the
126 // record. Must only be called after CloseLog() has been called.
127 void GetEncodedLog(std::string
* encoded_log
);
129 const base::TimeTicks
& creation_time() const {
130 return creation_time_
;
133 int num_events() const {
134 return uma_proto_
.omnibox_event_size() +
135 uma_proto_
.user_action_event_size();
138 LogType
log_type() const { return log_type_
; }
141 // Exposed for the sake of mocking/accessing in test code.
143 // Fills |field_trial_ids| with the list of initialized field trials name and
145 virtual void GetFieldTrialIds(
146 std::vector
<variations::ActiveGroupId
>* field_trial_ids
) const;
148 ChromeUserMetricsExtension
* uma_proto() { return &uma_proto_
; }
149 const ChromeUserMetricsExtension
* uma_proto() const {
154 // Returns true if the environment has already been filled in by a call to
155 // RecordEnvironment() or LoadSavedEnvironmentFromPrefs().
156 bool HasEnvironment() const;
158 // Returns true if the stability metrics have already been filled in by a
159 // call to RecordStabilityMetrics().
160 bool HasStabilityMetrics() const;
162 // Within the stability group, write required attributes.
163 void WriteRequiredStabilityAttributes(PrefService
* pref
);
165 // Within the stability group, write attributes that need to be updated asap
166 // and can't be delayed until the user decides to restart chromium.
167 // Delaying these stats would bias metrics away from happy long lived
168 // chromium processes (ones that don't crash, and keep on running).
169 void WriteRealtimeStabilityAttributes(PrefService
* pref
,
170 base::TimeDelta incremental_uptime
,
171 base::TimeDelta uptime
);
173 // closed_ is true when record has been packed up for sending, and should
174 // no longer be written to. It is only used for sanity checking.
177 // The type of the log, i.e. initial or ongoing.
178 const LogType log_type_
;
180 // Stores the protocol buffer representation for this log.
181 ChromeUserMetricsExtension uma_proto_
;
183 // Used to interact with the embedder. Weak pointer; must outlive |this|
185 MetricsServiceClient
* const client_
;
187 // The time when the current log was created.
188 const base::TimeTicks creation_time_
;
190 PrefService
* local_state_
;
192 DISALLOW_COPY_AND_ASSIGN(MetricsLog
);
195 } // namespace metrics
197 #endif // COMPONENTS_METRICS_METRICS_LOG_H_