Class: Riemann::AutoState

Inherits:
Object
  • Object
show all
Defined in:
lib/riemann/auto_state.rb

Instance Method Summary collapse

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


44
45
46
47
# File 'lib/riemann/auto_state.rb', line 44

def initialize(client = Client.new, state = {})
  @client = client
  @state = state
end

Instance Method Details

#descriptionObject



54
55
56
# File 'lib/riemann/auto_state.rb', line 54

def description
  @state[:description]
end

#description=(description) ⇒ Object



49
50
51
52
# File 'lib/riemann/auto_state.rb', line 49

def description=(description)
  @state[:description] = description
  flush
end

#flushObject

Send state to client



59
60
61
62
# File 'lib/riemann/auto_state.rb', line 59

def flush
  @state[:time] = Time.now.to_i
  @client << @state
end

#hostObject



69
70
71
# File 'lib/riemann/auto_state.rb', line 69

def host
  @state[:host]
end

#host=(host) ⇒ Object



64
65
66
67
# File 'lib/riemann/auto_state.rb', line 64

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



86
87
88
89
# File 'lib/riemann/auto_state.rb', line 86

def merge(opts)
  @state.merge! opts
  flush
end

#metricObject Also known as: metric_f



79
80
81
# File 'lib/riemann/auto_state.rb', line 79

def metric
  @state[:metric]
end

#metric=(metric) ⇒ Object Also known as: metric_f=



73
74
75
76
# File 'lib/riemann/auto_state.rb', line 73

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.



95
96
97
98
99
100
# File 'lib/riemann/auto_state.rb', line 95

def once(opts)
  o = @state.merge opts
  o[:time] = Time.now.to_i
  o[:tags] = ((o[:tags] | ["once"]) rescue ["once"])
  @client << o
end

#serviceObject



116
117
118
# File 'lib/riemann/auto_state.rb', line 116

def service
  @state[:service]
end

#service=(service) ⇒ Object



111
112
113
114
# File 'lib/riemann/auto_state.rb', line 111

def service=(service)
  @state[:service] = service
  flush
end

#stateObject



107
108
109
# File 'lib/riemann/auto_state.rb', line 107

def state
  @state[:state]
end

#state=(state) ⇒ Object



102
103
104
105
# File 'lib/riemann/auto_state.rb', line 102

def state=(state)
  @state[:state] = state
  flush
end