Skip to content

Commit 67fc84b

Browse files
authored
Merge pull request #9 from pedelman/configurable-server-threadpool
Adds ability to configure the server threadpool size
2 parents f283ce2 + 45896cf commit 67fc84b

8 files changed

Lines changed: 55 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Changelog for the bc-prometheus-ruby gem.
22

33
### Pending Release
44

5+
- Add configuration to control Thin web server thread pool size. Note that the default number of threads is changing from 20 to 3. You can configure this using an environment variable or initializer.
56
- Update rubocop to 1.0
67

78
### 0.3.1

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,16 @@ Bigcommerce::Prometheus::Instrumentors::Resque.new(app: Rails.application).start
3939

4040
After requiring the main file, you can further configure with:
4141

42-
| Option | Description | Default |
43-
| ------ | ----------- | ------- |
44-
| client_custom_labels | A hash of custom labels to send with each client request | `{}` |
45-
| client_max_queue_size | The max amount of metrics to send before flushing | 10000 |
46-
| client_thread_sleep | How often to sleep the worker thread that manages the client buffer (seconds) | 0.5 |
47-
| puma_collection_frequency | How often to poll puma collection metrics (seconds) | 30 |
48-
| server_host | The host to run the exporter on | 0.0.0.0 |
49-
| server_port | The port to run the exporter on | 9394 |
50-
| process_name | What the current process name is. Used in logging. | `ENV['PROCESS']` |
42+
| Option | Description | Default | Environment Variable |
43+
| ------ | ----------- | ------- | -------------------- |
44+
| client_custom_labels | A hash of custom labels to send with each client request | `{}` | None |
45+
| client_max_queue_size | The max amount of metrics to send before flushing | `10000` | `ENV['PROMETHEUS_CLIENT_MAX_QUEUE_SIZE']` |
46+
| client_thread_sleep | How often to sleep the worker thread that manages the client buffer (seconds) | `0.5` | `ENV['PROMETHEUS_CLIENT_THREAD_SLEEP']` |
47+
| puma_collection_frequency | How often to poll puma collection metrics (seconds) | `30` | `ENV['PROMETHEUS_PUMA_COLLECTION_FREQUENCY']` |
48+
| server_host | The host to run the exporter on | `"0.0.0.0"` | `ENV['PROMETHEUS_SERVER_HOST']` |
49+
| server_port | The port to run the exporter on | `9394` | `ENV['PROMETHEUS_SERVER_PORT']` |
50+
| server_thread_pool_size | The number of threads used for the exporter server | `3` | `ENV['PROMETHEUS_SERVER_THREAD_POOL_SIZE']` |
51+
| process_name | What the current process name is (used in logging) | `"unknown"` | `ENV['PROCESS']` |
5152

5253
## Custom Collectors
5354

lib/bigcommerce/prometheus/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module Configuration
4242
server_port: ENV.fetch('PROMETHEUS_SERVER_PORT', PrometheusExporter::DEFAULT_PORT).to_i,
4343
server_timeout: ENV.fetch('PROMETHEUS_DEFAULT_TIMEOUT', PrometheusExporter::DEFAULT_TIMEOUT).to_i,
4444
server_prefix: ENV.fetch('PROMETHEUS_DEFAULT_PREFIX', PrometheusExporter::DEFAULT_PREFIX).to_s,
45+
server_thread_pool_size: ENV.fetch('PROMETHEUS_SERVER_THREAD_POOL_SIZE', 3).to_i,
4546

4647
# Custom collector configuration
4748
collector_collection_frequency: ENV.fetch('PROMETHEUS_DEFAULT_COLLECTOR_COLLECTION_FREQUENCY_SEC', 15).to_i,

lib/bigcommerce/prometheus/server.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,21 @@ class Server
2626
# @param [Integer] port
2727
# @param [Integer] timeout
2828
# @param [String] prefix
29+
# @param [Integer] thread_pool_size
2930
#
30-
def initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil)
31+
def initialize(host: nil, port: nil, timeout: nil, prefix: nil, logger: nil, thread_pool_size: nil)
3132
@host = host || ::Bigcommerce::Prometheus.server_host
3233
@port = (port || ::Bigcommerce::Prometheus.server_port).to_i
3334
@timeout = (timeout || ::Bigcommerce::Prometheus.server_timeout).to_i
3435
@prefix = (prefix || ::PrometheusExporter::DEFAULT_PREFIX).to_s
3536
@process_name = ::Bigcommerce::Prometheus.process_name
3637
@logger = logger || ::Bigcommerce::Prometheus.logger
37-
@server = ::Bigcommerce::Prometheus::Servers::Thin::Server.new(port: @port, timeout: @timeout, logger: @logger)
38+
@server = ::Bigcommerce::Prometheus::Servers::Thin::Server.new(
39+
port: @port,
40+
timeout: @timeout,
41+
logger: @logger,
42+
thread_pool_size: (thread_pool_size || ::Bigcommerce::Prometheus.server_thread_pool_size).to_i
43+
)
3844
@running = false
3945
::PrometheusExporter::Metric::Base.default_prefix = @prefix
4046
setup_signal_handlers
@@ -51,7 +57,7 @@ def start
5157
end
5258
@running = true
5359

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

5662
@server
5763
rescue ::StandardError => e

lib/bigcommerce/prometheus/servers/thin/controllers/send_metrics_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ module Controllers
2525
#
2626
class SendMetricsController < BaseController
2727
class BadMetricsError < StandardError; end
28+
2829
class InvalidRequestError < StandardError; end
2930

3031
##

lib/bigcommerce/prometheus/servers/thin/server.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ module Thin
2323
# Thin adapter for server
2424
#
2525
class Server < ::Thin::Server
26-
def initialize(port:, host: nil, timeout: nil, logger: nil)
26+
def initialize(port: nil, host: nil, timeout: nil, logger: nil, thread_pool_size: nil)
2727
@port = port || ::Bigcommerce::Prometheus.server_port
2828
@host = host || ::Bigcommerce::Prometheus.server_host
2929
@timeout = timeout || ::Bigcommerce::Prometheus.server_timeout
3030
@logger = logger || ::Bigcommerce::Prometheus.logger
3131
@rack_app = ::Bigcommerce::Prometheus::Servers::Thin::RackApp.new(timeout: timeout, logger: logger)
3232
super(@host, @port, @rack_app)
3333
::Thin::Logging.logger = @logger
34+
self.threadpool_size = (thread_pool_size || ::Bigcommerce::Prometheus.server_thread_pool_size).to_i
3435
end
3536

3637
##

lib/bigcommerce/prometheus/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
#
1818
module Bigcommerce
1919
module Prometheus
20-
VERSION = '0.3.2.pre'
20+
VERSION = '0.4.0.pre'
2121
end
2222
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'spec_helper'
2+
3+
describe Bigcommerce::Prometheus::Servers::Thin::Server do
4+
let(:server) { described_class.new }
5+
6+
before do
7+
Bigcommerce::Prometheus.reset
8+
end
9+
10+
context 'when the thread pool size is not configured' do
11+
it 'falls back to the default configuration' do
12+
expect(server.threadpool_size).to eq ::Bigcommerce::Prometheus.server_thread_pool_size
13+
expect(server.threadpool_size).to eq 3
14+
end
15+
end
16+
17+
context 'when the default thread pool size is configured' do
18+
let(:server_thread_pool_size) { 12 }
19+
20+
before do
21+
Bigcommerce::Prometheus.configure do |c|
22+
c.server_thread_pool_size = server_thread_pool_size
23+
end
24+
end
25+
26+
it 'allows you to set the thread pool size through the configuration block' do
27+
expect(server.threadpool_size).to eq server_thread_pool_size
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)