Class: Bigcommerce::Prometheus::Collectors::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bigcommerce/prometheus/collectors/base.rb

Overview

Base class for collectors

Direct Known Subclasses

Resque

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: nil, type: nil, frequency: nil, options: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

  • client (Bigcommerce::Prometheus::Client) (defaults to: nil)
  • type (String) (defaults to: nil)
  • frequency (Integer) (defaults to: nil)
  • options (Hash) (defaults to: nil)


60
61
62
63
64
65
66
# File 'lib/bigcommerce/prometheus/collectors/base.rb', line 60

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

Class Method Details

.start(args = {}, &block) ⇒ Object

Start the collector

Parameters:

  • args (Hash) (defaults to: {})


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bigcommerce/prometheus/collectors/base.rb', line 30

def self.start(args = {}, &block)
  args ||= {}
  process_collector = new(**args, &block)

  stop if @thread

  @thread = Thread.new do
    Kernel.loop do
      process_collector.run
    end
  end
end

.stopObject

Stop the collector



46
47
48
49
50
51
52
# File 'lib/bigcommerce/prometheus/collectors/base.rb', line 46

def self.stop
  t = @thread
  return unless t

  t.kill
  @thread = nil
end

Instance Method Details

#collect(metrics = {}) ⇒ Hash

Collect metrics. This should be overridden in derivative collectors

Parameters:

  • metrics (Hash) (defaults to: {})

Returns:

  • (Hash)


85
86
87
# File 'lib/bigcommerce/prometheus/collectors/base.rb', line 85

def collect(metrics = {})
  metrics
end

#runObject

Run the collector and send stats



71
72
73
74
75
76
77
# File 'lib/bigcommerce/prometheus/collectors/base.rb', line 71

def run
  metrics = {}
  metrics = collect(metrics)
  push(metrics)
ensure
  sleep @frequency
end