Skip to content

Commit d323d2d

Browse files
committed
Use specific JSON::ParserError rescue instead of catch-all
1 parent 186820e commit d323d2d

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/bosh-director/lib/bosh/director/metrics_collector.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,12 @@ def fetch_and_update_gauge(endpoint, gauge)
154154
response = Net::HTTP.get_response('127.0.0.1', endpoint, @config.health_monitor_port)
155155
return unless response.is_a?(Net::HTTPSuccess)
156156

157-
deployment_counts = JSON.parse(response.body) rescue nil
157+
begin
158+
deployment_counts = JSON.parse(response.body)
159+
rescue JSON::ParserError => e
160+
@logger.warn("Failed to parse JSON response from #{endpoint}: #{e.message}")
161+
return
162+
end
158163
return unless deployment_counts.is_a?(Hash)
159164

160165
existing_deployment_names = gauge.values.map do |key, _|

src/bosh-director/spec/unit/bosh/director/metrics_collector_spec.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,21 @@ def tick
355355
end
356356

357357
context 'when the health monitor returns a non-json response' do
358+
let(:logger) { double(Logging::Logger) }
359+
358360
before do
361+
allow(config).to receive(:metrics_server_logger).and_return(logger)
362+
allow(logger).to receive(:info)
359363
stub_request(:get, "127.0.0.1:12345/unresponsive_agents")
360-
.to_return(status: 200, body: JSON.dump("bad response"))
364+
.to_return(status: 200, body: "not valid json {")
361365
stub_request(:get, "127.0.0.1:12345/unhealthy_agents")
362-
.to_return(status: 200, body: JSON.dump("bad response"))
366+
.to_return(status: 200, body: "not valid json {")
363367
end
364368

365-
it 'does not emit the vm metrics' do
369+
it 'does not emit the vm metrics and logs a warning' do
370+
expect(logger).to receive(:warn).with(/Failed to parse JSON response from \/unresponsive_agents/)
371+
expect(logger).to receive(:warn).with(/Failed to parse JSON response from \/unhealthy_agents/)
372+
366373
metrics_collector.start
367374
metric = Prometheus::Client.registry.get(:bosh_unresponsive_agents)
368375
expect(metric.values).to be_empty

0 commit comments

Comments
 (0)