lib/napa/stats_d_timer.rb in napa-0.4.0 vs lib/napa/stats_d_timer.rb in napa-0.4.1

- old
+ new

@@ -1,16 +1,20 @@ module Napa module StatsDTimer - def report_time(timer_name) - start_time = Time.now - yield - response_time = (Time.now - start_time) * 1000 # statsd reports timers in milliseconds - Napa::Stats.emitter.timing(timer_name, response_time) + def report_time(timer_name, sample_rate: 1) + if (Time.now.to_f * 1000).to_i % sample_rate == 0 + start_time = Time.now + yield + response_time = (Time.now - start_time) * 1000 # statsd reports timers in milliseconds + Napa::Stats.emitter.timing(timer_name, response_time) + else + yield + end end module ClassMethods - def report_time(timer_name) - new.report_time(timer_name) do + def report_time(timer_name, sample_rate: 1) + new.report_time(timer_name, sample_rate: sample_rate) do yield end end end