Module: NewRelic::Coerce

Class Method Summary collapse

Class Method Details

.boolean_int!(value) ⇒ Object

Use when you plan to perform a boolean check using the integer 1 for true and the integer 0 for false String values will be converted to 0



75
76
77
# File 'lib/new_relic/coerce.rb', line 75

def boolean_int!(value)
  value.to_i
end

.float(value, context = nil) ⇒ Object



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

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

.float!(value, precision = NewRelic::PRIORITY_PRECISION) ⇒ Object



79
80
81
82
83
# File 'lib/new_relic/coerce.rb', line 79

def float!(value, precision = NewRelic::PRIORITY_PRECISION)
  return nil unless value_or_nil(value)

  value.to_f.round(precision)
end

.int(value, context = nil) ⇒ Object

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 non-bang 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



16
17
18
19
20
21
# File 'lib/new_relic/coerce.rb', line 16

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

.int!(value) ⇒ Object



66
67
68
69
70
# File 'lib/new_relic/coerce.rb', line 66

def int!(value)
  return nil unless value_or_nil(value)

  Integer(value)
end

.int_or_nil(value, context = nil) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/new_relic/coerce.rb', line 23

def int_or_nil(value, context = nil)
  return nil if value.nil?

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

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



91
92
93
94
95
# File 'lib/new_relic/coerce.rb', line 91

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

.scalar(val) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/new_relic/coerce.rb', line 51

def scalar(val)
  case val
  when String, Integer, TrueClass, FalseClass, NilClass
    val
  when Float
    if val.finite?
      val
    end
  when Symbol
    val.to_s
  else
    "#<#{val.class.to_s}>"
  end
end

.string(value, context = nil) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/new_relic/coerce.rb', line 42

def string(value, context = nil)
  return value if value.nil?

  String(value)
rescue => error
  log_failure(value.class, String, context, error)
  EMPTY_STR
end

.value_or_nil(value) ⇒ Object



85
86
87
88
89
# File 'lib/new_relic/coerce.rb', line 85

def value_or_nil(value)
  return nil if value.nil? || (value.respond_to?(:empty?) && value.empty?)

  value
end