class Riemann::AutoState

Public Class Methods

new(client = Client.new, state = {}) click to toggle source

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
# File lib/riemann/auto_state.rb, line 46
def initialize(client = Client.new, state = {})
  @client = client
  @state = state
end

Public Instance Methods

<<(opts)
Alias for: merge
description() click to toggle source
# File lib/riemann/auto_state.rb, line 56
def description
  @state[:description]
end
description=(description) click to toggle source
# File lib/riemann/auto_state.rb, line 51
def description=(description)
  @state[:description] = description
  flush
end
flush() click to toggle source

Send state to client

# File lib/riemann/auto_state.rb, line 61
def flush
  @state[:time] = Time.now.to_i
  @client << @state
end
host() click to toggle source
# File lib/riemann/auto_state.rb, line 71
def host
  @state[:host]
end
host=(host) click to toggle source
# File lib/riemann/auto_state.rb, line 66
def host=(host)
  @state[:host] = host
  flush
end
merge(opts) click to toggle source

Performs multiple updates, followed by flush. Example: merge state: critical, metric_f: 10235.3

# File lib/riemann/auto_state.rb, line 88
def merge(opts)
  @state.merge! opts
  flush
end
Also aliased as: <<
metric() click to toggle source
# File lib/riemann/auto_state.rb, line 81
def metric
  @state[:metric]
end
Also aliased as: metric_f
metric=(metric) click to toggle source
# File lib/riemann/auto_state.rb, line 75
def metric=(metric)
  @state[:metric] = metric
  flush
end
Also aliased as: metric_f=
metric_f()
Alias for: metric
metric_f=(metric)
Alias for: metric=
once(opts) click to toggle source

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.

# 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() click to toggle source
# File lib/riemann/auto_state.rb, line 122
def service
  @state[:service]
end
service=(service) click to toggle source
# File lib/riemann/auto_state.rb, line 117
def service=(service)
  @state[:service] = service
  flush
end
state() click to toggle source
# File lib/riemann/auto_state.rb, line 113
def state
  @state[:state]
end
state=(state) click to toggle source
# File lib/riemann/auto_state.rb, line 108
def state=(state)
  @state[:state] = state
  flush
end