Log DB Table sizes in a Sidekiq Worker (#5518)

This commit is contained in:
Molly Struve 2020-01-15 12:33:14 -05:00 committed by GitHub
parent 4f9321e6f4
commit 2e5a36e538
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 6 deletions

View 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

View file

@ -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

View 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