class ElasticAPM::Metrics::Collector

@api private

Constants

TIMEOUT_INTERVAL

Attributes

callback[R]
config[R]
samplers[R]
tags[R]

Public Class Methods

new(config, tags: nil, &block) click to toggle source
# File lib/elastic_apm/metrics.rb, line 24
def initialize(config, tags: nil, &block)
  @config = config
  @tags = tags
  @samplers = [CpuMem].map do |kls|
    debug "Adding metrics collector '#{kls}'"
    kls.new(config)
  end
  @callback = block
end

Public Instance Methods

collect() click to toggle source
# File lib/elastic_apm/metrics.rb, line 85
def collect
  MUTEX.synchronize do
    samplers.each_with_object({}) do |sampler, samples|
      next unless (sample = sampler.collect)
      samples.merge!(sample)
    end
  end
end
collect_and_send() click to toggle source
# File lib/elastic_apm/metrics.rb, line 78
def collect_and_send
  metricset = Metricset.new(tags: tags, **collect)
  return if metricset.empty?

  callback.call(metricset)
end
running?() click to toggle source
# File lib/elastic_apm/metrics.rb, line 74
def running?
  !!@running
end
start() click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/elastic_apm/metrics.rb, line 37
def start
  unless config.collect_metrics?
    debug 'Skipping metrics'
    return
  end

  debug 'Starting metrics'

  @timer_task = Concurrent::TimerTask.execute(
    run_now: true,
    execution_interval: config.metrics_interval,
    timeout_interval: TIMEOUT_INTERVAL
  ) do
    begin
      debug 'Collecting metrics'
      collect_and_send
      true
    rescue StandardError => e
      error 'Error while collecting metrics: %e', e.inspect
      debug { e.backtrace.join("\n") }
      false
    end
  end

  @running = true
end
stop() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/elastic_apm/metrics.rb, line 65
def stop
  return unless running?

  debug 'Stopping metrics'

  @timer_task.shutdown
  @running = false
end