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

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

Overview

Base class for collectors

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 (PrometheusExporter::Client) (defaults to: nil)
  • type (String) (defaults to: nil)
  • frequency (Integer) (defaults to: nil)
  • options (Hash) (defaults to: nil)


57
58
59
60
61
62
63
# File 'lib/bigcommerce/prometheus/collectors/base.rb', line 57

def initialize(client: nil, type: nil, frequency: nil, options: nil)
  @client = client || PrometheusExporter::Client.default
  @type = type || self.class.to_s.downcase.gsub('::', '_').gsub('collector', '')
  @frequency = frequency || 15
  @options = options || {}
  @logger = Bigcommerce::Prometheus.logger
end

Class Method Details

.start(*args, &block) ⇒ Object

Start the collector



28
29
30
31
32
33
34
35
36
37
38
# 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

.stopObject

Stop the collector



43
44
45
46
47
48
49
# File 'lib/bigcommerce/prometheus/collectors/base.rb', line 43

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



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

def run
  metrics = { type: @type }
  metrics = collect(metrics)
  @logger.debug "[bigcommerce-prometheus] Pushing #{@type} metrics to type collector: #{metrics.inspect}"
  @client.send_json(metrics)
rescue StandardError => e
  @logger.error("Prometheus Exporter Failed To Collect #{@type} Stats #{e}")
ensure
  sleep @frequency
end