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

#initializeClient

Returns a new instance of Client.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bigcommerce/prometheus/client.rb', line 27

def initialize
  super(
    host: Bigcommerce::Prometheus.server_host,
    port: Bigcommerce::Prometheus.server_port,
    max_queue_size: Bigcommerce::Prometheus.client_max_queue_size,
    thread_sleep: Bigcommerce::Prometheus.client_thread_sleep,
    custom_labels: Bigcommerce::Prometheus.client_custom_labels
  )
  PrometheusExporter::Client.default = self
  @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



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

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



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

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) ⇒ URI

Parameters:

  • path (String)

Returns:

  • (URI)


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

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

#worker_loopObject

Patch the worker loop to make it more resilient



42
43
44
45
46
47
# File 'lib/bigcommerce/prometheus/client.rb', line 42

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