class NewRelic::Agent::LogEventAttributes

Constants

ATTRIBUTE_KEY_CHARACTER_LIMIT
ATTRIBUTE_VALUE_CHARACTER_LIMIT
MAX_ATTRIBUTE_COUNT

Public Instance Methods

add_custom_attributes(attributes) click to toggle source
# File lib/new_relic/agent/log_event_attributes.rb, line 12
def add_custom_attributes(attributes)
  return if defined?(@custom_attribute_limit_reached) && @custom_attribute_limit_reached

  attributes.each do |key, value|
    next if absent?(key) || absent?(value)

    add_custom_attribute(key, value)
  end
end
custom_attributes() click to toggle source
# File lib/new_relic/agent/log_event_attributes.rb, line 22
def custom_attributes
  @custom_attributes ||= {}
end

Private Instance Methods

absent?(value) click to toggle source
# File lib/new_relic/agent/log_event_attributes.rb, line 47
def absent?(value)
  value.nil? || (value.respond_to?(:empty?) && value.empty?)
end
add_custom_attribute(key, value) click to toggle source
# File lib/new_relic/agent/log_event_attributes.rb, line 51
def add_custom_attribute(key, value)
  if custom_attributes.size >= MAX_ATTRIBUTE_COUNT
    NewRelic::Agent.logger.warn(
      'Too many custom log attributes defined. ' \
      "Only taking the first #{MAX_ATTRIBUTE_COUNT}."
    )
    @custom_attribute_limit_reached = true
    return
  end

  @custom_attributes.merge!(truncate_attributes(key_to_string(key), value))
end
key_to_string(key) click to toggle source
# File lib/new_relic/agent/log_event_attributes.rb, line 64
def key_to_string(key)
  key.is_a?(String) ? key : key.to_s
end
truncate_attribute(attribute, limit) click to toggle source
# File lib/new_relic/agent/log_event_attributes.rb, line 68
def truncate_attribute(attribute, limit)
  case attribute
  when Integer
    if attribute.digits.length > limit
      raise TruncationError.new(attribute, limit)
    end
  when Float
    if attribute.to_s.length > limit
      raise TruncationError.new(attribute, limit)
    end
  when String, Symbol
    if attribute.length > limit
      attribute = attribute.slice(0..(limit - 1))
    end
  when TrueClass, FalseClass
    attribute
  else
    raise InvalidTypeError.new(attribute)
  end

  attribute
end
truncate_attributes(key, value) click to toggle source
# File lib/new_relic/agent/log_event_attributes.rb, line 91
def truncate_attributes(key, value)
  key = truncate_attribute(key, ATTRIBUTE_KEY_CHARACTER_LIMIT)
  value = truncate_attribute(value, ATTRIBUTE_VALUE_CHARACTER_LIMIT)

  {key => value}
rescue TruncationError => e
  NewRelic::Agent.logger.warn(
    "Dropping custom log attribute #{key} => #{value} \n" \
    "Length exceeds character limit of #{e.limit}. " \
    "Can't truncate: #{e.attribute}"
  )

  {}
rescue InvalidTypeError => e
  NewRelic::Agent.logger.warn(
    "Dropping custom log attribute #{key} => #{value} \n" \
    "Invalid type of #{e.attribute.class} given. " \
    "Can't send #{e.attribute}."
  )

  {}
end