module Observability::Instrumentation

Utilities for loading and installing pre-packaged instrumentation for common libraries.

Public Class Methods

check_for_module( mod ) click to toggle source

Returns true if mod is defined and is a Module.

# File lib/observability/instrumentation.rb, line 74
def self::check_for_module( mod )
        self.log.debug "Checking for presence of `%s` module..." % [ mod ]
        Object.const_defined?( mod ) && Object.const_get( mod ).is_a?( Module )
end
dependencies_met?( *module_names ) click to toggle source

Returns true if each of the given module_names are defined and are Module objects.

# File lib/observability/instrumentation.rb, line 66
def self::dependencies_met?( *module_names )
        return module_names.flatten.all? do |mod|
                self.check_for_module( mod )
        end
end
extended( mod ) click to toggle source

Extension callback – declare some instance variables in the extending mod.

Calls superclass method
# File lib/observability/instrumentation.rb, line 39
def self::extended( mod )
        super

        if self.modules.add?( mod )
                self.log.info "Loaded %p" % [ mod ]
                mod.extend( Loggability )
                mod.log_to( :observability )
                mod.instance_variable_set( :@depends_on, Set.new )
                mod.instance_variable_set( :@requires, Set.new )
                mod.instance_variable_set( :@installation_callbacks, Set.new )
                mod.singleton_class.attr_reader( :installation_callbacks )
        else
                self.log.warn "Already loaded %p" % [ mod ]
        end
end
install() click to toggle source

Install loaded instrumentation if the requisite modules are present.

# File lib/observability/instrumentation.rb, line 57
def self::install
        self.modules.each do |mod|
                mod.install if mod.available?
        end
end
load( *libraries ) click to toggle source

Load instrumentation for the specified libraries.

# File lib/observability/instrumentation.rb, line 27
def self::load( *libraries )
        libraries.flatten.each do |library|
                libfile = "observability/instrumentation/%s" % [ library ]
                require( libfile )
        end

        return self
end
unknown() click to toggle source

The Set of Instrumentation modules that are laoded

# File lib/observability/instrumentation.rb, line 22
singleton_class.attr_reader :modules

Public Instance Methods

available?() click to toggle source

Returns true if all of the modules registered with depends_on are defined.

# File lib/observability/instrumentation.rb, line 104
def available?
        return Observability::Instrumentation.dependencies_met?( self.depends_on.to_a )
end
depends_on( *modules ) click to toggle source

Declare modules that must be available for instrumentation to be loaded. If they are not present when the instrumentation loads, it will be skipped entirely.

# File lib/observability/instrumentation.rb, line 83
def depends_on( *modules )
        @depends_on.merge( modules.flatten )
        return @depends_on
end
install() click to toggle source

Call installation callbacks which meet their prerequistes.

# File lib/observability/instrumentation.rb, line 110
def install
        self.requires.each do |file|
                require( file )
        end
        self.installation_callbacks.each do |callback, dependencies|
                missing = dependencies.
                        reject {|mod| Observability::Instrumentation.check_for_module(mod) }

                if missing.empty?
                        self.log.debug "Instrumenting %s: %p" % [ dependencies.join(', '), callback ]
                        callback.call
                else
                        self.log.info "Skipping %p: missing %s" % [ callback, missing.join(', ') ]
                end
        end
rescue LoadError => err
        raise "%p while loading instrumentation"
end
requires( *files ) click to toggle source

Specified files to require if this instrumentation is loaded.

# File lib/observability/instrumentation.rb, line 90
def requires( *files )
        @requires.merge( files.flatten )
        return @requires
end
when_installed( *modules, &callback ) click to toggle source

Register a callback that will be called when instrumentation is installed, if and only if all of the given modules are present (may be empty).

# File lib/observability/instrumentation.rb, line 98
def when_installed( *modules, &callback )
        self.installation_callbacks.add( [callback, modules] )
end