Class: Ganymed::Collector::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/ganymed/collector.rb

Overview

Base class and DSL for Ganymed::Collector plugins.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Plugin

Create a new plugin instance.

Parameters:

  • config (Section)

    The configuration object.



101
102
103
# File 'lib/ganymed/collector.rb', line 101

def initialize(config)
  @config = config
end

Instance Attribute Details

#configSection

The configuration object.

Returns:

  • (Section)


88
89
90
# File 'lib/ganymed/collector.rb', line 88

def config
  @config
end

#intervalFixnum, Float

Plugin interval

Returns:

  • (Fixnum, Float)


92
93
94
# File 'lib/ganymed/collector.rb', line 92

def interval
  @interval
end

#splayFixnum (readonly)

Plugin splay

Returns:

  • (Fixnum)


96
97
98
# File 'lib/ganymed/collector.rb', line 96

def splay
  @splay
end

Instance Method Details

#collect(interval = nil, &block) ⇒ void

This method returns an undefined value.

Set the block used to collect metrics with this plugin.

Parameters:

  • interval (Fixnum, Float) (defaults to: nil)

    Custom plugin interval.



109
110
111
112
113
# File 'lib/ganymed/collector.rb', line 109

def collect(interval=nil, &block)
  @interval = interval || config.interval.tap{} || 1
  @splay = 0
  @collector = Proc.new(&block)
end

#from_file(filename) ⇒ Plugin

Loads a given ruby file, and runs instance_eval against it in the context of the current object.

Parameters:

  • filename (String)

    The absolute path to a plugin file.

Returns:

  • (Plugin)

    The instance for easy method chaining.



142
143
144
145
146
147
148
149
# File 'lib/ganymed/collector.rb', line 142

def from_file(filename)
  if File.exists?(filename) && File.readable?(filename)
    self.instance_eval(IO.read(filename), filename, 1)
  else
    raise IOError, "Cannot open or read #{filename}!"
  end
  self
end

#runvoid

This method returns an undefined value.

Start the EventMachine timer to collect metrics at the specified interval.



118
119
120
121
122
123
124
125
# File 'lib/ganymed/collector.rb', line 118

def run
  # we do not use a periodic timer here so that we can simply increase
  # the splay to slow down the collector in case it throws lots of
  # exceptions
  EM.add_timer(interval * (2 ** splay)) do
    EM.defer { collect!; run }
  end
end