From be47b5c627928ad17e0dc0668b2e625d257618aa Mon Sep 17 00:00:00 2001 From: Katharina Przybill <30441792+kathap@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:27:11 +0100 Subject: [PATCH 1/2] Fail open on Redis errors, consistent with other Valkey callers StatsdUpdater's RedisStore updates the outstanding-requests gauge in Valkey on every request. Any Redis error propagated out of the request-metrics middleware and failed the request with a 500. Wrap the RedisStore operations in a fail_open helper that logs and swallows Redis errors, matching the existing handling in base_rate_limiter.rb and concurrent_request_counter.rb: rescue Redis::BaseError, log at error with "Redis error: - ". --- .../metrics/statsd_updater.rb | 20 +++++++++-- .../metrics/statsd_updater_spec.rb | 34 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/cloud_controller/metrics/statsd_updater.rb b/lib/cloud_controller/metrics/statsd_updater.rb index 685ee75b7ce..40d85798fdc 100644 --- a/lib/cloud_controller/metrics/statsd_updater.rb +++ b/lib/cloud_controller/metrics/statsd_updater.rb @@ -133,15 +133,29 @@ def initialize(socket) end @prefix = 'metrics' @request_outstanding_gauge_key = "#{@prefix}:cc.requests.outstanding.gauge" - @redis.set(@request_outstanding_gauge_key, 0) + fail_open { @redis.set(@request_outstanding_gauge_key, 0) } end def increment_request_outstanding_gauge - @redis.incr(@request_outstanding_gauge_key) + fail_open { @redis.incr(@request_outstanding_gauge_key) } end def decrement_request_outstanding_gauge - @redis.decr(@request_outstanding_gauge_key) + fail_open { @redis.decr(@request_outstanding_gauge_key) } + end + + private + + # on any Redis error (eg valkey unreachable) log it and carry on. + def fail_open + yield + rescue Redis::BaseError => e + logger.error("Redis error: #{e.class} - #{e.message}") + nil + end + + def logger + @logger ||= Steno.logger('cc.metrics.statsd_updater.redis_store') end end end diff --git a/spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb b/spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb index eb209cce9b4..0e0d234d6cd 100644 --- a/spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb +++ b/spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb @@ -389,6 +389,40 @@ module VCAP::CloudController::Metrics allow(redis).to receive(:decr).with('metrics:cc.requests.outstanding.gauge').and_return(-1) expect(redis_store.decrement_request_outstanding_gauge).to eq(-1) end + + context 'when valkey is unreachable' do + before do + allow(redis).to receive(:incr).and_raise(Redis::CannotConnectError.new('connect(2) for valkey.sock')) + allow(redis).to receive(:decr).and_raise(Redis::TimeoutError) + end + + it 'does not raise when incrementing, returning nil' do + expect(redis_store.increment_request_outstanding_gauge).to be_nil + end + + it 'does not raise when decrementing, returning nil' do + expect(redis_store.decrement_request_outstanding_gauge).to be_nil + end + + it 'logs the redis error' do + logger = instance_double(Steno::Logger, error: nil) + allow(Steno).to receive(:logger).and_return(logger) + + redis_store.increment_request_outstanding_gauge + + expect(logger).to have_received(:error).with(/Redis error: Redis::CannotConnectError/) + end + end + + context 'when Redis raises a command error' do + before do + allow(redis).to receive(:decr).and_raise(Redis::CommandError) + end + + it 'fails open and returns nil' do + expect(redis_store.decrement_request_outstanding_gauge).to be_nil + end + end end end end From 2d7802d0d232fecf6b2226e17fa96832b4efee67 Mon Sep 17 00:00:00 2001 From: Katharina Przybill <30441792+kathap@users.noreply.github.com> Date: Mon, 23 Nov 2026 11:43:16 +0100 Subject: [PATCH 2/2] Skip gauge when Valkey is unreachable --- .../metrics/statsd_updater.rb | 6 ++-- .../metrics/statsd_updater_spec.rb | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/cloud_controller/metrics/statsd_updater.rb b/lib/cloud_controller/metrics/statsd_updater.rb index 40d85798fdc..7f684d028ce 100644 --- a/lib/cloud_controller/metrics/statsd_updater.rb +++ b/lib/cloud_controller/metrics/statsd_updater.rb @@ -81,13 +81,15 @@ def report_staging_failure_metrics(duration_ns) def start_request @statsd.increment('cc.requests.outstanding') - @statsd.gauge('cc.requests.outstanding.gauge', store.increment_request_outstanding_gauge) + outstanding_gauge = store.increment_request_outstanding_gauge + @statsd.gauge('cc.requests.outstanding.gauge', outstanding_gauge) unless outstanding_gauge.nil? end def complete_request(status) http_status_code = "#{status.to_s[0]}XX" http_status_metric = "cc.http_status.#{http_status_code}" - @statsd.gauge('cc.requests.outstanding.gauge', store.decrement_request_outstanding_gauge) + outstanding_gauge = store.decrement_request_outstanding_gauge + @statsd.gauge('cc.requests.outstanding.gauge', outstanding_gauge) unless outstanding_gauge.nil? @statsd.increment('cc.requests.outstanding', -1) @statsd.increment('cc.requests.completed') @statsd.increment(http_status_metric) diff --git a/spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb b/spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb index 0e0d234d6cd..66024406cc8 100644 --- a/spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb +++ b/spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb @@ -246,6 +246,19 @@ module VCAP::CloudController::Metrics expect(statsd_client).to have_received(:gauge).with('cc.requests.outstanding.gauge', 4) expect(statsd_client).to have_received(:increment).with('cc.requests.outstanding') end + + context 'when the store returns nil (eg valkey unreachable)' do + before do + allow(store).to receive(:increment_request_outstanding_gauge).and_return(nil) + end + + it 'does not send the gauge metric' do + updater.start_request + + expect(statsd_client).not_to have_received(:gauge).with('cc.requests.outstanding.gauge', anything) + expect(statsd_client).to have_received(:increment).with('cc.requests.outstanding') + end + end end describe '#complete_request' do @@ -278,6 +291,21 @@ module VCAP::CloudController::Metrics updater.complete_request(400) expect(statsd_client).to have_received(:increment).with('cc.http_status.4XX') end + + context 'when the store returns nil (eg valkey unreachable)' do + before do + allow(store).to receive(:decrement_request_outstanding_gauge).and_return(nil) + end + + it 'does not send the gauge metric but still records the other metrics' do + updater.complete_request(status) + + expect(statsd_client).not_to have_received(:gauge).with('cc.requests.outstanding.gauge', anything) + expect(statsd_client).to have_received(:increment).with('cc.requests.outstanding', -1) + expect(statsd_client).to have_received(:increment).with('cc.requests.completed') + expect(statsd_client).to have_received(:increment).with('cc.http_status.2XX') + end + end end describe '#store' do