class Bigcommerce::Prometheus::Client

Client implementation for Prometheus

Public Class Methods

new(host: nil, port: nil, max_queue_size: nil, thread_sleep: nil, custom_labels: nil, process_name: nil) click to toggle source

@param [String] host @param [Integer] port @param [Integer] max_queue_size @param [Integer|Float] thread_sleep @param [Hash] custom_labels @param [String] process_name

Calls superclass method
# 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

Public Instance Methods

close_socket_if_old!() click to toggle source

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

# 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_queue() click to toggle source

Process the current queue and flush to the collector

# 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) click to toggle source

@param [String] path @return [Module<URI>]

# File lib/bigcommerce/prometheus/client.rb, line 68
def uri_path(path)
  URI("http://#{@host}:#{@port}#{path}")
end
worker_loop() click to toggle source

Patch the worker loop to make it more resilient

# 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