Module: Appsignal::Probes

Defined in:
lib/appsignal/probes.rb,
lib/appsignal/probes/gvl.rb,
lib/appsignal/probes/mri.rb,
lib/appsignal/probes/helpers.rb,
lib/appsignal/probes/sidekiq.rb

Defined Under Namespace

Modules: Helpers Classes: GvlProbe, MriProbe, ProbeCollection, SidekiqProbe

Class Method Summary collapse

Class Method Details

.mutexObject

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.



67
68
69
# File 'lib/appsignal/probes.rb', line 67

def mutex
  @mutex ||= Thread::Mutex.new
end

.probesProbeCollection

Returns list of probes.

Returns:

See Also:



73
74
75
# File 'lib/appsignal/probes.rb', line 73

def probes
  @probes ||= ProbeCollection.new
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::Probes.register :my_probe, lambda {}

Overwrite an existing registered probe

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

Add a lambda as a probe

Appsignal::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::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::Probes.register :my_probe, MyProbe
Appsignal::Probes.start # This is called for you
# "started" # Printed on Appsignal::Probes.start
# "called" # Repeated every minute

Parameters:

  • name (Symbol/String)

    Name of the probe. Can be used with Appsignal::Probes::ProbeCollection#[]. 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.



143
144
145
146
147
# File 'lib/appsignal/probes.rb', line 143

def register(name, probe)
  probes.internal_register(name, probe)

  initialize_probe(name, probe) if started?
end

.startObject

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.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/appsignal/probes.rb', line 170

def start
  stop
  @started = true
  @thread = Thread.new do
    # Advise multi-threaded app servers to ignore this thread
    # for the purposes of fork safety warnings
    if Thread.current.respond_to?(:thread_variable_set)
      Thread.current.thread_variable_set(:fork_safe, true)
    end

    sleep initial_wait_time
    initialize_probes
    loop do
      logger = Appsignal.internal_logger
      mutex.synchronize do
        logger.debug("Gathering minutely metrics with #{probe_instances.count} probes")
        probe_instances.each do |name, probe|
          logger.debug("Gathering minutely metrics with '#{name}' probe")
          probe.call
        rescue => ex
          logger.error "Error in minutely probe '#{name}': #{ex}"
          logger.debug ex.backtrace.join("\n")
        end
      end
      sleep wait_time
    end
  end
end

.started?Boolean?

Returns if the probes thread has been started. If the value is false or nil, it has not been started yet.

Returns:

  • (Boolean, nil)


203
204
205
# File 'lib/appsignal/probes.rb', line 203

def started?
  @started
end

.stopObject

Stop the minutely probes mechanism. Stop the thread and clear all probe instances.



209
210
211
212
213
# File 'lib/appsignal/probes.rb', line 209

def stop
  defined?(@thread) && @thread.kill
  @started = false
  probe_instances.clear
end

.unregister(name) ⇒ void

This method returns an undefined value.

Unregister a probe that's registered with register. Can also be used to unregister automatically registered probes by the gem.

Examples:

Unregister probes

# First register a probe
Appsignal::Probes.register :my_probe, lambda {}

# Then unregister a probe if needed
Appsignal::Probes.unregister :my_probe

Parameters:

  • name (Symbol/String)

    Name of the probe used to register the probe.



163
164
165
166
167
# File 'lib/appsignal/probes.rb', line 163

def unregister(name)
  probes.unregister(name)

  uninitialize_probe(name)
end

.wait_timeObject

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.



216
217
218
# File 'lib/appsignal/probes.rb', line 216

def wait_time
  60 - Time.now.sec
end