Class: Riemann::AutoState
- Inherits:
-
Object
- Object
- Riemann::AutoState
- Defined in:
- lib/riemann/auto_state.rb
Instance Method Summary collapse
- #description ⇒ Object
- #description=(description) ⇒ Object
-
#flush ⇒ Object
Send state to client.
- #host ⇒ Object
- #host=(host) ⇒ Object
-
#initialize(client = Client.new, state = {}) ⇒ AutoState
constructor
Binds together a state hash and a Client.
-
#merge(opts) ⇒ Object
(also: #<<)
Performs multiple updates, followed by flush.
- #metric ⇒ Object (also: #metric_f)
- #metric=(metric) ⇒ Object (also: #metric_f=)
-
#once(opts) ⇒ Object
Issues an immediate update of the state with tag “once” set, but does not update the local state.
- #service ⇒ Object
- #service=(service) ⇒ Object
- #state ⇒ Object
- #state=(state) ⇒ Object
Constructor Details
#initialize(client = Client.new, state = {}) ⇒ AutoState
Binds together a state hash and a Client. Any change made here sends the state to the client. Useful when updates to a state are made decoherently, e.g. across many methods. Combine with MetricThread (or just Thread.new { loop { autostate.flush; sleep n } }) to ensure regular updates.
example:
class Job
def initialize
@state = AutoState.new
@state.service = 'job'
@state.state = 'starting up'
run
end
def run
loop do
begin
a
b
rescue Exception => e
@state.once(
state: 'error',
description: e.to_s
)
end
end
end
def a
@state.state = 'heavy lifting a'
...
end
def b
@state.state = 'heavy lifting b'
...
end
46 47 48 49 |
# File 'lib/riemann/auto_state.rb', line 46 def initialize(client = Client.new, state = {}) @client = client @state = state end |
Instance Method Details
#description ⇒ Object
56 57 58 |
# File 'lib/riemann/auto_state.rb', line 56 def description @state[:description] end |
#description=(description) ⇒ Object
51 52 53 54 |
# File 'lib/riemann/auto_state.rb', line 51 def description=(description) @state[:description] = description flush end |
#flush ⇒ Object
Send state to client
61 62 63 64 |
# File 'lib/riemann/auto_state.rb', line 61 def flush @state[:time] = Time.now.to_i @client << @state end |
#host ⇒ Object
71 72 73 |
# File 'lib/riemann/auto_state.rb', line 71 def host @state[:host] end |
#host=(host) ⇒ Object
66 67 68 69 |
# File 'lib/riemann/auto_state.rb', line 66 def host=(host) @state[:host] = host flush end |
#merge(opts) ⇒ Object Also known as: <<
Performs multiple updates, followed by flush. Example: merge state: critical, metric_f: 10235.3
88 89 90 91 |
# File 'lib/riemann/auto_state.rb', line 88 def merge(opts) @state.merge! opts flush end |
#metric ⇒ Object Also known as: metric_f
81 82 83 |
# File 'lib/riemann/auto_state.rb', line 81 def metric @state[:metric] end |
#metric=(metric) ⇒ Object Also known as: metric_f=
75 76 77 78 |
# File 'lib/riemann/auto_state.rb', line 75 def metric=(metric) @state[:metric] = metric flush end |
#once(opts) ⇒ Object
Issues an immediate update of the state with tag “once” set, but does not update the local state. Useful for transient errors. Opts are merged with the state.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/riemann/auto_state.rb', line 97 def once(opts) o = @state.merge opts o[:time] = Time.now.to_i o[:tags] = begin (o[:tags] | ['once']) rescue StandardError ['once'] end @client << o end |
#service ⇒ Object
122 123 124 |
# File 'lib/riemann/auto_state.rb', line 122 def service @state[:service] end |
#service=(service) ⇒ Object
117 118 119 120 |
# File 'lib/riemann/auto_state.rb', line 117 def service=(service) @state[:service] = service flush end |
#state ⇒ Object
113 114 115 |
# File 'lib/riemann/auto_state.rb', line 113 def state @state[:state] end |
#state=(state) ⇒ Object
108 109 110 111 |
# File 'lib/riemann/auto_state.rb', line 108 def state=(state) @state[:state] = state flush end |