Commit 3076b649 authored by Yorick Peterse's avatar Yorick Peterse

Add Prometheus metrics for loose foreign keys

This extends ModificationTracker to keep track of updates and deletions
in Prometheus, in addition to the existing metrics.

Changelog: added
parent cb5df34a
...@@ -12,14 +12,24 @@ module LooseForeignKeys ...@@ -12,14 +12,24 @@ module LooseForeignKeys
@delete_count_by_table = Hash.new { |h, k| h[k] = 0 } @delete_count_by_table = Hash.new { |h, k| h[k] = 0 }
@update_count_by_table = Hash.new { |h, k| h[k] = 0 } @update_count_by_table = Hash.new { |h, k| h[k] = 0 }
@start_time = monotonic_time @start_time = monotonic_time
@deletes_counter = Gitlab::Metrics.counter(
:loose_foreign_key_deletions,
'The number of loose foreign key deletions'
)
@updates_counter = Gitlab::Metrics.counter(
:loose_foreign_key_updates,
'The number of loose foreign key updates'
)
end end
def add_deletions(table, count) def add_deletions(table, count)
@delete_count_by_table[table] += count @delete_count_by_table[table] += count
@deletes_counter.increment({ table: table }, count)
end end
def add_updates(table, count) def add_updates(table, count)
@update_count_by_table[table] += count @update_count_by_table[table] += count
@updates_counter.increment({ table: table }, count)
end end
def over_limit? def over_limit?
......
...@@ -50,6 +50,26 @@ RSpec.describe LooseForeignKeys::ModificationTracker do ...@@ -50,6 +50,26 @@ RSpec.describe LooseForeignKeys::ModificationTracker do
end end
end end
describe '#add_deletions' do
it 'increments a Prometheus counter' do
counter = Gitlab::Metrics.registry.get(:loose_foreign_key_deletions)
subject.add_deletions(:users, 4)
expect(counter.get(table: :users)).to eq(4)
end
end
describe '#add_updates' do
it 'increments a Prometheus counter' do
counter = Gitlab::Metrics.registry.get(:loose_foreign_key_updates)
subject.add_updates(:users, 4)
expect(counter.get(table: :users)).to eq(4)
end
end
describe '#stats' do describe '#stats' do
it 'exposes stats' do it 'exposes stats' do
freeze_time do freeze_time do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment