Skip to content

Commit 1fc1fa0

Browse files
committed
Load delayed job metric plugin only for cc workers
The metrics plugin is only needed for the dedicated cc workers processes. Additionally: - Improve tests to avoid sporadic errors when metric is updated twice. - Raise an error if `publish_metrics` is true and execution context is not set for cc workers. For the local workers there is no concept yet on how to publish metrics.
1 parent 1dde8b0 commit 1fc1fa0

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

lib/cloud_controller/metrics/prometheus_updater.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def register_metrics_for_process
103103
METRICS.each { |metric| register(metric) }
104104
PUMA_METRICS.each { |metric| register(metric) }
105105
else
106-
raise 'Could not register Prometheus metrics: Unknown execution context'
106+
raise "Could not register Prometheus metrics: Unexpected execution context: #{@execution_context.inspect}"
107107
end
108108
end
109109
# rubocop:enable Metrics/CyclomaticComplexity

lib/delayed_job/delayed_worker.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ def initialize(options)
2727
def start_working
2828
config = RakeConfig.config
2929
if @publish_metrics
30+
# Local API workers don't have a concept of publishing metrics
31+
unless VCAP::CloudController::ExecutionContext.from_process_type_env == VCAP::CloudController::ExecutionContext::CC_WORKER
32+
raise 'Metric publishing is only supported for cc workers'
33+
end
34+
3035
setup_metrics(config)
3136
periodic_updater = CloudController::DependencyLocator.instance.vitals_periodic_updater
3237
periodic_updater.setup_updates
@@ -122,6 +127,8 @@ def setup_metrics(config)
122127
prometheus_dir = File.join(config.get(:directories, :tmpdir), 'prometheus')
123128
Prometheus::Client.config.data_store = Prometheus::Client::DataStores::DirectFileStore.new(dir: prometheus_dir)
124129

130+
Delayed::Worker.plugins << DelayedJobMetrics::Plugin unless Delayed::Worker.plugins.include?(DelayedJobMetrics::Plugin)
131+
125132
VCAP::CloudController::StandaloneMetricsWebserver.start_for_bosh_job(config.get(:prometheus_port) || 9394) if is_first_generic_worker_on_machine?
126133
end
127134
end

lib/delayed_job_plugins/delayed_jobs_metrics.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,3 @@ def prometheus
2121
end
2222
end
2323
end
24-
25-
Delayed::Worker.plugins << DelayedJobMetrics::Plugin

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module VCAP::CloudController::Metrics
3434

3535
it 'raises error when execution context is nil' do
3636
allow(VCAP::CloudController::ExecutionContext).to receive(:from_process_type_env).and_return(nil)
37-
expect { updater }.to raise_error('Could not register Prometheus metrics: Unknown execution context')
37+
expect { updater }.to raise_error('Could not register Prometheus metrics: Unexpected execution context: nil')
3838
end
3939

4040
context 'when execution context is set' do

spec/unit/lib/delayed_job/delayed_worker_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,16 +214,35 @@
214214
end
215215
end
216216

217+
context 'when set to true but not in CC_WORKER context' do
218+
before do
219+
options[:publish_metrics] = true
220+
allow(VCAP::CloudController::ExecutionContext).to receive(:from_process_type_env).and_return(nil)
221+
end
222+
223+
it 'raises an error' do
224+
expect do
225+
cc_delayed_worker.start_working
226+
end.to raise_error('Metric publishing is only supported for cc workers')
227+
end
228+
end
229+
217230
context 'when set to true' do
218231
before do
219232
options[:publish_metrics] = true
233+
allow(VCAP::CloudController::ExecutionContext).to receive(:from_process_type_env).and_return(VCAP::CloudController::ExecutionContext::CC_WORKER)
220234
end
221235

222236
it 'publishes metrics' do
223237
cc_delayed_worker.start_working
224238
expect(Prometheus::Client::DataStores::DirectFileStore).to have_received(:new)
225239
end
226240

241+
it 'loads the delayed job metrics plugin' do
242+
cc_delayed_worker.start_working
243+
expect(Delayed::Worker.plugins).to include(DelayedJobMetrics::Plugin)
244+
end
245+
227246
context 'when first worker on machine' do
228247
before do
229248
allow(cc_delayed_worker).to receive(:is_first_generic_worker_on_machine?).and_return(true)

spec/unit/lib/delayed_job_plugins/delayed_jobs_metrics_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
let(:prometheus) { instance_double(VCAP::CloudController::Metrics::PrometheusUpdater) }
55

66
before do
7+
Delayed::Worker.plugins << DelayedJobMetrics::Plugin
78
DelayedJobMetrics::Plugin.prometheus = prometheus
89
allow(prometheus).to receive(:update_histogram_metric)
910
end
@@ -33,13 +34,13 @@
3334
:cc_job_pickup_delay_seconds,
3435
be_within(0.5).of(10.0),
3536
labels: { queue: VCAP::CloudController::Jobs::Queues.generic, worker: 'test_worker' }
36-
).once
37+
).at_least(:once)
3738

3839
expect(prometheus).to have_received(:update_histogram_metric).with(
3940
:cc_job_duration_seconds,
4041
kind_of(Numeric),
4142
labels: { queue: VCAP::CloudController::Jobs::Queues.generic, worker: 'test_worker' }
42-
).once
43+
).at_least(:once)
4344
end
4445
end
4546
end

0 commit comments

Comments
 (0)