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

Side by Side Diff: components/metrics/persisted_logs.cc

Issue 2358223002: Add log date to the metrics log (Closed)
Patch Set: No explicit migration Created 4 years, 2 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/metrics/persisted_logs.h" 5 #include "components/metrics/persisted_logs.h"
6 6
7 #include <memory>
7 #include <string> 8 #include <string>
9 #include <utility>
8 10
9 #include "base/base64.h" 11 #include "base/base64.h"
10 #include "base/md5.h" 12 #include "base/md5.h"
11 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
12 #include "base/sha1.h" 14 #include "base/sha1.h"
15 #include "base/strings/string_number_conversions.h"
13 #include "base/timer/elapsed_timer.h" 16 #include "base/timer/elapsed_timer.h"
14 #include "components/prefs/pref_service.h" 17 #include "components/prefs/pref_service.h"
15 #include "components/prefs/scoped_user_pref_update.h" 18 #include "components/prefs/scoped_user_pref_update.h"
16 #include "third_party/zlib/google/compression_utils.h" 19 #include "third_party/zlib/google/compression_utils.h"
17 20
18 namespace metrics { 21 namespace metrics {
19 22
20 namespace { 23 namespace {
21 24
25 const char kLogHashKey[] = "hash";
26 const char kLogTimestampKey[] = "timestamp";
27 const char kLogDataKey[] = "data";
28
22 PersistedLogs::LogReadStatus MakeRecallStatusHistogram( 29 PersistedLogs::LogReadStatus MakeRecallStatusHistogram(
23 PersistedLogs::LogReadStatus status) { 30 PersistedLogs::LogReadStatus status) {
24 UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecallProtobufs", 31 UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecallProtobufs",
25 status, PersistedLogs::END_RECALL_STATUS); 32 status, PersistedLogs::END_RECALL_STATUS);
26 return status; 33 return status;
27 } 34 }
28 35
29 // Reads the value at |index| from |list_value| as a string and Base64-decodes 36 // Reads the value at |index| from |list_value| as a string and Base64-decodes
30 // it into |result|. Returns true on success. 37 // it into |result|. Returns true on success.
31 bool ReadBase64String(const base::ListValue& list_value, 38 bool ReadBase64String(const base::ListValue& list_value,
32 size_t index, 39 size_t index,
33 std::string* result) { 40 std::string* result) {
34 std::string base64_result; 41 std::string base64_result;
35 if (!list_value.GetString(index, &base64_result)) 42 if (!list_value.GetString(index, &base64_result))
36 return false; 43 return false;
37 return base::Base64Decode(base64_result, result); 44 return base::Base64Decode(base64_result, result);
38 } 45 }
39 46
40 // Base64-encodes |str| and appends the result to |list_value|. 47 std::string EncodeToBase64(const std::string& to_convert) {
41 void AppendBase64String(const std::string& str, base::ListValue* list_value) { 48 std::string base64_result;
42 std::string base64_str; 49 base::Base64Encode(to_convert, &base64_result);
43 base::Base64Encode(str, &base64_str); 50 return base64_result;
44 list_value->AppendString(base64_str); 51 }
52
53 std::string DecodeFromBase64(const std::string& to_convert) {
54 std::string result;
55 base::Base64Decode(to_convert, &result);
56 return result;
45 } 57 }
46 58
47 } // namespace 59 } // namespace
48 60
49 void PersistedLogs::LogHashPair::Init(const std::string& log_data) { 61 void PersistedLogs::LogInfo::Init(const std::string& log_data) {
50 DCHECK(!log_data.empty()); 62 DCHECK(!log_data.empty());
51 63
52 if (!compression::GzipCompress(log_data, &compressed_log_data)) { 64 if (!compression::GzipCompress(log_data, &compressed_log_data)) {
53 NOTREACHED(); 65 NOTREACHED();
54 return; 66 return;
55 } 67 }
56 68
57 UMA_HISTOGRAM_PERCENTAGE( 69 UMA_HISTOGRAM_PERCENTAGE(
58 "UMA.ProtoCompressionRatio", 70 "UMA.ProtoCompressionRatio",
59 static_cast<int>(100 * compressed_log_data.size() / log_data.size())); 71 static_cast<int>(100 * compressed_log_data.size() / log_data.size()));
60 72
61 hash = base::SHA1HashString(log_data); 73 hash = base::SHA1HashString(log_data);
74 timestamp = base::Int64ToString(
75 (base::Time::Now() - base::Time::UnixEpoch()).InSeconds());
gayane -on leave until 09-2017 2016/09/30 17:01:56 don't use ToTimeT() because it seems to be depreca
62 } 76 }
63 77
64 PersistedLogs::PersistedLogs(PrefService* local_state, 78 PersistedLogs::PersistedLogs(PrefService* local_state,
65 const char* pref_name, 79 const char* pref_name,
80 const char* outdated_pref_name,
66 size_t min_log_count, 81 size_t min_log_count,
67 size_t min_log_bytes, 82 size_t min_log_bytes,
68 size_t max_log_size) 83 size_t max_log_size)
69 : local_state_(local_state), 84 : local_state_(local_state),
70 pref_name_(pref_name), 85 pref_name_(pref_name),
86 outdated_pref_name_(outdated_pref_name),
71 min_log_count_(min_log_count), 87 min_log_count_(min_log_count),
72 min_log_bytes_(min_log_bytes), 88 min_log_bytes_(min_log_bytes),
73 max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)), 89 max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)),
74 staged_log_index_(-1) { 90 staged_log_index_(-1) {
75 DCHECK(local_state_); 91 DCHECK(local_state_);
76 // One of the limit arguments must be non-zero. 92 // One of the limit arguments must be non-zero.
77 DCHECK(min_log_count_ > 0 || min_log_bytes_ > 0); 93 DCHECK(min_log_count_ > 0 || min_log_bytes_ > 0);
78 } 94 }
79 95
80 PersistedLogs::~PersistedLogs() {} 96 PersistedLogs::~PersistedLogs() {}
81 97
82 void PersistedLogs::SerializeLogs() const { 98 void PersistedLogs::SerializeLogs() const {
83 ListPrefUpdate update(local_state_, pref_name_); 99 ListPrefUpdate update(local_state_, pref_name_);
84 WriteLogsToPrefList(update.Get()); 100 WriteLogsToPrefList(update.Get());
101
102 // After writing all the logs to the new pref remove old outdated pref.
103 // TODO(gayane): Remove when all users are migrated. crbug.com/649440
104 if (local_state_->HasPrefPath(outdated_pref_name_))
105 local_state_->ClearPref(outdated_pref_name_);
85 } 106 }
86 107
87 PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() { 108 PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() {
109 // TODO(gayane): Remove the code for reading logs from outdated pref when all
110 // users are migrated. crbug.com/649440
111 if (local_state_->HasPrefPath(outdated_pref_name_))
112 return ReadLogsFromOldFormatPrefList(
113 *local_state_->GetList(outdated_pref_name_));
88 return ReadLogsFromPrefList(*local_state_->GetList(pref_name_)); 114 return ReadLogsFromPrefList(*local_state_->GetList(pref_name_));
89 } 115 }
90 116
91 void PersistedLogs::StoreLog(const std::string& log_data) { 117 void PersistedLogs::StoreLog(const std::string& log_data) {
92 list_.push_back(LogHashPair()); 118 list_.push_back(LogInfo());
93 list_.back().Init(log_data); 119 list_.back().Init(log_data);
94 } 120 }
95 121
96 void PersistedLogs::StageLog() { 122 void PersistedLogs::StageLog() {
97 // CHECK, rather than DCHECK, because swap()ing with an empty list causes 123 // CHECK, rather than DCHECK, because swap()ing with an empty list causes
98 // hard-to-identify crashes much later. 124 // hard-to-identify crashes much later.
99 CHECK(!list_.empty()); 125 CHECK(!list_.empty());
100 DCHECK(!has_staged_log()); 126 DCHECK(!has_staged_log());
101 staged_log_index_ = list_.size() - 1; 127 staged_log_index_ = list_.size() - 1;
102 DCHECK(has_staged_log()); 128 DCHECK(has_staged_log());
103 } 129 }
104 130
105 void PersistedLogs::DiscardStagedLog() { 131 void PersistedLogs::DiscardStagedLog() {
106 DCHECK(has_staged_log()); 132 DCHECK(has_staged_log());
107 DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size()); 133 DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size());
108 list_.erase(list_.begin() + staged_log_index_); 134 list_.erase(list_.begin() + staged_log_index_);
109 staged_log_index_ = -1; 135 staged_log_index_ = -1;
110 } 136 }
111 137
138 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList(
139 const base::ListValue& list_value) {
140 if (list_value.empty())
141 return MakeRecallStatusHistogram(LIST_EMPTY);
142
143 const size_t log_count = list_value.GetSize();
144
145 DCHECK(list_.empty());
146 list_.resize(log_count);
147
148 for (size_t i = 0; i < log_count; ++i) {
149 const base::DictionaryValue* dict;
150 if (!list_value.GetDictionary(i, &dict) ||
151 !dict->GetString(kLogDataKey, &list_[i].compressed_log_data) ||
152 !dict->GetString(kLogHashKey, &list_[i].hash)) {
153 list_.clear();
154 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION);
155 }
156
157 list_[i].compressed_log_data =
158 DecodeFromBase64(list_[i].compressed_log_data);
159 list_[i].hash = DecodeFromBase64(list_[i].hash);
160 // Ignoring the success of this step as timestamp might not be there for
161 // older logs.
162 // NOTE: Should be added to the check with other fields once migration is
163 // over.
164 dict->GetString(kLogTimestampKey, &list_[i].timestamp);
165 }
166
167 return MakeRecallStatusHistogram(RECALL_SUCCESS);
168 }
169
112 void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const { 170 void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const {
113 list_value->Clear(); 171 list_value->Clear();
114 172
115 // Keep the most recent logs which are smaller than |max_log_size_|. 173 // Keep the most recent logs which are smaller than |max_log_size_|.
116 // We keep at least |min_log_bytes_| and |min_log_count_| of logs before 174 // We keep at least |min_log_bytes_| and |min_log_count_| of logs before
117 // discarding older logs. 175 // discarding older logs.
118 size_t start = list_.size(); 176 size_t start = list_.size();
119 size_t saved_log_count = 0; 177 size_t saved_log_count = 0;
120 size_t bytes_used = 0; 178 size_t bytes_used = 0;
121 for (; start > 0; --start) { 179 for (; start > 0; --start) {
(...skipping 11 matching lines...) Expand all
133 int dropped_logs_num = start - 1; 191 int dropped_logs_num = start - 1;
134 192
135 for (size_t i = start; i < list_.size(); ++i) { 193 for (size_t i = start; i < list_.size(); ++i) {
136 size_t log_size = list_[i].compressed_log_data.length(); 194 size_t log_size = list_[i].compressed_log_data.length();
137 if (log_size > max_log_size_) { 195 if (log_size > max_log_size_) {
138 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", 196 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted",
139 static_cast<int>(log_size)); 197 static_cast<int>(log_size));
140 dropped_logs_num++; 198 dropped_logs_num++;
141 continue; 199 continue;
142 } 200 }
143 AppendBase64String(list_[i].compressed_log_data, list_value); 201 std::unique_ptr<base::DictionaryValue> dict_value(
144 AppendBase64String(list_[i].hash, list_value); 202 new base::DictionaryValue);
gayane -on leave until 09-2017 2016/09/30 17:01:56 this changed because the overloaded version of lis
203 dict_value->SetString(kLogHashKey, EncodeToBase64(list_[i].hash));
204 dict_value->SetString(kLogDataKey,
205 EncodeToBase64(list_[i].compressed_log_data));
206 dict_value->SetString(kLogTimestampKey, list_[i].timestamp);
207 list_value->Append(std::move(dict_value));
145 } 208 }
146 if (dropped_logs_num > 0) 209 if (dropped_logs_num > 0)
147 UMA_HISTOGRAM_COUNTS("UMA.UnsentLogs.Dropped", dropped_logs_num); 210 UMA_HISTOGRAM_COUNTS("UMA.UnsentLogs.Dropped", dropped_logs_num);
148 } 211 }
149 212
150 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList( 213 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromOldFormatPrefList(
151 const base::ListValue& list_value) { 214 const base::ListValue& list_value) {
152 if (list_value.empty()) 215 if (list_value.empty())
153 return MakeRecallStatusHistogram(LIST_EMPTY); 216 return MakeRecallStatusHistogram(LIST_EMPTY);
154 217
155 // For each log, there's two entries in the list (the data and the hash). 218 // For each log, there's two entries in the list (the data and the hash).
156 DCHECK_EQ(0U, list_value.GetSize() % 2); 219 DCHECK_EQ(0U, list_value.GetSize() % 2);
157 const size_t log_count = list_value.GetSize() / 2; 220 const size_t log_count = list_value.GetSize() / 2;
158 221
159 // Resize |list_| ahead of time, so that values can be decoded directly into 222 // Resize |list_| ahead of time, so that values can be decoded directly into
160 // the elements of the list. 223 // the elements of the list.
161 DCHECK(list_.empty()); 224 DCHECK(list_.empty());
162 list_.resize(log_count); 225 list_.resize(log_count);
163 226
164 for (size_t i = 0; i < log_count; ++i) { 227 for (size_t i = 0; i < log_count; ++i) {
165 if (!ReadBase64String(list_value, i * 2, &list_[i].compressed_log_data) || 228 if (!ReadBase64String(list_value, i * 2, &list_[i].compressed_log_data) ||
166 !ReadBase64String(list_value, i * 2 + 1, &list_[i].hash)) { 229 !ReadBase64String(list_value, i * 2 + 1, &list_[i].hash)) {
167 list_.clear(); 230 list_.clear();
168 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); 231 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION);
169 } 232 }
170 } 233 }
171 234
172 return MakeRecallStatusHistogram(RECALL_SUCCESS); 235 return MakeRecallStatusHistogram(RECALL_SUCCESS);
173 } 236 }
174 237
175 } // namespace metrics 238 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698