class NewRelic::Agent::EventAggregator

Public Class Methods

buffer_class(klass = nil) click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 22
def buffer_class klass = nil
  if klass
    @buffer_class = klass
  else
    @buffer_class ||= SampledBuffer
  end
end
capacity_key(key = nil) click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 14
def capacity_key key = nil
  key ? @capacity_key = key : @capacity_key
end
enabled_key(key = nil) click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 18
def enabled_key key = nil
  key ? @enabled_key = key : @enabled_key
end
named(named = nil) click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 10
def named named = nil
  named ? @named = named.to_s.freeze : @named
end
new() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 31
def initialize
  @lock = Mutex.new
  @buffer = self.class.buffer_class.new NewRelic::Agent.config[self.class.capacity_key]
  @enabled = false
  @notified_full = false
  register_capacity_callback
  register_enabled_callback
  after_initialize
end

Public Instance Methods

after_harvest(metadata) click to toggle source

interface method for subclasses to override to provide post harvest functionality

# File lib/new_relic/agent/event_aggregator.rb, line 46
def after_harvest metadata
end
after_initialize() click to toggle source

interface method for subclasses to override to provide post-initialization setup

# File lib/new_relic/agent/event_aggregator.rb, line 42
def after_initialize
end
enabled?() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 49
def enabled?
  @enabled
end
harvest!() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 57
def harvest!
  metadata = nil
  samples = []
  @lock.synchronize do
    samples.concat @buffer.to_a
    metadata = @buffer.metadata
    reset_buffer!
  end
  after_harvest metadata
  [reservoir_metadata(metadata), samples]
end
has_metadata?() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 53
def has_metadata?
  true
end
merge!(payload, adjust_count = true) click to toggle source

Merges samples from payload back into buffer and optionally adjusts the count of the buffer to ensure accuracy of buffer of metadata. We want to make sure not to double count samples being merged back in from a failed harvest, yet we do not want to under-count samples being merged from the PipeService.

# File lib/new_relic/agent/event_aggregator.rb, line 73
def merge! payload, adjust_count = true
  @lock.synchronize do
    _, samples = payload

    if adjust_count
      @buffer.decrement_lifetime_counts_by samples.count
    end

    samples.each { |s| @buffer.append s }
  end
end
reset!() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 85
def reset!
  @lock.synchronize do
    reset_buffer!
  end
end

Private Instance Methods

notify_if_full() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 117
def notify_if_full
  return unless !@notified_full && @buffer.full?
  NewRelic::Agent.logger.debug "#{self.class.named} capacity of #{@buffer.capacity} reached, beginning sampling"
  @notified_full = true
end
register_capacity_callback() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 100
def register_capacity_callback
  NewRelic::Agent.config.register_callback(self.class.capacity_key) do |max_samples|
    NewRelic::Agent.logger.debug "#{self.class.named} max_samples set to #{max_samples}"
    @lock.synchronize do
      @buffer.capacity = max_samples
    end
  end
end
register_enabled_callback() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 109
def register_enabled_callback
  NewRelic::Agent.config.register_callback(self.class.enabled_key) do |enabled|
    # intentionally unsynchronized for liveness
    @enabled = enabled
    ::NewRelic::Agent.logger.debug "#{self.class.named} will #{enabled ? '' : 'not '}be sent to the New Relic service."
  end
end
reservoir_metadata(metadata) click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 93
def reservoir_metadata metadata
  {
    :reservoir_size => metadata[:capacity],
    :events_seen => metadata[:seen]
  }
end
reset_buffer!() click to toggle source
# File lib/new_relic/agent/event_aggregator.rb, line 123
def reset_buffer!
  @buffer.reset!
  @notified_full = false
end