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

Side by Side Diff: components/rappor/log_uploader.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 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
(Empty)
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.
4
5 #include "components/rappor/log_uploader.h"
6
7 #include "base/metrics/histogram.h"
8 #include "base/metrics/sparse_histogram.h"
9 #include "net/base/load_flags.h"
10 #include "net/url_request/url_fetcher.h"
11
12 namespace {
13
14 // The delay, in seconds, between uploading when there are queued logs to send.
15 const int kUnsentLogsIntervalSeconds = 3;
16
17 // When uploading metrics to the server fails, we progressively wait longer and
18 // longer before sending the next log. This backoff process helps reduce load
19 // on a server that is having issues.
20 // The following is the multiplier we use to expand that inter-log duration.
21 const double kBackoffMultiplier = 1.1;
22
23 // The maximum backoff multiplier.
24 const int kMaxBackoffIntervalSeconds = 60 * 60;
25
26 // The maximum number of unsent logs we will keep.
27 // TODO(holte): Limit based on log size instead.
28 const size_t kMaxQueuedLogs = 10;
29
30 enum DiscardReason {
31 UPLOAD_SUCCESS,
32 UPLOAD_REJECTED,
33 QUEUE_OVERFLOW,
34 NUM_DISCARD_REASONS
35 };
36
37 } // namespace
38
39 namespace rappor {
40
41 LogUploader::LogUploader(const GURL& server_url,
42 const std::string& mime_type,
43 net::URLRequestContextGetter* request_context)
44 : server_url_(server_url),
45 mime_type_(mime_type),
46 request_context_(request_context),
47 has_callback_pending_(false),
48 upload_interval_(base::TimeDelta::FromSeconds(
49 kUnsentLogsIntervalSeconds)) {
50 }
51
52 LogUploader::~LogUploader() {}
53
54 void LogUploader::QueueLog(const std::string& log) {
55 queued_logs_.push(log);
56 if (!IsUploadScheduled() && !has_callback_pending_)
57 StartScheduledUpload();
58 }
59
60 bool LogUploader::IsUploadScheduled() const {
61 return upload_timer_.IsRunning();
62 }
63
64 void LogUploader::ScheduleNextUpload(base::TimeDelta interval) {
65 if (IsUploadScheduled() || has_callback_pending_)
66 return;
67
68 upload_timer_.Start(
69 FROM_HERE, interval, this, &LogUploader::StartScheduledUpload);
70 }
71
72 void LogUploader::StartScheduledUpload() {
73 DCHECK(!has_callback_pending_);
74 has_callback_pending_ = true;
75 current_fetch_.reset(
76 net::URLFetcher::Create(server_url_, net::URLFetcher::POST, this));
77 current_fetch_->SetRequestContext(request_context_.get());
78 current_fetch_->SetUploadData(mime_type_, queued_logs_.front());
79
80 // We already drop cookies server-side, but we might as well strip them out
81 // client-side as well.
82 current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
83 net::LOAD_DO_NOT_SEND_COOKIES);
84 current_fetch_->Start();
85 }
86
87 void LogUploader::OnURLFetchComplete(const net::URLFetcher* source) {
88 // We're not allowed to re-use the existing |URLFetcher|s, so free them here.
89 // Note however that |source| is aliased to the fetcher, so we should be
90 // careful not to delete it too early.
91 DCHECK_EQ(current_fetch_.get(), source);
92 scoped_ptr<net::URLFetcher> fetch(current_fetch_.Pass());
93
94 int response_code = source->GetResponseCode();
95
96 // Log a histogram to track response success vs. failure rates.
97 UMA_HISTOGRAM_SPARSE_SLOWLY("Rappor.UploadResponseCode", response_code);
98
99 bool upload_succeeded = response_code == 200;
100
101 // Determine whether this log should be retransmitted.
102 DiscardReason reason = NUM_DISCARD_REASONS;
Ilya Sherman 2014/02/13 01:39:03 IMPORTANT: You should never try to log a value of
Steven Holte 2014/02/13 05:11:12 We never do log NUM_DISCARD_REASONS, as that's use
Ilya Sherman 2014/02/13 23:23:08 Ah, I see, I skimmed too quickly over the conditio
103 if (upload_succeeded) {
104 reason = UPLOAD_SUCCESS;
105 } else if (response_code == 400) {
106 reason = UPLOAD_REJECTED;
107 } else if (queued_logs_.size() > kMaxQueuedLogs) {
108 reason = QUEUE_OVERFLOW;
109 }
110
111 if (reason != NUM_DISCARD_REASONS) {
112 UMA_HISTOGRAM_ENUMERATION("Rappor.DiscardReason",
113 reason,
114 NUM_DISCARD_REASONS);
115 queued_logs_.pop();
116 }
117
118 // Error 400 indicates a problem with the log, not with the server, so
119 // don't consider that a sign that the server is in trouble.
Ilya Sherman 2014/02/13 01:39:03 It looks like this comment was copied from the UMA
Steven Holte 2014/02/13 05:11:12 It will be, though the server doesn't exist yet.
120 bool server_is_healthy = upload_succeeded || response_code == 400;
121 OnUploadFinished(server_is_healthy, !queued_logs_.empty());
122 }
123
124 void LogUploader::OnUploadFinished(bool server_is_healthy,
125 bool more_logs_remaining) {
Ilya Sherman 2014/02/13 01:39:03 nit: Indentation is slightly off.
Steven Holte 2014/02/13 05:11:12 Done.
126 DCHECK(has_callback_pending_);
127 has_callback_pending_ = false;
128 // If the server is having issues, back off. Otherwise, reset to default.
129 if (!server_is_healthy)
130 upload_interval_ = BackOffUploadInterval(upload_interval_);
131 else
132 upload_interval_ = base::TimeDelta::FromSeconds(kUnsentLogsIntervalSeconds);
133
134 if (more_logs_remaining)
135 ScheduleNextUpload(upload_interval_);
136 }
137
138 // static
139 base::TimeDelta LogUploader::BackOffUploadInterval(base::TimeDelta interval) {
140 DCHECK_GT(kBackoffMultiplier, 1.0);
141 interval = base::TimeDelta::FromMicroseconds(static_cast<int64>(
142 kBackoffMultiplier * interval.InMicroseconds()));
143
144 base::TimeDelta max_interval =
145 base::TimeDelta::FromSeconds(kMaxBackoffIntervalSeconds);
146 return interval > max_interval ? max_interval : interval;
147 }
Ilya Sherman 2014/02/13 01:39:03 nit: Please organize the .cc file so that the orde
Steven Holte 2014/02/13 05:11:12 Done.
148
149 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698