Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from thin to puma for the web server #25

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bc-prometheus-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 10 additions & 9 deletions lib/bigcommerce/prometheus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
##
Expand Down
10 changes: 4 additions & 6 deletions lib/bigcommerce/prometheus/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.max_threads} threads"

@server
rescue ::StandardError => e
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
module Bigcommerce
module Prometheus
module Servers
module Thin
module Puma
module Controllers
##
# Handle 500s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Bigcommerce
module Prometheus
module Servers
module Thin
module Puma
module Controllers
##
# GET /metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
module Bigcommerce
module Prometheus
module Servers
module Thin
module Puma
module Controllers
##
# Handle invalid requests to server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
module Bigcommerce
module Prometheus
module Servers
module Thin
module Puma
module Controllers
##
# POST /send-metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
##
Expand All @@ -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)
Expand All @@ -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

##
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(@rack_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

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
module Bigcommerce
module Prometheus
module Servers
module Thin
module Puma
##
# Server metrics for the collector
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
51 changes: 51 additions & 0 deletions spec/bigcommerce/prometheus/servers/puma/server_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

describe Bigcommerce::Prometheus::Servers::Puma::Server do
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
expect(server.max_threads).to eq 3
end
end

context 'when the default thread pool size is configured' do
let(:server_thread_pool_size) { 12 }

before do
Bigcommerce::Prometheus.configure do |c|
c.server_thread_pool_size = server_thread_pool_size
end
end

it 'allows you to set the thread pool size through the configuration block' do
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
30 changes: 0 additions & 30 deletions spec/bigcommerce/prometheus/servers/thin/server_spec.rb

This file was deleted.