class Bigcommerce::Prometheus::Collectors::Base

Base class for collectors

Public Class Methods

new(client: nil, type: nil, frequency: nil, options: nil) click to toggle source

@param [Bigcommerce::Prometheus::Client] client @param [String] type @param [Integer] frequency @param [Hash] options

# File lib/bigcommerce/prometheus/collectors/base.rb, line 57
def initialize(client: nil, type: nil, frequency: nil, options: nil)
  @client = client || Bigcommerce::Prometheus.client
  @type = type || self.class.to_s.downcase.gsub('::', '_').gsub('collector', '')
  @frequency = frequency || Bigcommerce::Prometheus.collector_collection_frequency
  @options = options || {}
  @logger = Bigcommerce::Prometheus.logger
end
start(args, &block) click to toggle source

Start the collector

# File lib/bigcommerce/prometheus/collectors/base.rb, line 28
def self.start(args, &block)
  process_collector = new(**args, &block)

  stop if @thread

  @thread = Thread.new do
    Kernel.loop do
      process_collector.run
    end
  end
end
stop() click to toggle source

Stop the collector

# File lib/bigcommerce/prometheus/collectors/base.rb, line 43
def self.stop
  t = @thread
  return unless t

  t.kill
  @thread = nil
end

Public Instance Methods

collect(metrics = {}) click to toggle source

Collect metrics. This should be overridden in derivative collectors

@param [Hash] metrics @return [Hash]

# File lib/bigcommerce/prometheus/collectors/base.rb, line 82
def collect(metrics = {})
  metrics
end
run() click to toggle source

Run the collector and send stats

# File lib/bigcommerce/prometheus/collectors/base.rb, line 68
def run
  metrics = {}
  metrics = collect(metrics)
  push(metrics)
ensure
  sleep @frequency
end

Private Instance Methods

push(metric) click to toggle source

@param [Hash] metric

# File lib/bigcommerce/prometheus/collectors/base.rb, line 91
def push(metric)
  metric[:type] = @type unless metric.key?(:type)
  @logger.debug("[bigcommerce-prometheus] Pushing #{metric[:type]} metrics to type collector: #{metric.inspect}")
  @client.send_json(metric)
rescue StandardError => e
  @logger.error("[bigcommerce-prometheus] Prometheus Exporter failed to send #{metric[:type]} stats to type collector #{e}")
end