Class: Gitlab::Metrics::Samplers::BaseSampler
- Defined in:
- lib/gitlab/metrics/samplers/base_sampler.rb
Direct Known Subclasses
ActionCableSampler, DatabaseSampler, PumaSampler, RubySampler, ThreadsSampler, UnicornSampler
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
Attributes inherited from Daemon
Instance Method Summary collapse
-
#initialize(interval = self.class::SAMPLING_INTERVAL_SECONDS) ⇒ BaseSampler
constructor
interval - The sampling interval in seconds.
- #safe_sample ⇒ Object
- #sample ⇒ Object
-
#sleep_interval ⇒ Object
Returns the sleep interval with a random adjustment.
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
#interval ⇒ Object (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_sample ⇒ Object
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 |
#sample ⇒ Object
28 29 30 |
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 28 def sample raise NotImplementedError end |
#sleep_interval ⇒ Object
Returns the sleep interval with a random adjustment.
The random adjustment is put in place to ensure we:
-
Don't generate samples at the exact same interval every time (thus potentially missing anything that happens in between samples).
-
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 |