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 "components/rappor/rappor_service.h" | 5 #include "components/rappor/rappor_service.h" |
6 | 6 |
7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "components/metrics/metrics_hashes.h" | 10 #include "components/metrics/metrics_hashes.h" |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 bool success = reports.SerializeToString(&log_text); | 177 bool success = reports.SerializeToString(&log_text); |
178 DCHECK(success); | 178 DCHECK(success); |
179 DVLOG(1) << "RapporService sending a report of " | 179 DVLOG(1) << "RapporService sending a report of " |
180 << reports.report_size() << " value(s)."; | 180 << reports.report_size() << " value(s)."; |
181 uploader_->QueueLog(log_text); | 181 uploader_->QueueLog(log_text); |
182 } | 182 } |
183 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds)); | 183 ScheduleNextLogRotation(base::TimeDelta::FromSeconds(kLogIntervalSeconds)); |
184 } | 184 } |
185 | 185 |
186 bool RapporService::ExportMetrics(RapporReports* reports) { | 186 bool RapporService::ExportMetrics(RapporReports* reports) { |
187 if (metrics_map_.empty()) { | |
188 DVLOG(2) << "metrics_map_ is empty."; | |
189 return false; | |
190 } | |
191 | |
192 DCHECK_GE(cohort_, 0); | 187 DCHECK_GE(cohort_, 0); |
193 reports->set_cohort(cohort_); | 188 reports->set_cohort(cohort_); |
194 | 189 |
195 for (std::map<std::string, RapporMetric*>::const_iterator it = | 190 for (const auto& kv : metrics_map_) { |
196 metrics_map_.begin(); | 191 const RapporMetric* metric = kv.second; |
197 it != metrics_map_.end(); | |
198 ++it) { | |
199 const RapporMetric* metric = it->second; | |
200 RapporReports::Report* report = reports->add_report(); | 192 RapporReports::Report* report = reports->add_report(); |
201 report->set_name_hash(metrics::HashMetricName(it->first)); | 193 report->set_name_hash(metrics::HashMetricName(kv.first)); |
202 ByteVector bytes = metric->GetReport(secret_); | 194 ByteVector bytes = metric->GetReport(secret_); |
203 report->set_bits(std::string(bytes.begin(), bytes.end())); | 195 report->set_bits(std::string(bytes.begin(), bytes.end())); |
204 } | 196 } |
205 STLDeleteValues(&metrics_map_); | 197 STLDeleteValues(&metrics_map_); |
206 return true; | 198 |
199 sampler_.ExportMetrics(secret_, reports); | |
200 | |
201 DVLOG(2) << "Generated a report with " << reports->report_size() | |
202 << "metrics."; | |
203 return reports->report_size() > 0; | |
207 } | 204 } |
208 | 205 |
209 bool RapporService::IsInitialized() const { | 206 bool RapporService::IsInitialized() const { |
210 return cohort_ >= 0; | 207 return cohort_ >= 0; |
211 } | 208 } |
212 | 209 |
213 void RapporService::RecordSample(const std::string& metric_name, | 210 void RapporService::RecordSample(const std::string& metric_name, |
214 RapporType type, | 211 RapporType type, |
215 const std::string& sample) { | 212 const std::string& sample) { |
216 // Ignore the sample if the service hasn't started yet. | 213 // Ignore the sample if the service hasn't started yet. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 RapporMetric* metric = it->second; | 249 RapporMetric* metric = it->second; |
253 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); | 250 DCHECK_EQ(parameters.ToString(), metric->parameters().ToString()); |
254 return metric; | 251 return metric; |
255 } | 252 } |
256 | 253 |
257 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); | 254 RapporMetric* new_metric = new RapporMetric(metric_name, parameters, cohort_); |
258 metrics_map_[metric_name] = new_metric; | 255 metrics_map_[metric_name] = new_metric; |
259 return new_metric; | 256 return new_metric; |
260 } | 257 } |
261 | 258 |
259 scoped_ptr<Sample> RapporService::MakeSampleObj(RapporType type) { | |
260 DCHECK(IsInitialized()); | |
261 return scoped_ptr<Sample>( | |
262 new Sample(cohort_, kRapporParametersForType[type])); | |
263 } | |
264 | |
265 void RapporService::RecordSampleObj(const std::string& metric_name, | |
266 scoped_ptr<Sample> sample) { | |
267 if (is_incognito_callback_.Run()) { | |
268 DVLOG(2) << "Metric not logged due to incognito mode."; | |
269 return; | |
270 } | |
271 // Skip this metric if it's reporting level is less than the enabled | |
Alexei Svitkine (slow)
2015/04/23 21:42:34
Nit: it's -> its
Steven Holte
2015/04/24 16:59:05
Done.
| |
272 // reporting level. | |
273 if (recording_level_ < sample->parameters().recording_level) { | |
274 DVLOG(2) << "Metric not logged due to recording_level " | |
275 << recording_level_ << " < " | |
276 << sample->parameters().recording_level; | |
277 return; | |
278 } | |
279 sampler_.AddSample(metric_name, sample.Pass()); | |
280 } | |
281 | |
262 } // namespace rappor | 282 } // namespace rappor |
OLD | NEW |