Skip to content

Commit afb0dbb

Browse files
committed
Fix code style and unit tests
1 parent fd63b36 commit afb0dbb

File tree

7 files changed

+185
-117
lines changed

7 files changed

+185
-117
lines changed

lib/cloud_controller/db.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def self.add_connection_expiration_extension(db, opts)
7171
end
7272

7373
def self.add_connection_metrics_extension(db)
74-
# only add the metrics for api processes. Otherwise e.g. rake db:migrate would also initialize metric updaters, which need additional config
74+
# only add the metrics for api and cc-worker processes. Otherwise e.g. rake db:migrate would also initialize metric updaters, which need additional config
7575
return if Object.const_defined?(:RakeConfig) && RakeConfig.context != :worker
7676

7777
db.extension(:connection_metrics)

lib/cloud_controller/metrics/prometheus_updater.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ def initialize(registry: Prometheus::Client.registry, cc_worker: false)
8181
PUMA_METRICS.each { |metric| register(metric) } if VCAP::CloudController::Config.config&.get(:webserver) == 'puma'
8282
end
8383

84-
def registry
85-
@registry
86-
end
87-
8884
def update_gauge_metric(metric, value, labels: {})
8985
@registry.get(metric).set(value, labels:)
9086
end

lib/delayed_job/delayed_worker.rb

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def initialize(options)
2222

2323
def start_working
2424
config = RakeConfig.config
25-
setup_metrics_endpoint(config) if @publish_metrics
25+
setup_metrics(config) if @publish_metrics
2626
BackgroundJobEnvironment.new(config).setup_environment(readiness_port)
2727

2828
logger = Steno.logger('cc-worker')
@@ -99,36 +99,39 @@ def is_first_generic_worker_on_machine?
9999
RakeConfig.context != :api && ENV['INDEX']&.to_i == 1
100100
end
101101

102-
def setup_metrics_endpoint(config)
102+
def setup_metrics(config)
103103
prometheus_dir = File.join(config.get(:directories, :tmpdir), 'prometheus')
104104
Prometheus::Client.config.data_store = Prometheus::Client::DataStores::DirectFileStore.new(dir: prometheus_dir)
105105

106-
if is_first_generic_worker_on_machine?
107-
FileUtils.mkdir_p(prometheus_dir)
106+
setup_webserver(config, prometheus_dir) if is_first_generic_worker_on_machine?
108107

109-
# Resetting metrics on startup
110-
Dir["#{prometheus_dir}/*.bin"].each do |file_path|
111-
File.unlink(file_path)
112-
end
108+
# initialize metric with 0 for discoverability, because it likely won't get updated on healthy systems
109+
CloudController::DependencyLocator.instance.cc_worker_prometheus_updater.update_gauge_metric(:cc_db_connection_pool_timeouts_total, 0, labels: { process_type: 'cc-worker' })
110+
end
113111

114-
metrics_app = Rack::Builder.new do
115-
use Prometheus::Middleware::Exporter, path: '/metrics'
112+
def setup_webserver(config, prometheus_dir)
113+
FileUtils.mkdir_p(prometheus_dir)
116114

117-
map '/' do
118-
run lambda { |env|
119-
# Return 404 for any other request
120-
['404', { 'Content-Type' => 'text/plain' }, ['Not Found']]
121-
}
122-
end
123-
end
115+
# Resetting metrics on startup
116+
Dir["#{prometheus_dir}/*.bin"].each do |file_path|
117+
File.unlink(file_path)
118+
end
119+
120+
metrics_app = Rack::Builder.new do
121+
use Prometheus::Middleware::Exporter, path: '/metrics'
124122

125-
Thread.new do
126-
server = Puma::Server.new(metrics_app)
127-
server.add_tcp_listener '0.0.0.0', config.get(:prometheus_port) || 9394
128-
server.run
123+
map '/' do
124+
run lambda { |_env|
125+
# Return 404 for any other request
126+
['404', { 'Content-Type' => 'text/plain' }, ['Not Found']]
127+
}
129128
end
130129
end
131130

132-
CloudController::DependencyLocator.instance.cc_worker_prometheus_updater.update_gauge_metric(:cc_db_connection_pool_timeouts_total, 0, labels: { process_type: 'cc-worker' })
131+
Thread.new do
132+
server = Puma::Server.new(metrics_app)
133+
server.add_tcp_listener '0.0.0.0', config.get(:prometheus_port) || 9394
134+
server.run
135+
end
133136
end
134137
end

lib/sequel/extensions/connection_metrics.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def self.extended(pool)
2424
pool.instance_exec do
2525
sync do
2626
@prometheus_updater = if process_type == 'cc-worker'
27-
CloudController::DependencyLocator.instance.cc_worker_prometheus_updater
27+
CloudController::DependencyLocator.instance.cc_worker_prometheus_updater
2828
else
29-
CloudController::DependencyLocator.instance.prometheus_updater
29+
CloudController::DependencyLocator.instance.prometheus_updater
3030
end
3131
@connection_info = {}
3232
end

spec/unit/lib/cloud_controller/metrics/prometheus_updater_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module VCAP::CloudController::Metrics
55
RSpec.describe PrometheusUpdater do
6-
let(:updater) { PrometheusUpdater.new(prom_client) }
6+
let(:updater) { PrometheusUpdater.new(registry: prom_client) }
77
let(:tmpdir) { Dir.mktmpdir }
88
let(:prom_client) do
99
Prometheus::Client.config.data_store = Prometheus::Client::DataStores::DirectFileStore.new(dir: tmpdir)

spec/unit/lib/delayed_job/delayed_worker_spec.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@
6464
worker_instance = CloudController::DelayedWorker.new(options)
6565
expect(worker_instance.instance_variable_get(:@queue_options)).not_to include(:grace_period_seconds)
6666
end
67+
68+
describe 'publish metrics' do
69+
context 'when not set' do
70+
it 'does not publish metrics' do
71+
worker_instance = CloudController::DelayedWorker.new(options)
72+
expect(worker_instance.instance_variable_get(:@publish_metrics)).to be(false)
73+
end
74+
end
75+
76+
context 'when set to true' do
77+
before do
78+
options[:publish_metrics] = true
79+
end
80+
81+
it 'publishes metrics' do
82+
worker_instance = CloudController::DelayedWorker.new(options)
83+
expect(worker_instance.instance_variable_get(:@publish_metrics)).to be(true)
84+
end
85+
end
86+
end
6787
end
6888

6989
describe '#start_working' do
@@ -120,6 +140,47 @@
120140
cc_delayed_worker.start_working
121141
end
122142
end
143+
144+
describe 'publish metrics' do
145+
before do
146+
allow(Prometheus::Client::DataStores::DirectFileStore).to receive(:new)
147+
end
148+
149+
context 'when set to false' do
150+
before do
151+
options[:publish_metrics] = false
152+
end
153+
154+
it 'does not publish metrics' do
155+
cc_delayed_worker.start_working
156+
expect(Prometheus::Client::DataStores::DirectFileStore).not_to have_received(:new)
157+
end
158+
end
159+
160+
context 'when set to true' do
161+
before do
162+
options[:publish_metrics] = true
163+
end
164+
165+
it 'publishes metrics' do
166+
cc_delayed_worker.start_working
167+
expect(Prometheus::Client::DataStores::DirectFileStore).to have_received(:new)
168+
end
169+
170+
context 'when first worker on machine' do
171+
before do
172+
allow(cc_delayed_worker).to receive(:is_first_generic_worker_on_machine?).and_return(true)
173+
allow(cc_delayed_worker).to receive(:readiness_port)
174+
allow(cc_delayed_worker).to receive(:setup_webserver)
175+
end
176+
177+
it 'sets up a webserver' do
178+
cc_delayed_worker.start_working
179+
expect(cc_delayed_worker).to have_received(:setup_webserver)
180+
end
181+
end
182+
end
183+
end
123184
end
124185

125186
describe '#clear_locks!' do

0 commit comments

Comments
 (0)