Class: Bigcommerce::Prometheus::Server

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

Overview

Server implementation for Prometheus

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil, thread_pool_size: nil) ⇒ Server

Returns a new instance of Server.

Parameters:

  • host (String) (defaults to: nil)
  • port (Integer) (defaults to: nil)
  • timeout (Integer) (defaults to: nil)
  • prefix (String) (defaults to: nil)
  • thread_pool_size (Integer) (defaults to: nil)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bigcommerce/prometheus/server.rb', line 31

def initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil, thread_pool_size: nil)
  @host = host || ::Bigcommerce::Prometheus.server_host
  @port = (port || ::Bigcommerce::Prometheus.server_port).to_i
  @timeout = (timeout || ::Bigcommerce::Prometheus.server_timeout).to_i
  @prefix = (prefix || ::PrometheusExporter::DEFAULT_PREFIX).to_s
  @process_name = ::Bigcommerce::Prometheus.process_name
  @logger = logger || ::Bigcommerce::Prometheus.logger
  @server = ::Bigcommerce::Prometheus::Servers::Thin::Server.new(
    port: @port,
    timeout: @timeout,
    logger: @logger,
    thread_pool_size: (thread_pool_size || ::Bigcommerce::Prometheus.server_thread_pool_size).to_i
  )
  @running = false
  ::PrometheusExporter::Metric::Base.default_prefix = @prefix
  setup_signal_handlers
end

Instance Method Details

#add_type_collector(collector) ⇒ Object

Add a type collector to this server

Parameters:

  • collector (PrometheusExporter::Server::TypeCollector)


106
107
108
109
# File 'lib/bigcommerce/prometheus/server.rb', line 106

def add_type_collector(collector)
  @logger.info "[bigcommerce-prometheus][#{@process_name}] Registering collector #{collector&.type}"
  @server.add_type_collector(collector)
end

#running?Boolean

Whether or not the server is running

Returns:

  • (Boolean)


97
98
99
# File 'lib/bigcommerce/prometheus/server.rb', line 97

def running?
  @running
end

#startObject

Start the server



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bigcommerce/prometheus/server.rb', line 52

def start
  @logger.info "[bigcommerce-prometheus][#{@process_name}] Starting prometheus exporter on port #{@host}:#{@port}"

  @run_thread = ::Thread.start do
    @server.start
  end
  @running = true

  @logger.info "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter started on #{@host}:#{@port} with #{@server.threadpool_size} threads"

  @server
rescue ::StandardError => e
  @logger.error "[bigcommerce-prometheus][#{@process_name}] Failed to start exporter: #{e.message}"
  stop
end

#start_until_stoppedObject

Start the server and run it until stopped



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

def start_until_stopped
  start
  yield
  @run_thread.join
rescue StandardError => e
  warn "[bigcommerce-prometheus] Server crashed: #{e.message}"
  stop
end

#stopObject

Stop the server



83
84
85
86
87
88
89
90
# File 'lib/bigcommerce/prometheus/server.rb', line 83

def stop
  @server.stop!
  @run_thread.kill
  @running = false
  $stdout.puts "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter cleanly shut down"
rescue ::StandardError => e
  warn "[bigcommerce-prometheus][#{@process_name}] Failed to stop exporter: #{e.message}"
end