Class: Riemann::MetricThread

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

Constant Summary collapse

INTERVAL =

A metric thread is simple: it wraps some metric object which responds to <<, and every interval seconds, calls #flush which replaces the object and calls a user specified function.

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, *klass_args, &block) ⇒ MetricThread

client = Riemann::Client.new m = MetricThread.new Mtrc::Rate do |rate|

client << rate

end

loop do

sleep rand
m << rand

end



22
23
24
25
26
27
28
29
30
31
# File 'lib/riemann/metric_thread.rb', line 22

def initialize(klass, *klass_args, &block)
  @klass = klass
  @klass_args = klass_args
  @block = block
  @interval = INTERVAL

  @metric = new_metric

  start
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



11
12
13
# File 'lib/riemann/metric_thread.rb', line 11

def interval
  @interval
end

#metricObject

Returns the value of attribute metric.



11
12
13
# File 'lib/riemann/metric_thread.rb', line 11

def metric
  @metric
end

Instance Method Details

#<<(value) ⇒ Object



33
34
35
# File 'lib/riemann/metric_thread.rb', line 33

def <<(value)
  @metric.<<(value)
end

#flushObject



41
42
43
44
45
# File 'lib/riemann/metric_thread.rb', line 41

def flush
  old = @metric
  @metric = new_metric
  @block[old]
end

#new_metricObject



37
38
39
# File 'lib/riemann/metric_thread.rb', line 37

def new_metric
  @klass.new(*@klass_args)
end

#startObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/riemann/metric_thread.rb', line 47

def start
  raise 'already running' if @runner

  @running = true
  @runner = Thread.new do
    while @running
      sleep @interval
      begin
        flush
      rescue StandardError
        # ignore
      end
    end
    @runner = nil
  end
end

#stopObject



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

def stop
  stop!
  @runner.join
end

#stop!Object



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

def stop!
  @running = false
end