Class: Gitlab::Metrics::Samplers::BaseSampler
- Defined in:
- lib/gitlab/metrics/samplers/base_sampler.rb
Direct Known Subclasses
ActionCableSampler, DatabaseSampler, PumaSampler, RubySampler, ThreadsSampler
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
Attributes inherited from Daemon
Instance Method Summary collapse
-
#initialize(interval: nil, logger: Logger.new($stdout), warmup: false, **options) ⇒ 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: nil, logger: Logger.new($stdout), warmup: false, **options) ⇒ BaseSampler
interval - The sampling interval in seconds. warmup - When true, takes a single sample eagerly before entering the sampling loop.
This can be useful to ensure that all metrics files exist after `start` returns,
since prometheus-client-mmap creates them lazily upon first access.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 15 def initialize(interval: nil, logger: Logger.new($stdout), warmup: false, **) interval ||= ENV[interval_env_key]&.to_i interval ||= self.class::DEFAULT_SAMPLING_INTERVAL_SECONDS interval_half = interval.to_f / 2 @interval = interval @interval_steps = (-interval_half..interval_half).step(0.1).to_a @logger = logger @warmup = warmup 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
29 30 31 32 33 34 |
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 29 def safe_sample sample rescue StandardError => e @logger.warn("#{self.class}: #{e}, stopping") stop end |
#sample ⇒ Object
36 37 38 |
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 36 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.
47 48 49 50 51 52 53 54 55 |
# File 'lib/gitlab/metrics/samplers/base_sampler.rb', line 47 def sleep_interval while step = @interval_steps.sample next if step == @last_step @last_step = step return @interval + @last_step end end |