Skip to content

Commit

Permalink
Record move counts by status
Browse files Browse the repository at this point in the history
This ensures we've got more useful metrics available to Prometheus.
  • Loading branch information
thomasleese committed Apr 11, 2022
1 parent ce3df35 commit 7202977
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/lib/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def configure
end

def record_move_count
move_count_gauge.set(Move.unscoped.count)
Move.unscoped.group(:status).count.each do |status, count|
move_count_gauge.set(count, labels: { status: status })
end
end

private
Expand All @@ -25,6 +27,7 @@ def move_count_gauge
@move_count_gauge ||= registry.gauge(
:app_move_count_total,
docstring: 'The total count of the number of moves.',
labels: %i[status],
store_settings: { aggregation: :max },
)
end
Expand Down
8 changes: 6 additions & 2 deletions spec/lib/metrics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@
before do
allow(move_count_gauge).to receive(:set)
allow(registry).to receive(:gauge).with(:app_move_count_total, anything).and_return(move_count_gauge)

create(:move, :booked)
create(:move, :requested)
end

after { instance.record_move_count }

it 'sets the move count' do
expect(move_count_gauge).to receive(:set)
it 'sets the move count based on the status' do
expect(move_count_gauge).to receive(:set).with(1, labels: { status: 'booked' })
expect(move_count_gauge).to receive(:set).with(1, labels: { status: 'requested' })
end
end
end

0 comments on commit 7202977

Please sign in to comment.