Class: Appsignal::Minutely::ProbeCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/minutely.rb

Instance Method Summary collapse

Constructor Details

#initializeProbeCollection

Returns a new instance of ProbeCollection.



6
7
8
# File 'lib/appsignal/minutely.rb', line 6

def initialize
  @probes = {}
end

Instance Method Details

#[](key) ⇒ Object

Fetch a probe using its name.

Parameters:

  • key (Symbol/String)

    The name of the probe to fetch.

Returns:

  • (Object)

    Returns the registered probe.



24
25
26
# File 'lib/appsignal/minutely.rb', line 24

def [](key)
  probes[key]
end

#clearvoid

This method returns an undefined value.

Clears all probes from the list.



17
18
19
# File 'lib/appsignal/minutely.rb', line 17

def clear
  probes.clear
end

#countInteger

Returns Number of probes that are registered.

Returns:

  • (Integer)

    Number of probes that are registered.



11
12
13
# File 'lib/appsignal/minutely.rb', line 11

def count
  probes.count
end

#each(&block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



102
103
104
# File 'lib/appsignal/minutely.rb', line 102

def each(&block)
  probes.each(&block)
end

#register(name, probe) ⇒ void

This method returns an undefined value.

Register a new minutely probe.

Supported probe types are:

  • Lambda - A lambda is an object that listens to a call method call. This call method is called every minute.
  • Class - A class object is an object that listens to a new and call method call. The new method is called when the Minutely probe thread is started to initialize all probes. This allows probes to load dependencies once beforehand. Their call method is called every minute.
  • Class instance - A class instance object is an object that listens to a call method call. The call method is called every minute.

Examples:

Register a new probe

Appsignal::Minutely.probes.register :my_probe, lambda {}

Overwrite an existing registered probe

Appsignal::Minutely.probes.register :my_probe, lambda {}
Appsignal::Minutely.probes.register :my_probe, lambda { puts "hello" }

Add a lambda as a probe

Appsignal::Minutely.probes.register :my_probe, lambda { puts "hello" }
# "hello" # printed every minute

Add a probe instance

class MyProbe
  def initialize
    puts "started"
  end

  def call
    puts "called"
  end
end

Appsignal::Minutely.probes.register :my_probe, MyProbe.new
# "started" # printed immediately
# "called" # printed every minute

Add a probe class

class MyProbe
  def initialize
    # Add things that only need to be done on start up for this probe
    require "some/library/dependency"
    @cache = {} # initialize a local cache variable
    puts "started"
  end

  def call
    puts "called"
  end
end

Appsignal::Minutely.probes.register :my_probe, MyProbe
Appsignal::Minutely.start # This is called for you
# "started" # Printed on Appsignal::Minutely.start
# "called" # Repeated every minute

Parameters:

  • name (Symbol/String)

    Name of the probe. Can be used with #[]. This name will be used in errors in the log and allows overwriting of probes by registering new ones with the same name.

  • probe (Object)

    Any object that listens to the call method will be used as a probe.



93
94
95
96
97
98
99
# File 'lib/appsignal/minutely.rb', line 93

def register(name, probe)
  if probes.key?(name)
    logger.debug "A probe with the name `#{name}` is already " \
      "registered. Overwriting the entry with the new probe."
  end
  probes[name] = probe
end