Sha256: 81e4f06a17b3ed640b922896a3f19abd88cc72bd7c40020b45378c0e2c441bbc

Contents?: true

Size: 1.21 KB

Versions: 105

Compression:

Stored size: 1.21 KB

Contents

require 'puppet/util/logging'

# Logging utility class that limits the frequency of identical log messages
class Puppet::Util::Log::RateLimitedLogger
  include Puppet::Util::Logging

  def initialize(interval)
    raise ArgumentError, "Logging rate-limit interval must be an integer" unless interval.is_a?(Integer)
    @interval = interval
    @log_record = {}
  end

  # Override the logging entry point to rate-limit it
  def send_log(level, message)
    Puppet::Util::Log.create({:level => level, :message => message}) if should_log?(level, message)
  end

  private

  def should_log?(level, message)
    # Initialize separate records for different levels, and only when needed
    record = (@log_record[level] ||= {})
    last_log = record[message]

    # Skip logging if the time interval since the last logging hasn't elapsed yet
    return false if last_log and within_interval?(last_log)

    # Purge stale entries; do this after the interval check to reduce passes through the cache
    record.delete_if { |key, time| !within_interval?(time) }

    # Reset the beginning of the interval to the current time
    record[message] = Time.now

    true
  end

  def within_interval?(time)
    time + @interval > Time.now
  end
end

Version data entries

105 entries across 105 versions & 3 rubygems

Version Path
puppet-3.7.3-x64-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.2 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.2-x86-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.2-x64-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.1 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.1-x86-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.1-x64-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.0 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.0-x86-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.7.0-x64-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.6.2 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.6.2-x86-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.6.1 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.6.1-x86-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.6.0 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.6.0-x86-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.6.0.rc1 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.6.0.rc1-x86-mingw32 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.5.1 lib/puppet/util/log/rate_limited_logger.rb
puppet-3.5.1-x86-mingw32 lib/puppet/util/log/rate_limited_logger.rb