Class: StandardCircuit::Config

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

Defined Under Namespace

Classes: CircuitSpec

Constant Summary collapse

DEFAULT_THRESHOLD =
3
DEFAULT_COOL_OFF =
30
DEFAULT_WINDOW =
60
DEFAULT_CRITICALITY =
:standard
CRITICALITIES =
[ :critical, :standard, :optional ].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



39
40
41
42
43
44
45
46
47
# File 'lib/standard_circuit/config.rb', line 39

def initialize
  @sentry_enabled = true
  @metric_prefix = "external"
  @data_store = Stoplight::DataStore::Memory.new
  @logger = nil
  @circuits = {}
  @prefixes = {}
  @extra_notifiers = []
end

Instance Attribute Details

#circuitsObject (readonly)

Returns the value of attribute circuits.



37
38
39
# File 'lib/standard_circuit/config.rb', line 37

def circuits
  @circuits
end

#data_storeObject

Returns the value of attribute data_store.



36
37
38
# File 'lib/standard_circuit/config.rb', line 36

def data_store
  @data_store
end

#extra_notifiersObject (readonly)

Returns the value of attribute extra_notifiers.



37
38
39
# File 'lib/standard_circuit/config.rb', line 37

def extra_notifiers
  @extra_notifiers
end

#loggerObject

Returns the value of attribute logger.



36
37
38
# File 'lib/standard_circuit/config.rb', line 36

def logger
  @logger
end

#metric_prefixObject

Returns the value of attribute metric_prefix.



36
37
38
# File 'lib/standard_circuit/config.rb', line 36

def metric_prefix
  @metric_prefix
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



37
38
39
# File 'lib/standard_circuit/config.rb', line 37

def prefixes
  @prefixes
end

#sentry_enabledObject

Returns the value of attribute sentry_enabled.



36
37
38
# File 'lib/standard_circuit/config.rb', line 36

def sentry_enabled
  @sentry_enabled
end

Instance Method Details

#add_notifier(notifier) ⇒ Object

Register a host-supplied subscriber. Subscribers must respond to ‘call(event_name, payload)` — Stoplight-shaped 4-arg notifiers from the 0.1.x API are no longer accepted as extras (Logger / Sentry / Metrics demonstrate the new shape).



79
80
81
82
83
84
85
# File 'lib/standard_circuit/config.rb', line 79

def add_notifier(notifier)
  unless notifier.respond_to?(:call)
    raise ArgumentError,
      "extra notifiers must respond to `call(event_name, payload)`; got #{notifier.class}"
  end
  @extra_notifiers << notifier
end

#register(name, **opts) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/standard_circuit/config.rb', line 55

def register(name, **opts)
  spec = CircuitSpec.build(**opts)
  @circuits[name.to_sym] = spec
  EventEmitter.emit("standard_circuit.circuit.registered",
    circuit: name.to_s,
    criticality: spec.criticality,
    scope: :name)
  spec
end

#register_prefix(prefix, **opts) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/standard_circuit/config.rb', line 65

def register_prefix(prefix, **opts)
  spec = CircuitSpec.build(**opts)
  @prefixes[prefix.to_s] = spec
  EventEmitter.emit("standard_circuit.circuit.registered",
    circuit: prefix.to_s,
    criticality: spec.criticality,
    scope: :prefix)
  spec
end

#reset_registry!Object



49
50
51
52
53
# File 'lib/standard_circuit/config.rb', line 49

def reset_registry!
  @circuits.clear
  @prefixes.clear
  @extra_notifiers.clear
end

#spec_for(name) ⇒ Object



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

def spec_for(name)
  @circuits[name.to_sym] || spec_for_prefix(name)
end