Class: SidekiqUtils::LatencyAlert

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_utils/latency_alert.rb

Constant Summary collapse

REDIS_KEY =
"sidekiq_queue_latency_alert"

Class Method Summary collapse

Class Method Details

.check!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/sidekiq_utils/latency_alert.rb', line 8

def check!
  alerts = {}
  Sidekiq::Queue.all.each do |queue|
    threshold = config['alert_thresholds'][queue.name].
      try!(&:to_i).try!(:minutes)
    next if threshold == :disabled
    threshold ||= config['alert_thresholds']['default'].
      try!(&:to_i).try!(:minutes) || 10.minutes
    if (latency = queue.latency) > threshold
      alerts[queue.name] = latency
    end
  end

  if alerts.blank?
    if should_alert_back_to_normal?
      Sidekiq.redis { |r| r.del(REDIS_KEY) }
      slack_alert("All queues under their thresholds.")
    end
    return false
  end

  alert_message = ["Sidekiq queue latency over threshold:"]
  alerts.each do |queue, latency|
    alert_message << "Queue #{queue} is #{formatted_latency(latency)} behind"
  end
  alert_message = alert_message.join("\n")
  if should_alert_again?(alert_message)
    slack_alert(alert_message)
  end

  true
end

.configObject



41
42
43
44
# File 'lib/sidekiq_utils/latency_alert.rb', line 41

def config
  @config ||= (YAML.load(ERB.new(
    File.read('config/sidekiq_utils.yml')).result) || {})
end