Class: Appsignal::Minutely::ProbeCollection

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

Instance Method Summary collapse

Methods included from Utils::DeprecationMessage

#deprecation_message

Constructor Details

#initializeProbeCollection

Returns a new instance of ProbeCollection.



8
9
10
# File 'lib/appsignal/minutely.rb', line 8

def initialize
  @probes = {}
end

Instance Method Details

#<<(probe) ⇒ void

Deprecated.

Use #register instead.

This method returns an undefined value.

Parameters:

  • probe (Object)

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



34
35
36
37
38
39
# File 'lib/appsignal/minutely.rb', line 34

def <<(probe)
  deprecation_message "Deprecated `Appsignal::Minute.probes <<` " \
    "call. Please use `Appsignal::Minutely.probes.register` instead.",
    logger
  register probe.object_id, probe
end

#[](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.



26
27
28
# File 'lib/appsignal/minutely.rb', line 26

def [](key)
  probes[key]
end

#clearvoid

This method returns an undefined value.

Clears all probes from the list.



19
20
21
# File 'lib/appsignal/minutely.rb', line 19

def clear
  probes.clear
end

#countInteger

Returns Number of probes that are registered.

Returns:

  • (Integer)

    Number of probes that are registered.



13
14
15
# File 'lib/appsignal/minutely.rb', line 13

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.



115
116
117
# File 'lib/appsignal/minutely.rb', line 115

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.



106
107
108
109
110
111
112
# File 'lib/appsignal/minutely.rb', line 106

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