Class: NewRelic::Agent::SamplerCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/new_relic/agent/sampler_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(event_listener) ⇒ SamplerCollection

Returns a new instance of SamplerCollection.



10
11
12
13
14
# File 'lib/new_relic/agent/sampler_collection.rb', line 10

def initialize(event_listener)
  @samplers = []
  @event_listener = event_listener
  @event_listener.subscribe(:before_harvest) { poll_samplers }
end

Instance Method Details

#add_sampler(sampler_class) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/new_relic/agent/sampler_collection.rb', line 49

def add_sampler(sampler_class)
  supported = sampler_class.supported_on_this_platform?
  enabled = sampler_class.enabled?
  if supported && enabled
    if !sampler_class_registered?(sampler_class)
      sampler = sampler_class.new
      sampler.setup_events(@event_listener) if sampler.respond_to?(:setup_events)
      @samplers << sampler
      ::NewRelic::Agent.logger.debug("Registered #{sampler_class.name} for harvest time sampling.")
    else
      ::NewRelic::Agent.logger.warn("Ignoring addition of #{sampler_class.name} because it is already registered.")
    end
  else
    ::NewRelic::Agent.logger.debug("#{sampler_class.name} not supported on this platform.")
  end
rescue NewRelic::Agent::Sampler::Unsupported => e
  ::NewRelic::Agent.logger.info("#{sampler_class.name} not available: #{e}")
rescue => e
  ::NewRelic::Agent.logger.error('Error registering sampler:', e)
end

#clearObject



20
21
22
# File 'lib/new_relic/agent/sampler_collection.rb', line 20

def clear
  @samplers.clear
end

#each(&blk) ⇒ Object



16
17
18
# File 'lib/new_relic/agent/sampler_collection.rb', line 16

def each(&blk)
  @samplers.each(&blk)
end

#load_samplersObject

adds samplers to the sampler collection so that they run every minute. This is dynamically recognized by any class that subclasses NewRelic::Agent::Sampler



31
32
33
34
35
# File 'lib/new_relic/agent/sampler_collection.rb', line 31

def load_samplers
  Sampler.sampler_classes.each do |subclass|
    add_sampler(subclass)
  end
end

#poll_samplersObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/new_relic/agent/sampler_collection.rb', line 37

def poll_samplers
  @samplers.delete_if do |sampler|
    begin
      sampler.poll
      false # it's okay.  don't delete it.
    rescue => e
      ::NewRelic::Agent.logger.warn("Removing #{sampler} from list", e)
      true # remove the sampler
    end
  end
end

#sampler_class_registered?(sampler_class) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/new_relic/agent/sampler_collection.rb', line 24

def sampler_class_registered?(sampler_class)
  self.any? { |s| s.class == sampler_class }
end