2017-07-04 11:28:34 -04:00
|
|
|
require 'logger'
|
|
|
|
module Gitlab
|
|
|
|
module Metrics
|
2017-08-07 13:13:02 -04:00
|
|
|
class BaseSampler < Daemon
|
2017-07-04 11:28:34 -04:00
|
|
|
# interval - The sampling interval in seconds.
|
|
|
|
def initialize(interval)
|
|
|
|
interval_half = interval.to_f / 2
|
|
|
|
|
|
|
|
@interval = interval
|
|
|
|
@interval_steps = (-interval_half..interval_half).step(0.1).to_a
|
|
|
|
|
2017-08-07 13:13:02 -04:00
|
|
|
super()
|
2017-07-04 11:28:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def safe_sample
|
|
|
|
sample
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn("#{self.class}: #{e}, stopping")
|
|
|
|
stop
|
|
|
|
end
|
|
|
|
|
|
|
|
def sample
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the sleep interval with a random adjustment.
|
|
|
|
#
|
|
|
|
# The random adjustment is put in place to ensure we:
|
|
|
|
#
|
|
|
|
# 1. Don't generate samples at the exact same interval every time (thus
|
|
|
|
# potentially missing anything that happens in between samples).
|
|
|
|
# 2. Don't sample data at the same interval two times in a row.
|
|
|
|
def sleep_interval
|
2017-08-07 13:13:02 -04:00
|
|
|
while (step = @interval_steps.sample)
|
2017-07-04 11:28:34 -04:00
|
|
|
if step != @last_step
|
|
|
|
@last_step = step
|
|
|
|
|
|
|
|
return @interval + @last_step
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-07 13:13:02 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :running
|
|
|
|
|
|
|
|
def start_working
|
|
|
|
@running = true
|
|
|
|
sleep(sleep_interval)
|
|
|
|
|
|
|
|
while running
|
|
|
|
safe_sample
|
|
|
|
|
|
|
|
sleep(sleep_interval)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_working
|
|
|
|
@running = false
|
|
|
|
end
|
2017-07-04 11:28:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|