Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 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 | */ |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef METRICS_UPLOADER_UPLOAD_SERVICE_H_ |
| 18 | #define METRICS_UPLOADER_UPLOAD_SERVICE_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | #include "base/metrics/histogram_base.h" |
| 23 | #include "base/metrics/histogram_flattener.h" |
| 24 | #include "base/metrics/histogram_snapshot_manager.h" |
Bertrand SIMONNET | e4fa61e | 2015-02-18 09:38:55 -0800 | [diff] [blame] | 25 | |
| 26 | #include "metrics/metrics_library.h" |
Bertrand SIMONNET | 1df10c4 | 2015-09-09 10:39:51 -0700 | [diff] [blame] | 27 | #include "persistent_integer.h" |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 28 | #include "uploader/metrics_log.h" |
| 29 | #include "uploader/sender.h" |
| 30 | #include "uploader/system_profile_cache.h" |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 31 | |
| 32 | namespace metrics { |
| 33 | class ChromeUserMetricsExtension; |
| 34 | class CrashSample; |
| 35 | class HistogramSample; |
| 36 | class LinearHistogramSample; |
| 37 | class MetricSample; |
| 38 | class SparseHistogramSample; |
| 39 | class UserActionSample; |
| 40 | } |
| 41 | |
| 42 | class SystemProfileSetter; |
| 43 | |
| 44 | // Service responsible for uploading the metrics periodically to the server. |
| 45 | // This service works as a simple 2-state state-machine. |
| 46 | // |
| 47 | // The two states are the presence or not of a staged log. |
| 48 | // A staged log is a compressed protobuffer containing both the aggregated |
| 49 | // metrics and event and information about the client. (product, hardware id, |
| 50 | // etc...). |
| 51 | // |
| 52 | // At regular intervals, the upload event will be triggered and the following |
| 53 | // will happen: |
| 54 | // * if a staged log is present: |
| 55 | // The previous upload may have failed for various reason. We then retry to |
| 56 | // upload the same log. |
| 57 | // - if the upload is successful, we discard the log (therefore |
| 58 | // transitioning back to no staged log) |
| 59 | // - if the upload fails, we keep the log to try again later. |
| 60 | // We do not try to read the metrics that are stored on |
| 61 | // the disk as we want to avoid storing the metrics in memory. |
| 62 | // |
| 63 | // * if no staged logs are present: |
| 64 | // Read all metrics from the disk, aggregate them and try to send them. |
| 65 | // - if the upload succeeds, we discard the staged log (transitioning back |
| 66 | // to the no staged log state) |
| 67 | // - if the upload fails, we keep the staged log in memory to retry |
| 68 | // uploading later. |
| 69 | // |
| 70 | class UploadService : public base::HistogramFlattener { |
| 71 | public: |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 72 | explicit UploadService(SystemProfileSetter* setter, |
Bertrand SIMONNET | e4fa61e | 2015-02-18 09:38:55 -0800 | [diff] [blame] | 73 | MetricsLibraryInterface* metrics_lib, |
Bertrand SIMONNET | 71a62ef | 2014-10-07 11:26:25 -0700 | [diff] [blame] | 74 | const std::string& server); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 75 | |
Bertrand SIMONNET | cac74e1 | 2014-10-09 10:14:13 -0700 | [diff] [blame] | 76 | void Init(const base::TimeDelta& upload_interval, |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 77 | const base::FilePath& metrics_directory); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 78 | |
| 79 | // Starts a new log. The log needs to be regenerated after each successful |
| 80 | // launch as it is destroyed when staging the log. |
| 81 | void StartNewLog(); |
| 82 | |
Steve Fung | ae4bdc4 | 2015-01-26 17:13:24 -0800 | [diff] [blame] | 83 | // Event callback for handling MessageLoop events. |
| 84 | void UploadEventCallback(const base::TimeDelta& interval); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 85 | |
| 86 | // Triggers an upload event. |
| 87 | void UploadEvent(); |
| 88 | |
| 89 | // Sends the staged log. |
| 90 | void SendStagedLog(); |
| 91 | |
| 92 | // Implements inconsistency detection to match HistogramFlattener's |
| 93 | // interface. |
| 94 | void InconsistencyDetected( |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 95 | base::HistogramBase::Inconsistency problem) override {} |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 96 | void UniqueInconsistencyDetected( |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 97 | base::HistogramBase::Inconsistency problem) override {} |
| 98 | void InconsistencyDetectedInLoggedCount(int amount) override {} |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 99 | |
| 100 | private: |
| 101 | friend class UploadServiceTest; |
| 102 | |
| 103 | FRIEND_TEST(UploadServiceTest, CanSendMultipleTimes); |
| 104 | FRIEND_TEST(UploadServiceTest, DiscardLogsAfterTooManyFailedUpload); |
| 105 | FRIEND_TEST(UploadServiceTest, EmptyLogsAreNotSent); |
| 106 | FRIEND_TEST(UploadServiceTest, FailedSendAreRetried); |
| 107 | FRIEND_TEST(UploadServiceTest, LogContainsAggregatedValues); |
| 108 | FRIEND_TEST(UploadServiceTest, LogEmptyAfterUpload); |
| 109 | FRIEND_TEST(UploadServiceTest, LogEmptyByDefault); |
Bertrand SIMONNET | 4c8a8ad | 2015-09-09 13:27:33 -0700 | [diff] [blame] | 110 | FRIEND_TEST(UploadServiceTest, LogFromTheMetricsLibrary); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 111 | FRIEND_TEST(UploadServiceTest, LogKernelCrash); |
| 112 | FRIEND_TEST(UploadServiceTest, LogUncleanShutdown); |
| 113 | FRIEND_TEST(UploadServiceTest, LogUserCrash); |
| 114 | FRIEND_TEST(UploadServiceTest, UnknownCrashIgnored); |
| 115 | FRIEND_TEST(UploadServiceTest, ValuesInConfigFileAreSent); |
| 116 | |
Steve Fung | ae4bdc4 | 2015-01-26 17:13:24 -0800 | [diff] [blame] | 117 | // Private constructor for use in unit testing. |
| 118 | UploadService(SystemProfileSetter* setter, |
Bertrand SIMONNET | e4fa61e | 2015-02-18 09:38:55 -0800 | [diff] [blame] | 119 | MetricsLibraryInterface* metrics_lib, |
Steve Fung | ae4bdc4 | 2015-01-26 17:13:24 -0800 | [diff] [blame] | 120 | const std::string& server, |
| 121 | bool testing); |
| 122 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 123 | // If a staged log fails to upload more than kMaxFailedUpload times, it |
| 124 | // will be discarded. |
| 125 | static const int kMaxFailedUpload; |
| 126 | |
| 127 | // Resets the internal state. |
| 128 | void Reset(); |
| 129 | |
| 130 | // Reads all the metrics from the disk. |
| 131 | void ReadMetrics(); |
| 132 | |
| 133 | // Adds a generic sample to the current log. |
| 134 | void AddSample(const metrics::MetricSample& sample); |
| 135 | |
| 136 | // Adds a crash to the current log. |
| 137 | void AddCrash(const std::string& crash_name); |
| 138 | |
| 139 | // Aggregates all histogram available in memory and store them in the current |
| 140 | // log. |
| 141 | void GatherHistograms(); |
| 142 | |
| 143 | // Callback for HistogramSnapshotManager to store the histograms. |
| 144 | void RecordDelta(const base::HistogramBase& histogram, |
Alex Vakulenko | e8a8e30 | 2014-08-14 12:56:21 -0700 | [diff] [blame] | 145 | const base::HistogramSamples& snapshot) override; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 146 | |
| 147 | // Compiles all the samples received into a single protobuf and adds all |
| 148 | // system information. |
| 149 | void StageCurrentLog(); |
| 150 | |
Bertrand SIMONNET | 1df10c4 | 2015-09-09 10:39:51 -0700 | [diff] [blame] | 151 | // Returns true iff a log is staged. |
| 152 | bool HasStagedLog(); |
| 153 | |
| 154 | // Remove the staged log iff the upload failed more than |kMaxFailedUpload|. |
| 155 | void RemoveFailedLog(); |
| 156 | |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 157 | // Returns the current log. If there is no current log, creates it first. |
| 158 | MetricsLog* GetOrCreateCurrentLog(); |
| 159 | |
Yunlian Jiang | e2c2d89 | 2014-09-02 10:40:19 -0700 | [diff] [blame] | 160 | scoped_ptr<SystemProfileSetter> system_profile_setter_; |
Bertrand SIMONNET | e4fa61e | 2015-02-18 09:38:55 -0800 | [diff] [blame] | 161 | MetricsLibraryInterface* metrics_lib_; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 162 | base::HistogramSnapshotManager histogram_snapshot_manager_; |
Yunlian Jiang | b1640ee | 2014-08-27 16:22:19 -0700 | [diff] [blame] | 163 | scoped_ptr<Sender> sender_; |
Bertrand SIMONNET | 1df10c4 | 2015-09-09 10:39:51 -0700 | [diff] [blame] | 164 | chromeos_metrics::PersistentInteger failed_upload_count_; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 165 | scoped_ptr<MetricsLog> current_log_; |
Steve Fung | 67906c6 | 2014-10-06 15:15:30 -0700 | [diff] [blame] | 166 | |
Bertrand SIMONNET | 2765d0a | 2015-09-09 10:38:20 -0700 | [diff] [blame] | 167 | base::FilePath metrics_file_; |
Bertrand SIMONNET | 1df10c4 | 2015-09-09 10:39:51 -0700 | [diff] [blame] | 168 | base::FilePath staged_log_path_; |
Steve Fung | ae4bdc4 | 2015-01-26 17:13:24 -0800 | [diff] [blame] | 169 | |
| 170 | bool testing_; |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | #endif // METRICS_UPLOADER_UPLOAD_SERVICE_H_ |