Class: Julewire::Core::Diagnostics::Health

Inherits:
Object
  • Object
show all
Defined in:
lib/julewire/core/diagnostics/health.rb

Instance Method Summary collapse

Constructor Details

#initialize(counter_keys:, callback_failure_counter: nil, callback_metadata: {}, failure_counter: nil, track_failures: true) ⇒ Health

Returns a new instance of Health.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/julewire/core/diagnostics/health.rb', line 10

def initialize(
  counter_keys:,
  callback_failure_counter: nil,
  callback_metadata: {},
  failure_counter: nil,
  track_failures: true
)
  @callback_failure_counter = callback_failure_counter
  @callback_metadata = 
  @failure_counter = failure_counter
  @track_failures = track_failures
  counter_keys = counter_keys.map { it }
  counter_keys = counter_keys.union([:failures]) if @track_failures
  @counts = counter_keys.to_h { [it, Concurrent::AtomicFixnum.new] }
  @current_degradation = Concurrent::AtomicReference.new
  @last_callback_failure = Concurrent::AtomicReference.new
  @last_failure = Concurrent::AtomicReference.new
  @last_loss = Concurrent::AtomicReference.new
end

Instance Method Details

#clear_degradation_if_unchanged(marker) ⇒ Object



58
59
60
# File 'lib/julewire/core/diagnostics/health.rb', line 58

def clear_degradation_if_unchanged(marker)
  @current_degradation.compare_and_set(marker, nil)
end

#clear_failures!Object



62
63
64
65
66
67
68
# File 'lib/julewire/core/diagnostics/health.rb', line 62

def clear_failures!
  @current_degradation.set(nil)
  @last_callback_failure.set(nil)
  @last_failure.set(nil)
  @last_loss.set(nil)
  self
end

#countsObject



34
35
36
# File 'lib/julewire/core/diagnostics/health.rb', line 34

def counts
  @counts.to_h { |key, counter| [key, counter.value] }.freeze
end

#degradation_markerObject



38
39
40
# File 'lib/julewire/core/diagnostics/health.rb', line 38

def degradation_marker
  @current_degradation.get
end

#degraded?(status_from: :current) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/julewire/core/diagnostics/health.rb', line 42

def degraded?(status_from: :current)
  degraded_from?(status_from)
end

#increment(key, by: 1) ⇒ Object



30
31
32
# File 'lib/julewire/core/diagnostics/health.rb', line 30

def increment(key, by: 1)
  @counts.fetch(key).increment(by)
end

#last_callback_failureObject



46
47
48
# File 'lib/julewire/core/diagnostics/health.rb', line 46

def last_callback_failure
  @last_callback_failure.get
end

#last_failureObject



50
51
52
# File 'lib/julewire/core/diagnostics/health.rb', line 50

def last_failure
  @last_failure.get
end

#last_lossObject



54
55
56
# File 'lib/julewire/core/diagnostics/health.rb', line 54

def last_loss
  @last_loss.get
end

#record_callback_failure(callback_failure) ⇒ Object



80
81
82
83
# File 'lib/julewire/core/diagnostics/health.rb', line 80

def record_callback_failure(callback_failure)
  @last_callback_failure.set(callback_failure.to_h)
  increment(@callback_failure_counter) if @callback_failure_counter
end

#record_failure(error, callback: nil, counter: @failure_counter, degrade: true, **metadata) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/julewire/core/diagnostics/health.rb', line 70

def record_failure(error, callback: nil, counter: @failure_counter, degrade: true, **)
  failure = FailureSnapshot.build(error, **)
  increment(:failures) if @track_failures
  increment(counter) if counter && !counter.equal?(:failures) && @counts.key?(counter)
  @last_failure.set(failure)
  @current_degradation.set(failure) if degrade
  notify_failure_callback(callback, error, )
  failure
end

#record_loss(reason:, counter: reason, degrade: true, **metadata) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/julewire/core/diagnostics/health.rb', line 85

def record_loss(reason:, counter: reason, degrade: true, **)
  loss = { reason: reason }.merge().compact.freeze
  increment(counter) if counter && @counts.key?(counter)
  @last_loss.set(loss)
  @current_degradation.set(loss) if degrade
  loss
end

#record_successObject



93
94
95
96
# File 'lib/julewire/core/diagnostics/health.rb', line 93

def record_success
  @current_degradation.set(nil)
  self
end

#snapshot(status: nil, status_from: :current, include_loss: false, **fields) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/julewire/core/diagnostics/health.rb', line 98

def snapshot(status: nil, status_from: :current, include_loss: false, **fields)
  result = {
    counts: counts,
    last_failure: last_failure,
    status: status || (degraded_from?(status_from) ? :degraded : :ok)
  }
  result[:last_loss] = last_loss if include_loss
  result.merge(fields).compact.freeze
end