A mixin that adds effortless Observability
to your systems.
Version control revision
Package version
Get the observer hooks for the specified mod
.
# File lib/observability.rb, line 41
def self::[]( mod )
mod = mod.class unless mod.is_a?( Module )
return self.observer_hooks[ mod ]
end
Extension callback
# File lib/observability.rb, line 48
def self::extended( mod )
super
Observability.observer_hooks.compute_if_absent( mod ) do
observer_hooks = Observability::ObserverHooks.dup
mod.prepend( observer_hooks )
observer_hooks
end
mod.singleton_class.extend( Observability ) unless mod.singleton_class?
end
Make a body for a wrapping method for the method with the given name
and context
, passing the given options
.
# File lib/observability.rb, line 86
def self::make_wrapped_method( name, context, options, &callback )
return Proc.new do |*m_args, **m_options, &block|
Loggability[ Observability ].debug "Wrapped method %p: %p" %
[ name, context ]
Observability.observer.event( context, **options ) do
# :TODO: Freeze or dup the arguments to prevent accidental modification?
callback.call( *m_args, **m_options, &block ) if callback
super( *m_args, **m_options, &block )
end
end
end
Return the current Observer
, creating it if necessary.
# File lib/observability.rb, line 62
def self::observer
unless @observer.complete?
self.log.debug "Creating the observer agent."
@observer.try_set do
obs = Observability::Observer.new
obs.start
obs
end
end
return @observer.value!
end
Reset all per-process Observability
state. This should be called, for instance, after a fork or between tests.
# File lib/observability.rb, line 78
def self::reset
@observer.value.stop if @observer.complete?
@observer = Concurrent::IVar.new
end
Wrap a class method in an observer call.
# File lib/observability.rb, line 116
def observe_class_method( method_name, *details, **options, &callback )
self.singleton_class.observe_method( method_name, *details, **options, &callback )
end
Wrap an instance method in an observer call.
# File lib/observability.rb, line 103
def observe_method( method_name, *details, **options, &callback )
hooks = Observability.observer_hooks[ self ] or
raise "No observer hooks installed for %p?!" % [ self ]
context = self.instance_method( method_name )
context = [ context, *details ]
method_body = Observability.make_wrapped_method( method_name, context, options, &callback )
hooks.define_method( method_name, &method_body )
end