Module: BreakerMachines::Circuit::Introspection

Extended by:
ActiveSupport::Concern
Included in:
BreakerMachines::Circuit
Defined in:
lib/breaker_machines/circuit/introspection.rb

Overview

Introspection provides methods for inspecting circuit state, statistics, and generating human-readable summaries of circuit status.

Instance Method Summary collapse

Instance Method Details

#configurationObject



26
27
28
# File 'lib/breaker_machines/circuit/introspection.rb', line 26

def configuration
  @config.dup
end

#event_log(limit: 20) ⇒ Object



30
31
32
# File 'lib/breaker_machines/circuit/introspection.rb', line 30

def event_log(limit: 20)
  @storage.event_log(@name, limit) if @storage.respond_to?(:event_log)
end

#last_errorObject



34
35
36
# File 'lib/breaker_machines/circuit/introspection.rb', line 34

def last_error
  @last_error.value
end

#last_error_infoObject



65
66
67
68
69
70
71
72
73
74
# File 'lib/breaker_machines/circuit/introspection.rb', line 65

def last_error_info
  error = @last_error.value
  return nil unless error

  {
    class: error.class.name,
    message: error.message,
    occurred_at: @last_failure_at.value
  }
end

#statsObject

State check methods are automatically generated by state_machines:

  • open? (returns true when status == :open)

  • closed? (returns true when status == :closed)

  • half_open? (returns true when status == :half_open)



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/breaker_machines/circuit/introspection.rb', line 14

def stats
  {
    state: status_name,
    failure_count: @storage.failure_count(@name, @config[:failure_window]),
    success_count: @storage.success_count(@name, @config[:failure_window]),
    last_failure_at: @last_failure_at.value,
    opened_at: @opened_at.value,
    half_open_attempts: @half_open_attempts.value,
    half_open_successes: @half_open_successes.value
  }
end

#summaryObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/breaker_machines/circuit/introspection.rb', line 49

def summary
  case status_name
  when :closed
    "Circuit '#{@name}' is CLOSED. #{stats[:failure_count]} failures recorded."
  when :open
    reset_time = Time.at(@opened_at.value + @config[:reset_timeout])
    opened_time = Time.at(@opened_at.value)
    error_info = @last_error.value ? " The last error was #{@last_error.value.class}." : ''
    "Circuit '#{@name}' is OPEN until #{reset_time}. " \
      "It opened at #{opened_time} after #{@config[:failure_threshold]} failures.#{error_info}"
  when :half_open
    "Circuit '#{@name}' is HALF-OPEN. Testing with limited requests " \
    "(#{@half_open_attempts.value}/#{@config[:half_open_calls]} attempts)."
  end
end

#to_hObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/breaker_machines/circuit/introspection.rb', line 38

def to_h
  {
    name: @name,
    state: status_name,
    stats: stats,
    config: configuration.except(:owner, :storage, :metrics),
    event_log: event_log || [],
    last_error: last_error_info
  }
end