Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions lib/cloud_controller/metrics/statsd_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -133,15 +135,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
Comment thread
philippthun marked this conversation as resolved.
Resolved
end

def logger
@logger ||= Steno.logger('cc.metrics.statsd_updater.redis_store')
end
end
end
Expand Down
62 changes: 62 additions & 0 deletions spec/unit/lib/cloud_controller/metrics/statsd_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -389,6 +417,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
Loading