diff --git a/r2/example.ini b/r2/example.ini index 177f533a4..e8e5e2e51 100644 --- a/r2/example.ini +++ b/r2/example.ini @@ -202,6 +202,8 @@ error_reporters = statsd_addr = # how often to send them [0.0 - 1.0] statsd_sample_rate = 1.0 +# percentage of stats for sampling (0-100) +stats_sample_rate = 1 ############################################ MEDIA STORAGE diff --git a/r2/r2/lib/template_helpers.py b/r2/r2/lib/template_helpers.py index d49da36b6..0d401f81b 100644 --- a/r2/r2/lib/template_helpers.py +++ b/r2/r2/lib/template_helpers.py @@ -151,6 +151,7 @@ def js_config(extra_config=None): # where do ajax requests go? "ajax_domain": get_domain(cname=c.authorized_cname, subreddit=False), "stats_domain": g.stats_domain or '', + "stats_sample_rate": g.stats_sample_rate or 0, "extension": c.extension, "https_endpoint": is_subdomain(request.host, g.domain) and g.https_endpoint, # does the client only want to communicate over HTTPS? diff --git a/r2/r2/public/static/js/ui.js b/r2/r2/public/static/js/ui.js index 63c0dc443..3347a38cf 100644 --- a/r2/r2/public/static/js/ui.js +++ b/r2/r2/public/static/js/ui.js @@ -86,11 +86,15 @@ r.ui.initLiveTimestamps = function() { } r.ui.initTimings = function() { - // sample at a rate of 1% - if (Math.random() > 0.01) { return } + // return if we're not configured for sending stats + if (!r.config.pageInfo.actionName || !r.config.stats_domain) { + return + } - if (!r.config.pageInfo.actionName) { return } - if (!r.config.stats_domain) { return } + // Sample based on the configuration sample rate + if (Math.random() > r.config.stats_sample_rate / 100) { + return + } var browserTimings = new r.NavigationTimings()