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
# File 'lib/new_relic/agent/sampler_collection.rb', line 10

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

Instance Method Details

#add_sampler(sampler_class) ⇒ Object



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

def add_sampler(sampler_class)
  if sampler_class.supported_on_this_platform?
    if !sampler_class_registered?(sampler_class)
      @samplers << sampler_class.new
      ::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

#each(&blk) ⇒ Object



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

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

#poll_samplersObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/new_relic/agent/sampler_collection.rb', line 23

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)


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

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