Class: Bigcommerce::Prometheus::Client

Inherits:
PrometheusExporter::Client
  • Object
show all
Includes:
Loggable, Singleton
Defined in:
lib/bigcommerce/prometheus/client.rb

Overview

Client implementation for Prometheus

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initialize(host: nil, port: nil, max_queue_size: nil, thread_sleep: nil, custom_labels: nil, process_name: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • host (String) (defaults to: nil)
  • port (Integer) (defaults to: nil)
  • max_queue_size (Integer) (defaults to: nil)
  • thread_sleep (Integer|Float) (defaults to: nil)
  • custom_labels (Hash) (defaults to: nil)
  • process_name (String) (defaults to: nil)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bigcommerce/prometheus/client.rb', line 35

def initialize(host: nil, port: nil, max_queue_size: nil, thread_sleep: nil, custom_labels: nil, process_name: nil)
  super(
    host: host || Bigcommerce::Prometheus.server_host,
    port: port || Bigcommerce::Prometheus.server_port,
    max_queue_size: max_queue_size || Bigcommerce::Prometheus.client_max_queue_size,
    thread_sleep: thread_sleep || Bigcommerce::Prometheus.client_thread_sleep,
    custom_labels: custom_labels || Bigcommerce::Prometheus.client_custom_labels
  )
  PrometheusExporter::Client.default = self
  @process_name = process_name || ::Bigcommerce::Prometheus.process_name
end

Instance Method Details

#close_socket_if_old!Object

Patch the close socket command to handle when @socket_started is nil



60
61
62
# File 'lib/bigcommerce/prometheus/client.rb', line 60

def close_socket_if_old!
  close_socket! if @socket && ((@socket_started.to_i + MAX_SOCKET_AGE) < Time.now.to_f)
end

#process_queueObject

Process the current queue and flush to the collector



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bigcommerce/prometheus/client.rb', line 75

def process_queue
  while @queue.length.to_i.positive?
    begin
      message = @queue.pop
      Net::HTTP.post(uri_path('/send-metrics'), message)
    rescue StandardError => e
      logger.warn "[bigcommerce-prometheus][#{@process_name}] Prometheus Exporter is dropping a message tp #{uri_path('/send-metrics')}: #{e}"
      raise
    end
  end
end

#uri_path(path) ⇒ Module<URI>

Parameters:

  • path (String)

Returns:

  • (Module<URI>)


68
69
70
# File 'lib/bigcommerce/prometheus/client.rb', line 68

def uri_path(path)
  URI("http://#{@host}:#{@port}#{path}")
end

#worker_loopObject

Patch the worker loop to make it more resilient



50
51
52
53
54
55
# File 'lib/bigcommerce/prometheus/client.rb', line 50

def worker_loop
  close_socket_if_old!
  process_queue
rescue StandardError => e
  logger.warn "[bigcommerce-prometheus][#{@process_name}] Prometheus client failed to send message to #{@host}:#{@port} #{e} - #{e.backtrace[0..5].join("\n")}"
end