Log DB Table sizes in a Sidekiq Worker (#5518)
This commit is contained in:
parent
4f9321e6f4
commit
2e5a36e538
3 changed files with 32 additions and 6 deletions
15
app/workers/metrics/record_db_table_counts_worker.rb
Normal file
15
app/workers/metrics/record_db_table_counts_worker.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
module Metrics
|
||||
class RecordDbTableCountsWorker
|
||||
include Sidekiq::Worker
|
||||
sidekiq_options queue: :low_priority, retry: 10
|
||||
|
||||
def perform
|
||||
models = [User, Article, Organization, Comment, Podcast, ClassifiedListing, PageView]
|
||||
models.each do |model|
|
||||
estimate = model.estimated_count
|
||||
Rails.logger.info("db_table_size", table_info: { table_name: model.table_name, table_size: estimate })
|
||||
DataDogStatsClient.gauge("postgres.db_table_size", estimate, tags: { table_name: model.table_name })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -113,12 +113,7 @@ task fix_credits_count_cache: :environment do
|
|||
end
|
||||
|
||||
task record_db_table_counts: :environment do
|
||||
models = [User, Article, Organization, Comment, Podcast, ClassifiedListing, PageView]
|
||||
models.each do |model|
|
||||
estimate = model.estimated_count
|
||||
Rails.logger.info("db_table_size", table_info: { table_name: model.table_name, table_size: estimate })
|
||||
DataDogStatsClient.gauge("postgres.db_table_size", estimate, tags: { table_name: model.table_name })
|
||||
end
|
||||
RecordDbTableCountsWorker.perform_async
|
||||
end
|
||||
|
||||
task log_worker_queue_stats: :environment do
|
||||
|
|
|
|||
16
spec/workers/metrics/record_db_table_counts_worker_spec.rb
Normal file
16
spec/workers/metrics/record_db_table_counts_worker_spec.rb
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe Metrics::RecordDbTableCountsWorker, type: :worker do
|
||||
include_examples "#enqueues_on_correct_queue", "low_priority", 1
|
||||
|
||||
describe "#perform" do
|
||||
it "logs estimated counts in Datadog" do
|
||||
allow(DataDogStatsClient).to receive(:gauge)
|
||||
described_class.new.perform
|
||||
|
||||
expect(
|
||||
DataDogStatsClient,
|
||||
).to have_received(:gauge).with("postgres.db_table_size", 0, Hash).at_least(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue