Module: NewRelic::Coerce

Overview

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

Instance Method Summary collapse

Instance Method Details

#float(value, context = nil) ⇒ Object



20
21
22
23
24
25
# File 'lib/new_relic/coerce.rb', line 20

def float(value, context=nil)
  Float(value)
rescue => error
  log_failure(value, Float, context, error)
  0.0
end

#int(value, context = nil) ⇒ Object



13
14
15
16
17
18
# File 'lib/new_relic/coerce.rb', line 13

def int(value, context=nil)
  Integer(value)
rescue => error
  log_failure(value, Integer, context, error)
  0
end

#log_failure(value, type, context, error) ⇒ Object



35
36
37
38
39
# File 'lib/new_relic/coerce.rb', line 35

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

#string(value, context = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/new_relic/coerce.rb', line 27

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