• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // This file defines a set of user experience metrics data recorded by
18 // the MetricsService.  This is the unit of data that is sent to the server.
19 
20 #ifndef METRICS_UPLOADER_METRICS_LOG_BASE_H_
21 #define METRICS_UPLOADER_METRICS_LOG_BASE_H_
22 
23 #include <string>
24 
25 #include "base/macros.h"
26 #include "base/metrics/histogram.h"
27 #include "base/time/time.h"
28 #include "uploader/proto/chrome_user_metrics_extension.pb.h"
29 
30 namespace base {
31 class HistogramSamples;
32 }  // namespace base
33 
34 namespace metrics {
35 
36 // This class provides base functionality for logging metrics data.
37 class MetricsLogBase {
38  public:
39   // TODO(asvitkine): Remove the NO_LOG value.
40   enum LogType {
41     INITIAL_STABILITY_LOG,  // The initial log containing stability stats.
42     ONGOING_LOG,            // Subsequent logs in a session.
43     NO_LOG,                 // Placeholder value for when there is no log.
44   };
45 
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   MetricsLogBase(const std::string& client_id,
50                  int session_id,
51                  LogType log_type,
52                  const std::string& version_string);
53   virtual ~MetricsLogBase();
54 
55   // Computes the MD5 hash of the given string, and returns the first 8 bytes of
56   // the hash.
57   static uint64_t Hash(const std::string& value);
58 
59   // Get the GMT buildtime for the current binary, expressed in seconds since
60   // January 1, 1970 GMT.
61   // The value is used to identify when a new build is run, so that previous
62   // reliability stats, from other builds, can be abandoned.
63   static int64_t GetBuildTime();
64 
65   // Convenience function to return the current time at a resolution in seconds.
66   // This wraps base::TimeTicks, and hence provides an abstract time that is
67   // always incrementing for use in measuring time durations.
68   static int64_t GetCurrentTime();
69 
70   // Records a user-initiated action.
71   void RecordUserAction(const std::string& key);
72 
73   // Record any changes in a given histogram for transmission.
74   void RecordHistogramDelta(const std::string& histogram_name,
75                             const base::HistogramSamples& snapshot);
76 
77   // Stop writing to this record and generate the encoded representation.
78   // None of the Record* methods can be called after this is called.
79   void CloseLog();
80 
81   // Fills |encoded_log| with the serialized protobuf representation of the
82   // record.  Must only be called after CloseLog() has been called.
83   void GetEncodedLog(std::string* encoded_log);
84 
num_events()85   int num_events() { return num_events_; }
86 
set_hardware_class(const std::string & hardware_class)87   void set_hardware_class(const std::string& hardware_class) {
88     uma_proto_.mutable_system_profile()->mutable_hardware()->set_hardware_class(
89         hardware_class);
90   }
91 
log_type()92   LogType log_type() const { return log_type_; }
93 
94  protected:
locked()95   bool locked() const { return locked_; }
96 
uma_proto()97   metrics::ChromeUserMetricsExtension* uma_proto() { return &uma_proto_; }
uma_proto()98   const metrics::ChromeUserMetricsExtension* uma_proto() const {
99     return &uma_proto_;
100   }
101 
102   // TODO(isherman): Remove this once the XML pipeline is outta here.
103   int num_events_;  // the number of events recorded in this log
104 
105  private:
106   // locked_ is true when record has been packed up for sending, and should
107   // no longer be written to.  It is only used for sanity checking and is
108   // not a real lock.
109   bool locked_;
110 
111   // The type of the log, i.e. initial or ongoing.
112   const LogType log_type_;
113 
114   // Stores the protocol buffer representation for this log.
115   metrics::ChromeUserMetricsExtension uma_proto_;
116 
117   DISALLOW_COPY_AND_ASSIGN(MetricsLogBase);
118 };
119 
120 }  // namespace metrics
121 
122 #endif  // METRICS_UPLOADER_METRICS_LOG_BASE_H_
123