# encoding: utf-8 require 'one_apm/collector/support/shell_poller' require 'one_apm/collector/support/proc_poller' require 'one_apm/collector/support/jruby_poller' module OneApm module Collector class SamplerCollection include Enumerable def initialize(event_listener) @samplers = [] @event_listener = event_listener @event_listener.subscribe(:before_harvest) { poll_samplers } end def each(&blk) @samplers.each(&blk) end def clear() @samplers.clear end def sampler_class_registered?(sampler_class) self.any? { |s| s.class == sampler_class } end def load_samplers Sampler.sampler_classes.each do |subclass| add_sampler(subclass) end end def poll_samplers @samplers.delete_if do |sampler| begin sampler.poll false rescue => e OneApm::Manager.logger.warn("Removing #{sampler} from list", e) true end end end def add_sampler(sampler_class) supported = sampler_class.supported_on_this_platform? enabled = sampler_class.enabled? if supported && enabled && !sampler_class_registered?(sampler_class) sampler = sampler_class.new sampler.setup_events(@event_listener) if sampler.respond_to?(:setup_events) sampler.set_poller(poller) if sampler.respond_to?(:set_poller) @samplers << sampler OneApm::Manager.logger.debug("Registered #{sampler_class.name} for harvest time sampling.") else OneApm::Manager.logger.debug("#{sampler_class.name} not supported on this platform.") end rescue OneApm::Collector::Sampler::Unsupported => e OneApm::Manager.logger.info("#{sampler_class.name} not available: #{e}") rescue => e OneApm::Manager.logger.error("Error registering sampler:", e) end def poller @pller ||= begin if OneApm::Collector::JrubyPoller.enable? OneApm::Collector::JrubyPoller.new elsif OneApm::Collector::ProcPoller.enable? OneApm::Collector::ProcPoller.new else OneApm::Collector::ShellPoller.new end end end end end end