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

Unified Diff: chrome/browser/metrics/chrome_metrics_services_manager_client.cc

Issue 2180373003: Adding reporting of metrics sampling rate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding reporting of sampling rate. Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/metrics/chrome_metrics_services_manager_client.cc
diff --git a/chrome/browser/metrics/chrome_metrics_services_manager_client.cc b/chrome/browser/metrics/chrome_metrics_services_manager_client.cc
index a31d9af6a10d4291b9d515ff2d0631a7f3f1fe11..61dc5dc149381f24714d61314b99f68d7c228a3d 100644
--- a/chrome/browser/metrics/chrome_metrics_services_manager_client.cc
+++ b/chrome/browser/metrics/chrome_metrics_services_manager_client.cc
@@ -8,6 +8,7 @@
#include "base/feature_list.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
+#include "base/strings/string_number_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/metrics/chrome_metrics_service_client.h"
@@ -21,10 +22,20 @@
#include "components/prefs/pref_service.h"
#include "components/rappor/rappor_service.h"
#include "components/variations/service/variations_service.h"
+#include "components/variations/variations_associated_data.h"
#include "content/public/browser/browser_thread.h"
namespace {
+// Name of the variations param that defines the sampling rate.
+const char kRateParamName[] = "sampling_rate_per_mille";
+
+// Metrics reporting feature. This feature, along with user consent, controls if
+// recording and reporting are enabled. If the feature is enabled, but no
+// consent is given, then there will be no recording or reporting.
+const base::Feature kMetricsReportingFeature{"MetricsReporting",
+ base::FEATURE_ENABLED_BY_DEFAULT};
+
// Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread
// because it needs access to IO and cannot work from UI thread.
void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) {
@@ -33,10 +44,16 @@ void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) {
base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info));
}
+// Only clients that were given an opt-out metrics-reporting consent flow are
+// eligible for sampling.
+bool IsClientEligibleForSampling() {
+ return metrics::GetMetricsReportingDefaultState(
+ g_browser_process->local_state()) ==
+ metrics::EnableMetricsDefault::OPT_OUT;
+}
+
} // namespace
-const base::Feature kMetricsReportingFeature{"MetricsReporting",
- base::FEATURE_ENABLED_BY_DEFAULT};
class ChromeMetricsServicesManagerClient::ChromeEnabledStateProvider
: public metrics::EnabledStateProvider {
@@ -50,7 +67,7 @@ class ChromeMetricsServicesManagerClient::ChromeEnabledStateProvider
bool IsReportingEnabled() override {
return IsConsentGiven() &&
- base::FeatureList::IsEnabled(kMetricsReportingFeature);
+ ChromeMetricsServicesManagerClient::IsClientInSample();
}
DISALLOW_COPY_AND_ASSIGN(ChromeEnabledStateProvider);
@@ -67,6 +84,45 @@ ChromeMetricsServicesManagerClient::ChromeMetricsServicesManagerClient(
ChromeMetricsServicesManagerClient::~ChromeMetricsServicesManagerClient() {}
+// static
+bool ChromeMetricsServicesManagerClient::IsClientInSample() {
+ // Only some clients are eligible for sampling. Clients that aren't eligible
+ // will always be considered "in sample". In this case, we don't want the
+ // feature state queried, because we don't want the field trial that controls
+ // sampling to be reported as active.
+ if (!IsClientEligibleForSampling()) {
Alexei Svitkine (slow) 2016/08/01 15:49:56 Nit: No {}'s Same below for 1-liners.
jwd 2016/08/01 16:45:29 Done.
+ return true;
+ }
+
+ return base::FeatureList::IsEnabled(kMetricsReportingFeature);
+}
+
+// static
+int ChromeMetricsServicesManagerClient::GetSamplingRatePerMille() {
+ // The population that is NOT eligible for sampling in considered "in sample",
+ // so that population's sampling rate is 1000/1000.
+ if (!IsClientEligibleForSampling()) {
+ return 1000;
+ }
+
+ std::string rate_str = variations::GetVariationParamValueByFeature(
+ kMetricsReportingFeature, kRateParamName);
+ if (rate_str.empty()) {
+ // The parameter can be empty either if the feature is dissabled, or the
Alexei Svitkine (slow) 2016/08/01 15:49:56 disabled - also mispeled below
jwd 2016/08/01 16:45:29 Done.
+ // parameter isn't defined in an associated variation. The dissabled case is
+ // unimportant, since no metrics are reported. If the parameter is
+ // undefined, assume sampling is not being done, i.e. the sample rate is
+ // 1000/1000.
+ return 1000;
+ }
+
+ int rate;
+ if (!base::StringToInt(rate_str, &rate) || rate > 1000) {
+ return 1000;
+ }
+ return rate;
+}
+
std::unique_ptr<rappor::RapporService>
ChromeMetricsServicesManagerClient::CreateRapporService() {
DCHECK(thread_checker_.CalledOnValidThread());

Powered by Google App Engine
This is Rietveld 408576698