From efe38ffadbd86157f5a241c1c602de171ff9626e Mon Sep 17 00:00:00 2001 From: Josh Kleinpeter Date: Thu, 21 Nov 2024 08:44:35 -0600 Subject: [PATCH 1/3] PoC migrating from thign to puma --- bc-prometheus-ruby.gemspec | 3 ++- lib/bigcommerce/prometheus.rb | 19 ++++++++++--------- lib/bigcommerce/prometheus/server.rb | 2 +- .../controllers/base_controller.rb | 6 +++--- .../controllers/error_controller.rb | 2 +- .../controllers/metrics_controller.rb | 2 +- .../controllers/not_found_controller.rb | 2 +- .../controllers/send_metrics_controller.rb | 2 +- .../servers/{thin => puma}/rack_app.rb | 14 +++++++------- .../servers/{thin => puma}/server.rb | 14 +++++++------- .../servers/{thin => puma}/server_metrics.rb | 2 +- .../controllers/metrics_controller_spec.rb | 4 ++-- .../send_metrics_controller_spec.rb | 4 ++-- .../servers/{thin => puma}/server_spec.rb | 10 +++++----- 14 files changed, 44 insertions(+), 42 deletions(-) rename lib/bigcommerce/prometheus/servers/{thin => puma}/controllers/base_controller.rb (94%) rename lib/bigcommerce/prometheus/servers/{thin => puma}/controllers/error_controller.rb (98%) rename lib/bigcommerce/prometheus/servers/{thin => puma}/controllers/metrics_controller.rb (99%) rename lib/bigcommerce/prometheus/servers/{thin => puma}/controllers/not_found_controller.rb (98%) rename lib/bigcommerce/prometheus/servers/{thin => puma}/controllers/send_metrics_controller.rb (99%) rename lib/bigcommerce/prometheus/servers/{thin => puma}/rack_app.rb (89%) rename lib/bigcommerce/prometheus/servers/{thin => puma}/server.rb (85%) rename lib/bigcommerce/prometheus/servers/{thin => puma}/server_metrics.rb (99%) rename spec/bigcommerce/prometheus/servers/{thin => puma}/controllers/metrics_controller_spec.rb (95%) rename spec/bigcommerce/prometheus/servers/{thin => puma}/controllers/send_metrics_controller_spec.rb (95%) rename spec/bigcommerce/prometheus/servers/{thin => puma}/server_spec.rb (63%) diff --git a/bc-prometheus-ruby.gemspec b/bc-prometheus-ruby.gemspec index 5ed645d..daff97a 100644 --- a/bc-prometheus-ruby.gemspec +++ b/bc-prometheus-ruby.gemspec @@ -35,6 +35,7 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'bigcommerce-multitrap', '~> 0.1' spec.add_runtime_dependency 'prometheus_exporter', '~> 0.7' + spec.add_runtime_dependency 'puma', '> 5' + spec.add_runtime_dependency 'rack', '>= 3.0' spec.add_runtime_dependency 'rake', '>= 10.0' - spec.add_runtime_dependency 'thin', '~> 1.7' end diff --git a/lib/bigcommerce/prometheus.rb b/lib/bigcommerce/prometheus.rb index 5f9e845..e1819ad 100644 --- a/lib/bigcommerce/prometheus.rb +++ b/lib/bigcommerce/prometheus.rb @@ -22,7 +22,8 @@ require 'prometheus_exporter/client' require 'prometheus_exporter/middleware' require 'prometheus_exporter/instrumentation' -require 'thin' +require 'puma' +require 'rack' require_relative 'prometheus/version' require_relative 'prometheus/loggable' @@ -42,14 +43,14 @@ require_relative 'prometheus/integrations/puma' require_relative 'prometheus/integrations/resque' -require_relative 'prometheus/servers/thin/server' -require_relative 'prometheus/servers/thin/rack_app' -require_relative 'prometheus/servers/thin/server_metrics' -require_relative 'prometheus/servers/thin/controllers/base_controller' -require_relative 'prometheus/servers/thin/controllers/error_controller' -require_relative 'prometheus/servers/thin/controllers/metrics_controller' -require_relative 'prometheus/servers/thin/controllers/not_found_controller' -require_relative 'prometheus/servers/thin/controllers/send_metrics_controller' +require_relative 'prometheus/servers/puma/server' +require_relative 'prometheus/servers/puma/rack_app' +require_relative 'prometheus/servers/puma/server_metrics' +require_relative 'prometheus/servers/puma/controllers/base_controller' +require_relative 'prometheus/servers/puma/controllers/error_controller' +require_relative 'prometheus/servers/puma/controllers/metrics_controller' +require_relative 'prometheus/servers/puma/controllers/not_found_controller' +require_relative 'prometheus/servers/puma/controllers/send_metrics_controller' module Bigcommerce ## diff --git a/lib/bigcommerce/prometheus/server.rb b/lib/bigcommerce/prometheus/server.rb index 0d70e39..ebc75a1 100644 --- a/lib/bigcommerce/prometheus/server.rb +++ b/lib/bigcommerce/prometheus/server.rb @@ -35,7 +35,7 @@ def initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil, thr @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( + @server = ::Bigcommerce::Prometheus::Servers::Puma::Server.new( port: @port, timeout: @timeout, logger: @logger, diff --git a/lib/bigcommerce/prometheus/servers/thin/controllers/base_controller.rb b/lib/bigcommerce/prometheus/servers/puma/controllers/base_controller.rb similarity index 94% rename from lib/bigcommerce/prometheus/servers/thin/controllers/base_controller.rb rename to lib/bigcommerce/prometheus/servers/puma/controllers/base_controller.rb index 94c5fe4..1bf4fa9 100644 --- a/lib/bigcommerce/prometheus/servers/thin/controllers/base_controller.rb +++ b/lib/bigcommerce/prometheus/servers/puma/controllers/base_controller.rb @@ -18,16 +18,16 @@ module Bigcommerce module Prometheus module Servers - module Thin + module Puma module Controllers ## - # Base thin controller for prometheus metrics + # Base puma controller for prometheus metrics # class BaseController ## # @param [Rack::Request] request # @param [Rack::Response] response - # @param [Bigcommerce::Prometheus::Servers::Thin::ServerMetrics] + # @param [Bigcommerce::Prometheus::Servers::Puma::ServerMetrics] # @param [PrometheusExporter::Server::Collector] collector # @param [Logger] logger # diff --git a/lib/bigcommerce/prometheus/servers/thin/controllers/error_controller.rb b/lib/bigcommerce/prometheus/servers/puma/controllers/error_controller.rb similarity index 98% rename from lib/bigcommerce/prometheus/servers/thin/controllers/error_controller.rb rename to lib/bigcommerce/prometheus/servers/puma/controllers/error_controller.rb index 06840b8..e3ffcfc 100644 --- a/lib/bigcommerce/prometheus/servers/thin/controllers/error_controller.rb +++ b/lib/bigcommerce/prometheus/servers/puma/controllers/error_controller.rb @@ -18,7 +18,7 @@ module Bigcommerce module Prometheus module Servers - module Thin + module Puma module Controllers ## # Handle 500s diff --git a/lib/bigcommerce/prometheus/servers/thin/controllers/metrics_controller.rb b/lib/bigcommerce/prometheus/servers/puma/controllers/metrics_controller.rb similarity index 99% rename from lib/bigcommerce/prometheus/servers/thin/controllers/metrics_controller.rb rename to lib/bigcommerce/prometheus/servers/puma/controllers/metrics_controller.rb index e6cbeab..76aca82 100644 --- a/lib/bigcommerce/prometheus/servers/thin/controllers/metrics_controller.rb +++ b/lib/bigcommerce/prometheus/servers/puma/controllers/metrics_controller.rb @@ -22,7 +22,7 @@ module Bigcommerce module Prometheus module Servers - module Thin + module Puma module Controllers ## # GET /metrics diff --git a/lib/bigcommerce/prometheus/servers/thin/controllers/not_found_controller.rb b/lib/bigcommerce/prometheus/servers/puma/controllers/not_found_controller.rb similarity index 98% rename from lib/bigcommerce/prometheus/servers/thin/controllers/not_found_controller.rb rename to lib/bigcommerce/prometheus/servers/puma/controllers/not_found_controller.rb index ae09ba2..f8c70d6 100644 --- a/lib/bigcommerce/prometheus/servers/thin/controllers/not_found_controller.rb +++ b/lib/bigcommerce/prometheus/servers/puma/controllers/not_found_controller.rb @@ -18,7 +18,7 @@ module Bigcommerce module Prometheus module Servers - module Thin + module Puma module Controllers ## # Handle invalid requests to server diff --git a/lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb b/lib/bigcommerce/prometheus/servers/puma/controllers/send_metrics_controller.rb similarity index 99% rename from lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb rename to lib/bigcommerce/prometheus/servers/puma/controllers/send_metrics_controller.rb index 302e051..b2ef824 100644 --- a/lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb +++ b/lib/bigcommerce/prometheus/servers/puma/controllers/send_metrics_controller.rb @@ -18,7 +18,7 @@ module Bigcommerce module Prometheus module Servers - module Thin + module Puma module Controllers ## # POST /send-metrics diff --git a/lib/bigcommerce/prometheus/servers/thin/rack_app.rb b/lib/bigcommerce/prometheus/servers/puma/rack_app.rb similarity index 89% rename from lib/bigcommerce/prometheus/servers/thin/rack_app.rb rename to lib/bigcommerce/prometheus/servers/puma/rack_app.rb index a41434c..198f891 100644 --- a/lib/bigcommerce/prometheus/servers/thin/rack_app.rb +++ b/lib/bigcommerce/prometheus/servers/puma/rack_app.rb @@ -18,9 +18,9 @@ module Bigcommerce module Prometheus module Servers - module Thin + module Puma ## - # Handles metrics requests as a Rack App on the Thin server + # Handles metrics requests as a Rack App on the Puma server # class RackApp ## @@ -29,7 +29,7 @@ def initialize(collector: nil, timeout: nil, logger: nil) @timeout = timeout || ::Bigcommerce::Prometheus.server_timeout @collector = collector || ::PrometheusExporter::Server::Collector.new @logger = logger || ::Bigcommerce::Prometheus.logger - @server_metrics = ::Bigcommerce::Prometheus::Servers::Thin::ServerMetrics.new(logger: @logger) + @server_metrics = ::Bigcommerce::Prometheus::Servers::Puma::ServerMetrics.new(logger: @logger) end def call(env) @@ -39,7 +39,7 @@ def call(env) handle(controller: controller, request: request, response: response) rescue StandardError => e @logger.error "Error: #{e.message}" - handle(controller: ::Bigcommerce::Prometheus::Servers::Thin::Controllers::ErrorController, request: request, response: response) + handle(controller: ::Bigcommerce::Prometheus::Servers::Puma::Controllers::ErrorController, request: request, response: response) end ## @@ -60,11 +60,11 @@ def add_type_collector(collector) # def route(request) if request.fullpath == '/metrics' && request.request_method.to_s.downcase == 'get' - Bigcommerce::Prometheus::Servers::Thin::Controllers::MetricsController + Bigcommerce::Prometheus::Servers::Puma::Controllers::MetricsController elsif request.fullpath == '/send-metrics' && request.request_method.to_s.downcase == 'post' - Bigcommerce::Prometheus::Servers::Thin::Controllers::SendMetricsController + Bigcommerce::Prometheus::Servers::Puma::Controllers::SendMetricsController else - Bigcommerce::Prometheus::Servers::Thin::Controllers::NotFoundController + Bigcommerce::Prometheus::Servers::Puma::Controllers::NotFoundController end end diff --git a/lib/bigcommerce/prometheus/servers/thin/server.rb b/lib/bigcommerce/prometheus/servers/puma/server.rb similarity index 85% rename from lib/bigcommerce/prometheus/servers/thin/server.rb rename to lib/bigcommerce/prometheus/servers/puma/server.rb index 4a3388b..27eecdb 100644 --- a/lib/bigcommerce/prometheus/servers/thin/server.rb +++ b/lib/bigcommerce/prometheus/servers/puma/server.rb @@ -18,21 +18,21 @@ module Bigcommerce module Prometheus module Servers - module Thin + module Puma ## - # Thin adapter for server + # Puma adapter for server # - class Server < ::Thin::Server + class Server < ::Puma::Server def initialize(port: nil, host: nil, timeout: nil, logger: nil, thread_pool_size: nil) @port = port || ::Bigcommerce::Prometheus.server_port @host = host || ::Bigcommerce::Prometheus.server_host @timeout = timeout || ::Bigcommerce::Prometheus.server_timeout @logger = logger || ::Bigcommerce::Prometheus.logger - @rack_app = ::Bigcommerce::Prometheus::Servers::Thin::RackApp.new(timeout: timeout, logger: logger) - super(@host, @port, @rack_app) - ::Thin::Logging.logger = @logger + @rack_app = ::Bigcommerce::Prometheus::Servers::Puma::RackApp.new(timeout: timeout, logger: logger) + thread_pool_size = (thread_pool_size || ::Bigcommerce::Prometheus.server_thread_pool_size).to_i + super(@app, nil, max_threads: thread_pool_size) + add_tcp_listener(@host, @port) @logger.info "[bigcommerce-prometheus] Prometheus server started on #{@host}:#{@port}" - self.threadpool_size = (thread_pool_size || ::Bigcommerce::Prometheus.server_thread_pool_size).to_i end ## diff --git a/lib/bigcommerce/prometheus/servers/thin/server_metrics.rb b/lib/bigcommerce/prometheus/servers/puma/server_metrics.rb similarity index 99% rename from lib/bigcommerce/prometheus/servers/thin/server_metrics.rb rename to lib/bigcommerce/prometheus/servers/puma/server_metrics.rb index 31d55b3..76df096 100644 --- a/lib/bigcommerce/prometheus/servers/thin/server_metrics.rb +++ b/lib/bigcommerce/prometheus/servers/puma/server_metrics.rb @@ -22,7 +22,7 @@ module Bigcommerce module Prometheus module Servers - module Thin + module Puma ## # Server metrics for the collector # diff --git a/spec/bigcommerce/prometheus/servers/thin/controllers/metrics_controller_spec.rb b/spec/bigcommerce/prometheus/servers/puma/controllers/metrics_controller_spec.rb similarity index 95% rename from spec/bigcommerce/prometheus/servers/thin/controllers/metrics_controller_spec.rb rename to spec/bigcommerce/prometheus/servers/puma/controllers/metrics_controller_spec.rb index 443c80a..e0980e6 100644 --- a/spec/bigcommerce/prometheus/servers/thin/controllers/metrics_controller_spec.rb +++ b/spec/bigcommerce/prometheus/servers/puma/controllers/metrics_controller_spec.rb @@ -15,7 +15,7 @@ # require 'spec_helper' -describe Bigcommerce::Prometheus::Servers::Thin::Controllers::MetricsController do +describe Bigcommerce::Prometheus::Servers::Puma::Controllers::MetricsController do let(:request_method) { 'GET' } let(:env) do { @@ -27,7 +27,7 @@ let(:request) { Rack::Request.new(env) } let(:response) { Rack::Response.new } let(:collector) { PrometheusExporter::Server::Collector.new } - let(:server_metrics) { Bigcommerce::Prometheus::Servers::Thin::ServerMetrics.new } + let(:server_metrics) { Bigcommerce::Prometheus::Servers::Puma::ServerMetrics.new } let(:controller) { described_class.new(request: request, response: response, server_metrics: server_metrics, collector: collector, logger: logger) } let(:metrics) { ['ruby_collector_metrics_total 19', 'ruby_collector_sessions_total 19'].join("\n") } diff --git a/spec/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller_spec.rb b/spec/bigcommerce/prometheus/servers/puma/controllers/send_metrics_controller_spec.rb similarity index 95% rename from spec/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller_spec.rb rename to spec/bigcommerce/prometheus/servers/puma/controllers/send_metrics_controller_spec.rb index 3eac8f2..ef547bb 100644 --- a/spec/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller_spec.rb +++ b/spec/bigcommerce/prometheus/servers/puma/controllers/send_metrics_controller_spec.rb @@ -15,7 +15,7 @@ # require 'spec_helper' -describe Bigcommerce::Prometheus::Servers::Thin::Controllers::SendMetricsController do +describe Bigcommerce::Prometheus::Servers::Puma::Controllers::SendMetricsController do let(:request_method) { 'POST' } let(:env) do { @@ -27,7 +27,7 @@ let(:request) { Rack::Request.new(env) } let(:response) { Rack::Response.new } let(:collector) { PrometheusExporter::Server::Collector.new } - let(:server_metrics) { Bigcommerce::Prometheus::Servers::Thin::ServerMetrics.new } + let(:server_metrics) { Bigcommerce::Prometheus::Servers::Puma::ServerMetrics.new } let(:controller) { described_class.new(request: request, response: response, server_metrics: server_metrics, collector: collector, logger: logger) } describe '#call' do diff --git a/spec/bigcommerce/prometheus/servers/thin/server_spec.rb b/spec/bigcommerce/prometheus/servers/puma/server_spec.rb similarity index 63% rename from spec/bigcommerce/prometheus/servers/thin/server_spec.rb rename to spec/bigcommerce/prometheus/servers/puma/server_spec.rb index f482699..07344fc 100644 --- a/spec/bigcommerce/prometheus/servers/thin/server_spec.rb +++ b/spec/bigcommerce/prometheus/servers/puma/server_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' -describe Bigcommerce::Prometheus::Servers::Thin::Server do - let(:server) { described_class.new } +describe Bigcommerce::Prometheus::Servers::Puma::Server do + let(:server) { described_class.new(port: 9800+rand(100)) } before do Bigcommerce::Prometheus.reset @@ -9,8 +9,8 @@ context 'when the thread pool size is not configured' do it 'falls back to the default configuration' do - expect(server.threadpool_size).to eq ::Bigcommerce::Prometheus.server_thread_pool_size - expect(server.threadpool_size).to eq 3 + expect(server.max_threads).to eq ::Bigcommerce::Prometheus.server_thread_pool_size + expect(server.max_threads).to eq 3 end end @@ -24,7 +24,7 @@ end it 'allows you to set the thread pool size through the configuration block' do - expect(server.threadpool_size).to eq server_thread_pool_size + expect(server.max_threads).to eq server_thread_pool_size end end end From 4b5703a28f3b914bb5b3a6909c0d4139c0d95a58 Mon Sep 17 00:00:00 2001 From: Josh Kleinpeter Date: Thu, 5 Dec 2024 10:35:50 -0600 Subject: [PATCH 2/3] Fix implementation bugs for methods not on Puma::Server --- lib/bigcommerce/prometheus/server.rb | 8 +++----- lib/bigcommerce/prometheus/servers/puma/server.rb | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/bigcommerce/prometheus/server.rb b/lib/bigcommerce/prometheus/server.rb index ebc75a1..7a5600e 100644 --- a/lib/bigcommerce/prometheus/server.rb +++ b/lib/bigcommerce/prometheus/server.rb @@ -52,12 +52,10 @@ def initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil, thr def start @logger.info "[bigcommerce-prometheus][#{@process_name}] Starting prometheus exporter on port #{@host}:#{@port}" - @run_thread = ::Thread.start do - @server.start - end + @run_thread = @server.run @running = true - @logger.info "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter started on #{@host}:#{@port} with #{@server.threadpool_size} threads" + @logger.info "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter started on #{@host}:#{@port} with #{@server.pool_capacity} threads" @server rescue ::StandardError => e @@ -81,7 +79,7 @@ def start_until_stopped # Stop the server # def stop - @server.stop! + @server.stop @run_thread.kill @running = false $stdout.puts "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter cleanly shut down" diff --git a/lib/bigcommerce/prometheus/servers/puma/server.rb b/lib/bigcommerce/prometheus/servers/puma/server.rb index 27eecdb..6e84030 100644 --- a/lib/bigcommerce/prometheus/servers/puma/server.rb +++ b/lib/bigcommerce/prometheus/servers/puma/server.rb @@ -30,7 +30,7 @@ def initialize(port: nil, host: nil, timeout: nil, logger: nil, thread_pool_size @logger = logger || ::Bigcommerce::Prometheus.logger @rack_app = ::Bigcommerce::Prometheus::Servers::Puma::RackApp.new(timeout: timeout, logger: logger) thread_pool_size = (thread_pool_size || ::Bigcommerce::Prometheus.server_thread_pool_size).to_i - super(@app, nil, max_threads: thread_pool_size) + super(@rack_app, nil, max_threads: thread_pool_size) add_tcp_listener(@host, @port) @logger.info "[bigcommerce-prometheus] Prometheus server started on #{@host}:#{@port}" end From 8915ffd56435a27757138b2b543c50464f70b4fb Mon Sep 17 00:00:00 2001 From: Josh Kleinpeter Date: Thu, 5 Dec 2024 11:31:11 -0600 Subject: [PATCH 3/3] Add extra tests --- lib/bigcommerce/prometheus/server.rb | 2 +- .../prometheus/servers/puma/server_spec.rb | 25 +++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/bigcommerce/prometheus/server.rb b/lib/bigcommerce/prometheus/server.rb index 7a5600e..3d2e3b6 100644 --- a/lib/bigcommerce/prometheus/server.rb +++ b/lib/bigcommerce/prometheus/server.rb @@ -55,7 +55,7 @@ def start @run_thread = @server.run @running = true - @logger.info "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter started on #{@host}:#{@port} with #{@server.pool_capacity} threads" + @logger.info "[bigcommerce-prometheus][#{@process_name}] Prometheus exporter started on #{@host}:#{@port} with #{@server.max_threads} threads" @server rescue ::StandardError => e diff --git a/spec/bigcommerce/prometheus/servers/puma/server_spec.rb b/spec/bigcommerce/prometheus/servers/puma/server_spec.rb index 07344fc..9363e45 100644 --- a/spec/bigcommerce/prometheus/servers/puma/server_spec.rb +++ b/spec/bigcommerce/prometheus/servers/puma/server_spec.rb @@ -1,12 +1,18 @@ require 'spec_helper' describe Bigcommerce::Prometheus::Servers::Puma::Server do - let(:server) { described_class.new(port: 9800+rand(100)) } - + let(:server) { described_class.new(port: default_port) } + let(:default_port) { 9800 + rand(100) } before do Bigcommerce::Prometheus.reset end + context 'when the server is initialized' do + it 'has a valid rack app' do + expect(server.app).to be_a(Bigcommerce::Prometheus::Servers::Puma::RackApp) + end + end + context 'when the thread pool size is not configured' do it 'falls back to the default configuration' do expect(server.max_threads).to eq ::Bigcommerce::Prometheus.server_thread_pool_size @@ -27,4 +33,19 @@ expect(server.max_threads).to eq server_thread_pool_size end end + + context 'when the default port is configured' do + let(:server_port) { 9000 + rand(100) } + let(:default_port) { nil } + + before do + Bigcommerce::Prometheus.configure do |c| + c.server_port = server_port + end + end + + it 'allows you to set the port through the configuration block' do + expect(server.connected_ports).to include server_port + end + end end