Skip to content

Commit

Permalink
[DBX-88683] update 526 state logger with StatsD
Browse files Browse the repository at this point in the history
  • Loading branch information
SamStuckey committed Sep 4, 2024
1 parent 05ca84f commit 1edfa12
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
51 changes: 38 additions & 13 deletions app/sidekiq/form526_state_logging_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ class Form526StateLoggingJob

END_DATE = Time.zone.today.beginning_of_day
START_DATE = END_DATE - 1.week
STATSD_PREFIX = 'form526.state'

def initialize(start_date: START_DATE, end_date: END_DATE)
@start_date = start_date
@end_date = end_date
end

def perform
Rails.logger.info('Form 526 State Data',
state_log: abbreviated_state_log,
start_date:,
end_date:)
write_as_gauges
write_as_log
rescue => e
Rails.logger.error('Error logging 526 state data',
class: self.class.name,
Expand All @@ -28,20 +27,46 @@ def perform
end_date:)
end

def state_log
timeboxed_state.merge(all_time_state)
def write_as_log
Rails.logger.info('Form 526 State Data',
state_log: counts_with_failures,
start_date:,
end_date:)
end

def counts_with_failures
counts = state_as_counts
counts[:total_failure_type_ids] = total_failure_type
counts
end

def base_state
@base_state ||= timeboxed_state.merge(all_time_state)
end

def state_as_counts
@state_as_counts ||= {}.tap do |abbreviation|
base_state.each do |dp, ids|
abbreviation[:"#{dp}_count"] = ids.count
end
end

Check failure on line 52 in app/sidekiq/form526_state_logging_job.rb

View workflow job for this annotation

GitHub Actions / Linting and Security

Layout/BlockAlignment: `end` at 52, 25 is not aligned with `@state_as_counts ||= {}.tap do |abbreviation|` at 48, 4.
end

def abbreviated_state_log
{}.tap do |abbreviation|
state_log.each do |dp, ids|
abbreviation[:"#{dp}_count"] = ids.count
end
abbreviation[:total_failure_type_ids] = total_failure_type
def write_as_gauges
state_as_counts.each do |description, count|
StatsD.gauge("#{STATSD_PREFIX}.#{description}", count)
end
end

def timeboxed_state
@timeboxed_state ||= load_timeboxed_state
end

def all_time_state
@all_time_state ||= load_all_time_state
end

def load_timeboxed_state
{
timeboxed: timeboxed_submissions.pluck(:id).sort,
timeboxed_primary_successes: timeboxed_submissions.accepted_to_primary_path.pluck(:id).sort,
Expand All @@ -51,7 +76,7 @@ def timeboxed_state
}
end

def all_time_state
def load_all_time_state
{
total_awaiting_backup_status: Form526Submission.pending_backup.pluck(:id).sort,
total_incomplete_type: Form526Submission.incomplete_type.pluck(:id).sort,
Expand Down
18 changes: 17 additions & 1 deletion spec/sidekiq/form526_state_logging_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@
].sort
}

expect(described_class.new.state_log).to eq(expected_log)
[wipn8923]
expect(described_class.new.base_state).to eq(expected_log)
end

it 'converts the logs to counts where prefered' do
Expand Down Expand Up @@ -304,5 +305,20 @@
end
described_class.new.perform
end

it 'writes counts as Stats D gauges' do
prefix = described_class::STATSD_PREFIX

expect(StatsD).to receive(:gauge).with("#{prefix}.timeboxed_count", 17)
expect(StatsD).to receive(:gauge).with("#{prefix}.timeboxed_primary_successes_count", 4)
expect(StatsD).to receive(:gauge).with("#{prefix}.timeboxed_exhausted_primary_job_count", 8)
expect(StatsD).to receive(:gauge).with("#{prefix}.timeboxed_exhausted_backup_job_count", 3)
expect(StatsD).to receive(:gauge).with("#{prefix}.timeboxed_incomplete_type_count", 5)
expect(StatsD).to receive(:gauge).with("#{prefix}.total_awaiting_backup_status_count", 1)
expect(StatsD).to receive(:gauge).with("#{prefix}.total_incomplete_type_count", 5)
expect(StatsD).to receive(:gauge).with("#{prefix}.total_failure_type_count", 10)

described_class.new.perform
end
end
end

0 comments on commit 1edfa12

Please sign in to comment.