diff --git a/app/services/search/base.rb b/app/services/search/base.rb index 8eef4e0f0..635e8ea5b 100644 --- a/app/services/search/base.rb +++ b/app/services/search/base.rb @@ -37,6 +37,10 @@ module Search Search::Client.indices.put_mapping(index: index_alias, body: self::MAPPINGS) end + def document_count + Search::Client.count(index: self::INDEX_ALIAS).dig("count") + end + private def search(body:) diff --git a/app/workers/metrics/record_db_table_counts_worker.rb b/app/workers/metrics/record_data_counts_worker.rb similarity index 66% rename from app/workers/metrics/record_db_table_counts_worker.rb rename to app/workers/metrics/record_data_counts_worker.rb index 1d0c6c32b..95f80d4b1 100644 --- a/app/workers/metrics/record_db_table_counts_worker.rb +++ b/app/workers/metrics/record_data_counts_worker.rb @@ -1,5 +1,5 @@ module Metrics - class RecordDbTableCountsWorker + class RecordDataCountsWorker include Sidekiq::Worker sidekiq_options queue: :low_priority, retry: 10 @@ -9,6 +9,11 @@ module Metrics 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 }) + + next unless model.const_defined?(:SEARCH_CLASS) + + document_count = model::SEARCH_CLASS.document_count + DatadogStatsClient.gauge("elasticsearch.document_count", document_count, tags: { table_name: model.table_name }) end end end diff --git a/lib/tasks/fetch.rake b/lib/tasks/fetch.rake index 4422f11fa..ce4166efc 100644 --- a/lib/tasks/fetch.rake +++ b/lib/tasks/fetch.rake @@ -117,8 +117,8 @@ task fix_credits_count_cache: :environment do Credit.counter_culture_fix_counts only: %i[user organization] end -task record_db_table_counts: :environment do - Metrics::RecordDbTableCountsWorker.perform_async +task record_data_counts: :environment do + Metrics::RecordDataCountsWorker.perform_async end task log_worker_queue_stats: :environment do diff --git a/spec/workers/metrics/record_db_table_counts_worker_spec.rb b/spec/workers/metrics/record_data_counts_worker_spec.rb similarity index 67% rename from spec/workers/metrics/record_db_table_counts_worker_spec.rb rename to spec/workers/metrics/record_data_counts_worker_spec.rb index b065184e7..8934b155a 100644 --- a/spec/workers/metrics/record_db_table_counts_worker_spec.rb +++ b/spec/workers/metrics/record_data_counts_worker_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe Metrics::RecordDbTableCountsWorker, type: :worker do +RSpec.describe Metrics::RecordDataCountsWorker, type: :worker do default_logger = Rails.logger include_examples "#enqueues_on_correct_queue", "low_priority", 1 @@ -22,5 +22,14 @@ RSpec.describe Metrics::RecordDbTableCountsWorker, type: :worker do DatadogStatsClient, ).to have_received(:gauge).with("postgres.db_table_size", 0, Hash).at_least(1) end + + it "logs index counts in Datadog" do + allow(DatadogStatsClient).to receive(:gauge) + described_class.new.perform + + expect( + DatadogStatsClient, + ).to have_received(:gauge).with("elasticsearch.document_count", 0, Hash).at_least(1) + end end end