Class: SidekiqVigil::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq_vigil/config.rb

Defined Under Namespace

Classes: CheckDefinition, NotifierDefinition

Constant Summary collapse

BUILT_IN_CHECKS =
%i[
  queue_latency queue_size retry_set dead_set process_alive utilization failure_rate stuck_jobs memory redis_health
  scheduled_backlog throughput_anomaly
].freeze
DEFAULT_CHECKS =
(BUILT_IN_CHECKS - [:throughput_anomaly]).freeze
SLACK_MENTION =
/\A(?:<@U[A-Z0-9]+>|<!subteam\^S[A-Z0-9]+>|<!here>)\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment: nil) ⇒ Config

Returns a new instance of Config.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sidekiq_vigil/config.rb', line 41

def initialize(environment: nil)
  @environment = environment || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
  @enabled = true
  @enabled_explicit = false
  @interval = 30
  @flush_interval = 10
  @key_prefix = "default"
  @timezone = "UTC"
  @redis = nil
  @checks = DEFAULT_CHECKS.map { |name| CheckDefinition.new(name:, klass: nil, options: {}) }
  @notifiers = []
  @alerting = AlertingConfig.new
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



39
40
41
# File 'lib/sidekiq_vigil/config.rb', line 39

def checks
  @checks
end

#flush_intervalObject

Returns the value of attribute flush_interval.



38
39
40
# File 'lib/sidekiq_vigil/config.rb', line 38

def flush_interval
  @flush_interval
end

#intervalObject

Returns the value of attribute interval.



38
39
40
# File 'lib/sidekiq_vigil/config.rb', line 38

def interval
  @interval
end

#key_prefixObject

Returns the value of attribute key_prefix.



38
39
40
# File 'lib/sidekiq_vigil/config.rb', line 38

def key_prefix
  @key_prefix
end

#notifiersObject (readonly)

Returns the value of attribute notifiers.



39
40
41
# File 'lib/sidekiq_vigil/config.rb', line 39

def notifiers
  @notifiers
end

#redisObject

Returns the value of attribute redis.



38
39
40
# File 'lib/sidekiq_vigil/config.rb', line 38

def redis
  @redis
end

#timezoneObject

Returns the value of attribute timezone.



38
39
40
# File 'lib/sidekiq_vigil/config.rb', line 38

def timezone
  @timezone
end

Instance Method Details

#alerting {|@alerting| ... } ⇒ Object

Yields:



87
88
89
90
# File 'lib/sidekiq_vigil/config.rb', line 87

def alerting
  yield @alerting if block_given?
  @alerting
end

#check(name_or_class, **options) ⇒ Object



72
73
74
75
76
# File 'lib/sidekiq_vigil/config.rb', line 72

def check(name_or_class, **options)
  name, klass = normalize_check(name_or_class)
  checks.reject! { |definition| definition.name == name }
  checks << CheckDefinition.new(name:, klass:, options:)
end

#digestObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sidekiq_vigil/config.rb', line 106

def digest
  payload = {
    enabled: enabled?,
    interval: interval,
    flush_interval: flush_interval,
    key_prefix: key_prefix,
    timezone: timezone,
    checks: checks.map { |item| [item.name, item.options] },
    notifiers: notifiers.map { |item| [item.name, item.options] },
    alerting: alerting.to_h
  }
  Digest::SHA256.hexdigest(JSON.generate(payload))
end

#disable_check(name) ⇒ Object



78
79
80
# File 'lib/sidekiq_vigil/config.rb', line 78

def disable_check(name)
  checks.reject! { |definition| definition.name == name.to_sym }
end

#enabled=(value) ⇒ Object



55
56
57
58
# File 'lib/sidekiq_vigil/config.rb', line 55

def enabled=(value)
  @enabled_explicit = true
  @enabled = value
end

#enabled?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/sidekiq_vigil/config.rb', line 60

def enabled?
  @enabled == true
end

#external_notifications_enabled?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/sidekiq_vigil/config.rb', line 68

def external_notifications_enabled?
  enabled? && (production? || @enabled_explicit)
end

#notifier(name_or_class, **options) ⇒ Object



82
83
84
85
# File 'lib/sidekiq_vigil/config.rb', line 82

def notifier(name_or_class, **options)
  name, klass = normalize_notifier(name_or_class)
  notifiers << NotifierDefinition.new(name:, klass:, options:)
end

#production?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sidekiq_vigil/config.rb', line 64

def production?
  environment == "production"
end

#runnable_notifiersObject



120
121
122
123
124
125
126
# File 'lib/sidekiq_vigil/config.rb', line 120

def runnable_notifiers
  validate!
  definitions = notifiers.empty? ? [NotifierDefinition.new(name: :log, klass: nil, options: {})] : notifiers
  return definitions if external_notifications_enabled?

  definitions.select { |definition| definition.name == :log }
end

#validate!Object

Raises:



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/sidekiq_vigil/config.rb', line 92

def validate!
  validate_positive!(:interval, interval)
  validate_positive!(:flush_interval, flush_interval)
  raise ConfigError, "key_prefix must not be empty" if key_prefix.to_s.empty?
  raise ConfigError, "key_prefix contains unsupported characters" unless key_prefix.to_s.match?(/\A[\w.-]+\z/)
  raise ConfigError, "timezone must not be empty" if timezone.to_s.empty?

  validate_redis!
  checks.each { |definition| validate_options!(definition.options, "check #{definition.name}") }
  notifiers.each { |definition| validate_notifier!(definition) }
  alerting.validate!
  self
end