Class: Gitlab::Metrics::Samplers::BaseSampler

Inherits:
Daemon
  • Object
show all
Defined in:
lib/gitlab/metrics/samplers/base_sampler.rb

Instance Attribute Summary collapse

Attributes inherited from Daemon

#thread

Instance Method Summary collapse

Methods inherited from Daemon

#enabled?, initialize_instance, instance, #start, #stop, #thread?, #thread_name

Constructor Details

#initialize(interval = self.class::SAMPLING_INTERVAL_SECONDS) ⇒ BaseSampler

interval - The sampling interval in seconds.


12
13
14
15
16
17
18
19
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 12

def initialize(interval = self.class::SAMPLING_INTERVAL_SECONDS)
  interval_half = interval.to_f / 2

  @interval = interval
  @interval_steps = (-interval_half..interval_half).step(0.1).to_a

  super()
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval


9
10
11
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 9

def interval
  @interval
end

Instance Method Details

#safe_sampleObject


21
22
23
24
25
26
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 21

def safe_sample
  sample
rescue => e
  Gitlab::AppLogger.warn("#{self.class}: #{e}, stopping")
  stop
end

#sampleObject

Raises:

  • (NotImplementedError)

28
29
30
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 28

def sample
  raise NotImplementedError
end

#sleep_intervalObject

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.


39
40
41
42
43
44
45
46
47
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 39

def sleep_interval
  while step = @interval_steps.sample
    if step != @last_step
      @last_step = step

      return @interval + @last_step
    end
  end
end