class BloodContracts::Instrumentation::Config

Class that configures the instrumentation for refinement types matching

Attributes

finalizer_pool_size[R]

Pool size of finalizer instance

@return [Integer]

instruments[R]

Map of instrument classes where the key is the matching pattern

@return [Hash<Regexp, Array<Instrument>>]

session_finalizer[R]

Type of finalizer instance

@return [Symbol]

types[R]

List of refinement types defined in the app

@return [Array<BC::Refined>]

Public Class Methods

new() click to toggle source

Initialize the config with default values Also inits the SessionFinalizer

# File lib/blood_contracts/instrumentation/config.rb, line 35
def initialize
  @instruments = {}
  @finalizer_pool_size = SessionFinalizer::DEFAULT_POOL_SIZE
  @session_finalizer = :basic

  reset_types!
  reset_session_finalizer!
end

Public Instance Methods

finalizer_pool_size=(value) click to toggle source

Set the pool size for SessionFinalizer (make sense only for SessionFinalizer::Fibers right now)

@param value [Integer] size of finalizer fibers pool @return [Integer]

# File lib/blood_contracts/instrumentation/config.rb, line 50
def finalizer_pool_size=(value)
  @finalizer_pool_size = Integer(value)
ensure
  reset_session_finalizer!
end
instrument(pattern, processor, **kwargs) click to toggle source

Main setting in the config Define an instument for Refinement Types, applies only to types matched by the pattern by type class name

@param pattern [String, Regexp] defines which types to apply this

instrument

@param processor [Proc, call] defines the processor for insturmentation

session, during the finalize phase the processor#call method would be
called with Session instance as a parameter

@option before [Proc, call] defines a method that is called right

after Session#start inside matching process of the refinement type

@option after [Proc, call] defines a method that is called right

after Session#finish inside matching process of the refinement type

@return [Nothing]

# File lib/blood_contracts/instrumentation/config.rb, line 87
def instrument(pattern, processor, **kwargs)
  pattern = /#{pattern}/i unless pattern.is_a?(Regexp)

  old_value = @instruments[pattern].to_a
  new_value = old_value << Instrument.build(processor, **kwargs)
  @instruments[pattern] = new_value

  reset_cache!(pattern)
end
session_finalizer=(value) click to toggle source

Set the type of SessionFinalizer for instrumentation See SessionFinalizer::Basic, SessionFinalizer::Threads and SessionFinalizer::Fibers

@param value [Symbol] name of SessionFinalizer, could be one of

:basic, :threads, :fibers

@return [Symbol]

# File lib/blood_contracts/instrumentation/config.rb, line 64
def session_finalizer=(value)
  @session_finalizer = value
ensure
  reset_session_finalizer!
end

Protected Instance Methods

reset_cache!(filter = nil) click to toggle source

@protected Reset instruments cache for refinement types that match filter

# File lib/blood_contracts/instrumentation/config.rb, line 108
          def reset_cache!(filter = nil)
  filter = /#{filter}/i unless filter.is_a?(Regexp)

  types.each { |type| type.reset_instruments! if type.name =~ filter }
end
reset_session_finalizer!() click to toggle source

@protected Reset session finalizer instance using current config

# File lib/blood_contracts/instrumentation/config.rb, line 121
          def reset_session_finalizer!
  SessionFinalizer.init(session_finalizer, pool_size: finalizer_pool_size)
end
reset_types!() click to toggle source

@protected

# File lib/blood_contracts/instrumentation/config.rb, line 115
          def reset_types!
  @types = Set.new
end
select_instruments(type_name) click to toggle source

@protected Select only instruments matching the type_name

# File lib/blood_contracts/instrumentation/config.rb, line 99
          def select_instruments(type_name)
  @instruments.flat_map do |pattern, instruments|
    next unless type_name =~ /#{pattern}/i
    instruments
  end.compact
end