OLD | NEW |
---|---|
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 "google_apis/gcm/monitoring/gcm_stats_recorder.h" | 5 #include "google_apis/gcm/monitoring/gcm_stats_recorder.h" |
6 | 6 |
7 #include <deque> | 7 #include <deque> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/format_macros.h" | |
10 #include "base/logging.h" | 11 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
14 | 15 |
15 namespace gcm { | 16 namespace gcm { |
16 | 17 |
17 const uint32 MAX_LOGGED_ACTIVITY_COUNT = 100; | 18 const uint32 MAX_LOGGED_ACTIVITY_COUNT = 100; |
18 | 19 |
19 namespace { | 20 namespace { |
(...skipping 27 matching lines...) Expand all Loading... | |
47 case gcm::MCSClient::NO_CONNECTION_ON_ZERO_TTL: | 48 case gcm::MCSClient::NO_CONNECTION_ON_ZERO_TTL: |
48 return "NO_CONNECTION_ON_ZERO_TTL"; | 49 return "NO_CONNECTION_ON_ZERO_TTL"; |
49 case gcm::MCSClient::TTL_EXCEEDED: | 50 case gcm::MCSClient::TTL_EXCEEDED: |
50 return "TTL_EXCEEDED"; | 51 return "TTL_EXCEEDED"; |
51 default: | 52 default: |
52 NOTREACHED(); | 53 NOTREACHED(); |
53 return "UNKNOWN"; | 54 return "UNKNOWN"; |
54 } | 55 } |
55 } | 56 } |
56 | 57 |
58 // Helper for getting string representation of the | |
59 // ConnectionFactory::ConnectionResetReason enum. | |
60 std::string GetConnectionResetReasonString( | |
61 gcm::ConnectionFactory::ConnectionResetReason reason) { | |
62 switch (reason) { | |
63 case gcm::ConnectionFactory::LOGIN_FAILURE: | |
64 return "LOGIN_FAILURE"; | |
65 case gcm::ConnectionFactory::CLOSE_COMMAND: | |
66 return "CLOSE_COMMAND"; | |
67 case gcm::ConnectionFactory::HEARTBEAT_FAILURE: | |
68 return "HEARTBEAT_FAILURE"; | |
69 case gcm::ConnectionFactory::SOCKET_FAILURE: | |
70 return "SOCKET_FAILURE"; | |
71 case gcm::ConnectionFactory::NETWORK_CHANGE: | |
72 return "NETWORK_CHANGE"; | |
73 default: | |
74 NOTREACHED(); | |
75 return "UNKNOWN_REASON"; | |
76 } | |
77 } | |
78 | |
79 // Helper for getting string representation of the RegistrationRequest::Status | |
80 // enum. | |
81 std::string GetRegistrationStatusString( | |
82 gcm::RegistrationRequest::Status status) { | |
83 switch (status) { | |
84 case gcm::RegistrationRequest::SUCCESS: | |
85 return "SUCCESS"; | |
86 case gcm::RegistrationRequest::INVALID_PARAMETERS: | |
87 return "INVALID_PARAMETERS"; | |
88 case gcm::RegistrationRequest::INVALID_SENDER: | |
89 return "INVALID_SENDER"; | |
90 case gcm::RegistrationRequest::AUTHENTICATION_FAILED: | |
91 return "AUTHENTICATION_FAILED"; | |
92 case gcm::RegistrationRequest::DEVICE_REGISTRATION_ERROR: | |
93 return "DEVICE_REGISTRATION_ERROR"; | |
94 case gcm::RegistrationRequest::UNKNOWN_ERROR: | |
95 return "UNKNOWN_ERROR"; | |
96 case gcm::RegistrationRequest::URL_FETCHING_FAILED: | |
97 return "URL_FETCHING_FAILED"; | |
98 case gcm::RegistrationRequest::HTTP_NOT_OK: | |
99 return "HTTP_NOT_OK"; | |
100 case gcm::RegistrationRequest::RESPONSE_PARSING_FAILED: | |
101 return "RESPONSE_PARSING_FAILED"; | |
102 case gcm::RegistrationRequest::REACHED_MAX_RETRIES: | |
103 return "REACHED_MAX_RETRIES"; | |
104 default: | |
105 NOTREACHED(); | |
106 return "UNKNOWN_STATUS"; | |
107 } | |
108 } | |
109 | |
110 // Helper for getting string representation of the RegistrationRequest::Status | |
111 // enum. | |
112 std::string GetUnregistrationStatusString( | |
113 gcm::UnregistrationRequest::Status status) { | |
114 switch (status) { | |
115 case gcm::UnregistrationRequest::SUCCESS: | |
116 return "SUCCESS"; | |
117 case gcm::UnregistrationRequest::URL_FETCHING_FAILED: | |
118 return "URL_FETCHING_FAILED"; | |
119 case gcm::UnregistrationRequest::NO_RESPONSE_BODY: | |
120 return "NO_RESPONSE_BODY"; | |
121 case gcm::UnregistrationRequest::RESPONSE_PARSING_FAILED: | |
122 return "RESPONSE_PARSING_FAILED"; | |
123 case gcm::UnregistrationRequest::INCORRECT_APP_ID: | |
124 return "INCORRECT_APP_ID"; | |
125 case gcm::UnregistrationRequest::INVALID_PARAMETERS: | |
126 return "INVALID_PARAMETERS"; | |
127 case gcm::UnregistrationRequest::SERVICE_UNAVAILABLE: | |
128 return "SERVICE_UNAVAILABLE"; | |
129 case gcm::UnregistrationRequest::INTERNAL_SERVER_ERROR: | |
130 return "INTERNAL_SERVER_ERROR"; | |
131 case gcm::UnregistrationRequest::HTTP_NOT_OK: | |
132 return "HTTP_NOT_OK"; | |
133 case gcm::UnregistrationRequest::UNKNOWN_ERROR: | |
134 return "UNKNOWN_ERROR"; | |
135 default: | |
136 NOTREACHED(); | |
137 return "UNKNOWN_STATUS"; | |
138 } | |
139 } | |
140 | |
57 } // namespace | 141 } // namespace |
58 | 142 |
59 GCMStatsRecorder::Activity::Activity() | 143 GCMStatsRecorder::Activity::Activity() |
60 : time(base::Time::Now()) { | 144 : time(base::Time::Now()) { |
61 } | 145 } |
62 | 146 |
63 GCMStatsRecorder::Activity::~Activity() { | 147 GCMStatsRecorder::Activity::~Activity() { |
64 } | 148 } |
65 | 149 |
150 GCMStatsRecorder::ConnectionActivity::ConnectionActivity() { | |
151 } | |
152 | |
153 GCMStatsRecorder::ConnectionActivity::~ConnectionActivity() { | |
154 } | |
155 | |
156 GCMStatsRecorder::RegistrationActivity::RegistrationActivity() { | |
157 } | |
158 | |
159 GCMStatsRecorder::RegistrationActivity::~RegistrationActivity() { | |
160 } | |
161 | |
162 GCMStatsRecorder::ReceivingActivity::ReceivingActivity() | |
163 : message_byte_size(0) { | |
164 } | |
165 | |
166 GCMStatsRecorder::ReceivingActivity::~ReceivingActivity() { | |
167 } | |
168 | |
66 GCMStatsRecorder::SendingActivity::SendingActivity() { | 169 GCMStatsRecorder::SendingActivity::SendingActivity() { |
67 } | 170 } |
68 | 171 |
69 GCMStatsRecorder::SendingActivity::~SendingActivity() { | 172 GCMStatsRecorder::SendingActivity::~SendingActivity() { |
70 } | 173 } |
71 | 174 |
72 GCMStatsRecorder::GCMStatsRecorder() : is_recording_(false) { | 175 GCMStatsRecorder::GCMStatsRecorder() : is_recording_(false) { |
73 } | 176 } |
74 | 177 |
75 GCMStatsRecorder::~GCMStatsRecorder() { | 178 GCMStatsRecorder::~GCMStatsRecorder() { |
76 } | 179 } |
77 | 180 |
78 void GCMStatsRecorder::SetRecording(bool recording) { | 181 void GCMStatsRecorder::SetRecording(bool recording) { |
79 is_recording_ = recording; | 182 is_recording_ = recording; |
80 } | 183 } |
81 | 184 |
82 void GCMStatsRecorder::Clear() { | 185 void GCMStatsRecorder::Clear() { |
83 sending_activities_.clear(); | 186 sending_activities_.clear(); |
84 } | 187 } |
85 | 188 |
86 void GCMStatsRecorder::CollectSendingActivities( | 189 void GCMStatsRecorder::RecordConnection( |
87 std::vector<SendingActivity>* activities) const { | 190 const std::string& event, |
88 activities->insert(activities->begin(), | 191 const std::string& details) { |
89 sending_activities_.begin(), | 192 ConnectionActivity data; |
90 sending_activities_.end()); | 193 ConnectionActivity* inserted_data = InsertCircularBuffer( |
194 &connection_activities_, data); | |
195 inserted_data->event = event; | |
196 inserted_data->details = details; | |
197 } | |
198 | |
199 void GCMStatsRecorder::RecordConnectionInitiated(const std::string& host) { | |
200 if (is_recording_) | |
Nicolas Zea
2014/04/23 23:37:04
I think it's clearer to just do if (!is_recording)
juyik
2014/04/24 00:53:45
Done.
| |
201 RecordConnection("Connection initiated", host); | |
202 } | |
203 | |
204 void GCMStatsRecorder::RecordConnectionDelayedDueToBackoff(int64 delay_msec) { | |
205 if (is_recording_) { | |
206 RecordConnection("Connection backoff", | |
207 base::StringPrintf("Delayed for %" PRId64 " msec", | |
208 delay_msec)); | |
209 } | |
210 } | |
211 | |
212 void GCMStatsRecorder::RecordConnectionSuccess() { | |
213 if (is_recording_) { | |
214 RecordConnection("Connection succeeded", std::string()); | |
215 } | |
216 } | |
217 | |
218 void GCMStatsRecorder::RecordConnectionResetSignaled( | |
219 ConnectionFactory::ConnectionResetReason reason) { | |
220 if (is_recording_) { | |
221 RecordConnection("Connection reset", | |
222 GetConnectionResetReasonString(reason)); | |
223 } | |
224 } | |
225 | |
226 void GCMStatsRecorder::RecordRegistration( | |
227 const std::string& app_id, | |
228 const std::string& sender_ids, | |
229 const std::string& event, | |
230 const std::string& details) { | |
231 RegistrationActivity data; | |
232 RegistrationActivity* inserted_data = InsertCircularBuffer( | |
233 ®istration_activities_, data); | |
234 inserted_data->app_id = app_id; | |
235 inserted_data->sender_ids = sender_ids; | |
236 inserted_data->event = event; | |
237 inserted_data->details = details; | |
238 } | |
239 | |
240 void GCMStatsRecorder::RecordRegistrationSent( | |
241 const std::string& app_id, | |
242 const std::string& sender_ids) { | |
243 UMA_HISTOGRAM_COUNTS("GCM.RegistrationRequest", 1); | |
244 if (is_recording_) { | |
245 RecordRegistration(app_id, sender_ids, | |
246 "Registration request sent", std::string()); | |
247 } | |
248 } | |
249 | |
250 void GCMStatsRecorder::RecordRegistrationResponse( | |
251 const std::string& app_id, | |
252 const std::vector<std::string>& sender_ids, | |
253 RegistrationRequest::Status status) { | |
254 if (is_recording_) { | |
255 RecordRegistration(app_id, JoinString(sender_ids, ","), | |
256 "Registration response received", | |
257 GetRegistrationStatusString(status)); | |
258 } | |
259 } | |
260 | |
261 void GCMStatsRecorder::RecordRegistrationRetryRequested( | |
262 const std::string& app_id, | |
263 const std::vector<std::string>& sender_ids, | |
264 int retries_left) { | |
265 if (is_recording_) { | |
266 RecordRegistration(app_id, JoinString(sender_ids, ","), | |
267 "Registration retry requested", | |
268 base::StringPrintf("Retries left: %d", retries_left)); | |
269 } | |
270 } | |
271 | |
272 void GCMStatsRecorder::RecordUnregistrationSent( | |
273 const std::string& app_id) { | |
274 UMA_HISTOGRAM_COUNTS("GCM.UnregistrationRequest", 1); | |
275 if (is_recording_) { | |
276 RecordRegistration(app_id, std::string(), "Unregistration request sent", | |
277 std::string()); | |
278 } | |
279 } | |
280 | |
281 void GCMStatsRecorder::RecordUnregistrationResponse( | |
282 const std::string& app_id, | |
283 UnregistrationRequest::Status status) { | |
284 if (is_recording_) { | |
285 RecordRegistration(app_id, | |
286 std::string(), | |
287 "Unregistration response received", | |
288 GetUnregistrationStatusString(status)); | |
289 } | |
290 } | |
291 | |
292 void GCMStatsRecorder::RecordUnregistrationRetryDelayed( | |
293 const std::string& app_id, | |
294 int64 delay_msec) { | |
295 if (is_recording_) { | |
296 RecordRegistration(app_id, | |
297 std::string(), | |
298 "Unregistration retry delayed", | |
299 base::StringPrintf("Delayed for %" PRId64 " msec", | |
300 delay_msec)); | |
301 } | |
302 } | |
303 | |
304 void GCMStatsRecorder::RecordReceiving( | |
305 const std::string& app_id, | |
306 const std::string& from, | |
307 int message_byte_size, | |
308 const std::string& event, | |
309 const std::string& details) { | |
310 ReceivingActivity data; | |
311 ReceivingActivity* inserted_data = InsertCircularBuffer( | |
312 &receiving_activities_, data); | |
313 inserted_data->app_id = app_id; | |
314 inserted_data->from = from; | |
315 inserted_data->message_byte_size = message_byte_size; | |
316 inserted_data->event = event; | |
317 inserted_data->details = details; | |
318 } | |
319 | |
320 void GCMStatsRecorder::RecordDataMessageRecieved( | |
321 const std::string& app_id, | |
322 const std::string& from, | |
323 int message_byte_size, | |
324 bool to_registered_app, | |
325 bool is_message_dropped) { | |
326 if (to_registered_app) | |
327 UMA_HISTOGRAM_COUNTS("GCM.DataMessageReceived", 1); | |
328 if (is_recording_) { | |
329 if (!to_registered_app) { | |
330 RecordReceiving(app_id, from, message_byte_size, "Data msg received", | |
331 to_registered_app ? std::string() : | |
332 "No such registered app found"); | |
333 } else if (is_message_dropped) { | |
334 RecordReceiving(app_id, from, message_byte_size, "Data msg received", | |
335 "Message has been deleted on server"); | |
336 } else { | |
337 RecordReceiving(app_id, from, message_byte_size, "Data msg received", | |
338 std::string()); | |
339 } | |
340 } | |
341 } | |
342 | |
343 void GCMStatsRecorder::CollectActivities( | |
344 std::vector<ConnectionActivity>* connection_activities, | |
345 std::vector<RegistrationActivity>* registration_activities, | |
346 std::vector<ReceivingActivity>* receiving_activities, | |
347 std::vector<SendingActivity>* sending_activities) const { | |
348 connection_activities->insert(connection_activities->begin(), | |
349 connection_activities_.begin(), | |
350 connection_activities_.end()); | |
351 registration_activities->insert(registration_activities->begin(), | |
352 registration_activities_.begin(), | |
353 registration_activities_.end()); | |
354 receiving_activities->insert(receiving_activities->begin(), | |
355 receiving_activities_.begin(), | |
356 receiving_activities_.end()); | |
357 sending_activities->insert(sending_activities->begin(), | |
358 sending_activities_.begin(), | |
359 sending_activities_.end()); | |
91 } | 360 } |
92 | 361 |
93 void GCMStatsRecorder::RecordSending(const std::string& app_id, | 362 void GCMStatsRecorder::RecordSending(const std::string& app_id, |
94 const std::string& receiver_id, | 363 const std::string& receiver_id, |
95 const std::string& message_id, | 364 const std::string& message_id, |
96 const std::string& event, | 365 const std::string& event, |
97 const std::string& details) { | 366 const std::string& details) { |
98 SendingActivity data; | 367 SendingActivity data; |
99 SendingActivity* inserted_data = InsertCircularBuffer( | 368 SendingActivity* inserted_data = InsertCircularBuffer( |
100 &sending_activities_, data); | 369 &sending_activities_, data); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 const std::string& receiver_id, | 410 const std::string& receiver_id, |
142 const std::string& message_id) { | 411 const std::string& message_id) { |
143 UMA_HISTOGRAM_COUNTS("GCM.IncomingSendErrors", 1); | 412 UMA_HISTOGRAM_COUNTS("GCM.IncomingSendErrors", 1); |
144 if (is_recording_) { | 413 if (is_recording_) { |
145 RecordSending(app_id, receiver_id, message_id, "Received 'send error' msg", | 414 RecordSending(app_id, receiver_id, message_id, "Received 'send error' msg", |
146 std::string()); | 415 std::string()); |
147 } | 416 } |
148 } | 417 } |
149 | 418 |
150 } // namespace gcm | 419 } // namespace gcm |
OLD | NEW |