class Sqreen::Ecosystem::TracingBroker

Constants

ObserverLookup

Stores a lookup resolution so that lookup (incl. sampling) is done only once, not in the beginning of the producer code (when it asks whether it should proceed) and again once it delivers the data

Attributes

sampling_configuration[W]

@return [Sqreen::Ecosystem::Tracing::SamplingConfiguration]

Public Class Methods

new(tracing_modules) click to toggle source

@param [Array<Sqreen::Ecosystem::ModuleApi::Tracing>] tracing_modules

# File lib/sqreen/ecosystem/tracing_broker.rb, line 24
def initialize(tracing_modules)
  @sampling_configuration = nil
  @type_to_subscribers = {}
  tracing_modules.each do |mod|
    consumed_type = mod.consumed_type
    @type_to_subscribers[consumed_type] ||= []
    @type_to_subscribers[consumed_type] << mod
  end
end

Public Instance Methods

interested_consumers(data_type, _hints = {}) click to toggle source

@param [Module] data_type @param [Hash] _hints reserved for future use, e.g. virtual scopes @return [Sqreen::Ecosystem::TracingBroker::ObserverLookup]

# File lib/sqreen/ecosystem/tracing_broker.rb, line 45
def interested_consumers(data_type, _hints = {})
  unless @sampling_configuration
    logger.debug do
      "Declaring no one is interested in #{data_type} " \
      "because tracing hasn't been enabled yet"
    end
    return false
  end

  # if we have several modules with the same scope, we
  # should ask whether we should sample only once
  scope_to_should_sample = Hash.new do |hash, scope|
    result = @sampling_configuration.should_sample?(scope)
    if result
      logger.debug { "Will sample scope #{scope}. Sampling line: #{result}" }
    else
      logger.debug { "Will NOT sample scope #{scope}" }
    end

    hash[scope] = result
  end

  res = subscribers(data_type).select do |mod|
    scope_to_should_sample[mod.scope]
  end

  res.empty? ? false : ObserverLookup.new(res)
end
publish(data, prev_lookup) click to toggle source

@param [Object] data @param [Sqreen::Ecosystem::TracingBroker::ObserverLookup] prev_lookup

# File lib/sqreen/ecosystem/tracing_broker.rb, line 36
def publish(data, prev_lookup)
  prev_lookup.modules.each do |mod|
    mod_process_data mod, data
  end
end

Private Instance Methods

mod_process_data(mod, data) click to toggle source

@param [Sqreen::Ecosystem::ModuleApi::Tracing] mod

# File lib/sqreen/ecosystem/tracing_broker.rb, line 77
def mod_process_data(mod, data)
  mod.receive(data)
rescue ::Exception => e # rubocop:disable Lint/RescueException
  report_exception("Error invoking tracing module #{mod}", e)
end
parents(type) click to toggle source
# File lib/sqreen/ecosystem/tracing_broker.rb, line 101
def parents(type)
  type.ancestors - Object.ancestors - [type]
end
subscribers(data_type) click to toggle source

@param [Module] data_type @return Array<Sqreen::Ecosystem::ModuleApi::Tracing>

# File lib/sqreen/ecosystem/tracing_broker.rb, line 85
def subscribers(data_type)
  subscribers = @type_to_subscribers[data_type]

  # None of the modules subscribes to data_type directly,
  # but maybe they subscribe to one of the ancestors
  # Cache this lookup
  unless subscribers
    subscribers = parents(data_type).inject([]) do |accum, type|
      accum + (@type_to_subscribers[type] || [])
    end
    @type_to_subscribers[data_type] = subscribers
  end

  subscribers
end