Sha256: d4be110867626d9e535c3921451f75e69196c4330d0c8a2995ca0dfb5adcb150

Contents?: true

Size: 1.33 KB

Versions: 34

Compression:

Stored size: 1.33 KB

Contents

# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic::Agent
  # Basic mechanism for the agent instance to provide agent-wide eventing.
  # It is intended to keep different pieces of the app decoupled from each other.
  #
  # While an EventListener could be used elsewhere, it's strongly expected
  # your eventing needs should be met by the agent's instance.
  class EventListener
    attr_accessor :runaway_threshold

    def initialize
      @events = {}
      @runaway_threshold = 100
    end

    def subscribe(event, &handler)
      @events[event] ||= []
      @events[event] << handler
      check_for_runaway_subscriptions(event)
    end

    def check_for_runaway_subscriptions(event)
      count = @events[event].size
      NewRelic::Agent.logger.debug("Run-away event subscription on #{event}? Subscribed #{count}") if count > @runaway_threshold
    end

    def clear
      @events.clear
    end

    def notify(event, *args)
      return unless @events.has_key?(event)

      @events[event].each do |handler|
        begin
          handler.call(*args)
        rescue => err
          NewRelic::Agent.logger.debug("Failure during notify for #{event}", err)
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
newrelic_rpm-9.17.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.16.1 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.16.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.15.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.14.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.13.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.12.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.11.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.10.2 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.10.1 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.10.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.9.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.8.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.7.1 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.7.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.6.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.5.0 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.4.2 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.4.1 lib/new_relic/agent/event_listener.rb
newrelic_rpm-9.4.0 lib/new_relic/agent/event_listener.rb