Class: Bigcommerce::Prometheus::Server
- Inherits:
-
Object
- Object
- Bigcommerce::Prometheus::Server
- Defined in:
- lib/bigcommerce/prometheus/server.rb
Overview
Server implementation for Prometheus
Instance Method Summary collapse
-
#add_type_collector(collector) ⇒ Object
Add a type collector to this server.
-
#initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil, thread_pool_size: nil) ⇒ Server
constructor
A new instance of Server.
-
#running? ⇒ Boolean
Whether or not the server is running.
-
#start ⇒ Object
Start the server.
-
#start_until_stopped ⇒ Object
Start the server and run it until stopped.
-
#stop ⇒ Object
Stop the server.
Constructor Details
#initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil, thread_pool_size: nil) ⇒ Server
Returns a new instance of Server.
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
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
97 98 99 |
# File 'lib/bigcommerce/prometheus/server.rb', line 97 def running? @running end |
#start ⇒ Object
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.}" stop end |
#start_until_stopped ⇒ Object
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.}" stop end |
#stop ⇒ Object
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.}" end |