Sha256: d7c51945f47fe9f17349f606d634ed73c2fb285b6f5aeafa038ed4774e7824c9

Contents?: true

Size: 1.81 KB

Versions: 26

Compression:

Stored size: 1.81 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
  module CollectionHelper
    DEFAULT_TRUNCATION_SIZE = 16 * 1024
    DEFAULT_ARRAY_TRUNCATION_SIZE = 128
    # Transform parameter hash into a hash whose values are strictly
    # strings
    def normalize_params(params)
      case params
        when Hash
          # Optimize for empty hash since that is often what this is called with.
          return params if params.empty?

          new_params = {}
          params.each do |key, value|
            new_params[truncate(normalize_params(key), 64)] = normalize_params(value)
          end
          new_params
        when Symbol, FalseClass, TrueClass, nil
          params
        when Numeric
          truncate(params.to_s)
        when String
          truncate(params)
        when Array
          params.first(DEFAULT_ARRAY_TRUNCATION_SIZE).map { |item| normalize_params(item) }
        else
          truncate(flatten(params))
      end
    end

    private

    # Convert any kind of object to a short string.
    def flatten(object)
      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}>"
      end
    end

    def truncate(string, len = DEFAULT_TRUNCATION_SIZE)
      case string
      when Symbol then string
      when nil then EMPTY_STR
      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

26 entries across 26 versions & 1 rubygems

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