Sha256: e5fc7ec43550553bda01e49c7872659d1d3676c41cf7128d51d2927dbb77876c

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

module NewRelic
  module CollectionHelper
  DEFAULT_TRUNCATION_SIZE=256
  DEFAULT_ARRAY_TRUNCATION_SIZE=1024
  # Transform parameter hash into a hash whose values are strictly
  # strings
  def normalize_params(params)
    case params
      when Symbol, FalseClass, TrueClass, nil
        params
      when Numeric
        truncate(params.to_s)
      when String
        truncate(params)
      when Hash
        new_params = {}
        params.each do | key, value |
          new_params[truncate(normalize_params(key),32)] = normalize_params(value)
        end
        new_params
      when Array
        params.first(DEFAULT_ARRAY_TRUNCATION_SIZE).map{|item| normalize_params(item)}
    else
      truncate(flatten(params))
    end
  end

  # Return an array of strings (backtrace), cleaned up for readability
  # Return nil if there is no backtrace

  def strip_nr_from_backtrace(backtrace)
    if backtrace
      # this is for 1.9.1, where strings no longer have Enumerable
      backtrace = backtrace.split("\n") if String === backtrace
      backtrace = backtrace.reject {|line| line =~ /new_?relic/ }
      # rename methods back to their original state
      backtrace = backtrace.collect {|line| line.to_s.gsub(/_without_(newrelic|trace)/, "")}
    end
    backtrace
  end

  private

  # Convert any kind of object to a short string.
  def flatten(object)
    s = case object
      when nil then ''
      when object.instance_of?(String) then object
      when String then String.new(object)  # convert string subclasses to strings
      else "#<#{object.class.to_s}>"
    end
  end
  def truncate(string, len=DEFAULT_TRUNCATION_SIZE)
    case string
    when Symbol then string
    when nil then ""
    when String
      real_string = flatten(string)
      if real_string.size > len
        real_string = real_string.slice(0...len)
        real_string << "..."
      end
      real_string
    else
      truncate(flatten(string), len)
    end
  end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
ghazel-newrelic_rpm-3.1.0.1 lib/new_relic/collection_helper.rb
newrelic_rpm-3.1.0 lib/new_relic/collection_helper.rb
newrelic_rpm-3.1.0.beta5 lib/new_relic/collection_helper.rb
newrelic_rpm-3.1.0.beta4 lib/new_relic/collection_helper.rb
newrelic_rpm-3.1.0.djlogging2 lib/new_relic/collection_helper.rb
newrelic_rpm-3.1.0.djlogging lib/new_relic/collection_helper.rb
newrelic_rpm-3.1.0.beta3 lib/new_relic/collection_helper.rb
newrelic_rpm-3.1.0.beta2 lib/new_relic/collection_helper.rb
newrelic_rpm-3.1.0.beta1 lib/new_relic/collection_helper.rb