class PoiseProfiler::Base

Base class for poise-provider event handlers.

@api private @since 1.1.0

Attributes

events[W]

Used in {#_monkey_patch_old_chef}.

@api private

monkey_patched[W]

Used in {#_monkey_patch_old_chef}.

@api private

Public Class Methods

install!() click to toggle source

Install this event handler in to Chef.

@return [void]

# File lib/poise_profiler/base.rb, line 39
def self.install!
  # Clear instance state to be safe.
  instance.reset!
  # For pre-Chef.run_context, use the monkey patch. Otherwise use the
  # events API or global config.
  if Gem::Version.create(Chef::VERSION) <= Gem::Version.create('12.2.1')
    Chef::Log.debug("Registering poise-profiler handler #{self} using monkey patch")
    instance._monkey_patch_old_chef!
  elsif Chef.run_context && Chef.run_context.events
    # :nocov:
    Chef::Log.debug("Registering poise-profiler handler #{self} using events API")
    Chef.run_context.events.register(instance)
    # :nocov:
  else
    Chef::Log.debug("Registering poise-profiler handler #{self} using global config")
    Chef::Config[:event_handlers] |= [instance]
  end
end

Public Instance Methods

_monkey_patch_old_chef!() click to toggle source

Inject this instance for Chef < 12.3. Don't call this on newer Chef.

@api private @see Base.install @return [void]

# File lib/poise_profiler/base.rb, line 76
def _monkey_patch_old_chef!
  require 'chef/event_dispatch/dispatcher'
  instance = self
  orig_method = Chef::EventDispatch::Dispatcher.instance_method(:library_file_loaded)
  Chef::EventDispatch::Dispatcher.send(:define_method, :library_file_loaded) do |filename|
    instance.events = self
    instance.monkey_patched = false
    @subscribers |= [instance]
    Chef::EventDispatch::Dispatcher.send(:define_method, :library_file_loaded, orig_method)
    orig_method.bind(self).call(filename)
  end
end
reset!() click to toggle source

Hook to reset the handler for testing.

@api private @return [void]

# File lib/poise_profiler/base.rb, line 67
def reset!
  @events = nil
end

Private Instance Methods

config() click to toggle source

Accessor for the profiler config.

@api private @return [PoiseProfiler::Config]

# File lib/poise_profiler/base.rb, line 114
def config
  # This could be a single global but it doesn't use enough RAM or CPU
  # cycles that I care.
  @config ||= PoiseProfiler::Config.new
end
events() click to toggle source

Accessor for the current global event handler. The is either set via {#_monkey_patch_old_chef} (<= 12.2.1) or retrieved via the global API (>= 12.3).

@api private return [Chef::EventDispatch::Dispatcher]

# File lib/poise_profiler/base.rb, line 106
def events
  @events ||= Chef.run_context.events
end
puts(line) click to toggle source

Convenience helper to print a line of text out via the event handler.

@api private @param line [String] Line to display. @return [void]

# File lib/poise_profiler/base.rb, line 96
def puts(line)
  events.stream_output(:profiler, line+"\n")
end