Sha256: fa6dd9adab435e7c2b480c48a79010ebc3a2a020700a28c1c476c23500f1dc5d

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.

module NewRelic
  # We really don't want to send bad values to the collector, and it doesn't
  # accept types like Rational that have occasionally slipped into our data.
  #
  # These methods are intended to safely coerce things into the form we want,
  # to provide documentation of expected types on to_collector_array methods,
  # and to log failures if totally invalid data gets into outgoing data
  module Coerce
    def int(value, context=nil)
      Integer(value)
    rescue => error
      log_failure(value, Integer, context, error)
      0
    end

    def int_or_nil(value, context=nil)
      return nil if value.nil?
      Integer(value)
    rescue => error
      log_failure(value, Integer, context, error)
      nil
    end

    def float(value, context=nil)
      result = Float(value)
      raise "Value #{result.inspect} is not finite." unless result.finite?
      result
    rescue => error
      log_failure(value, Float, context, error)
      0.0
    end

    def string(value, context=nil)
      return value if value.nil?
      String(value)
    rescue => error
      log_failure(value.class, String, context, error)
      ""
    end

    def log_failure(value, type, context, error)
      msg = "Unable to convert '#{value}' to #{type}"
      msg += " in context '#{context}'" if context
      NewRelic::Agent.logger.warn(msg, error)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
newrelic_rpm-3.6.9.171 lib/new_relic/coerce.rb
newrelic_rpm-3.6.8.168 lib/new_relic/coerce.rb
newrelic_rpm-3.6.8.164 lib/new_relic/coerce.rb