Class: NewRelic::NoticedError

Inherits:
Object
  • Object
show all
Extended by:
CollectionHelper
Includes:
Coerce
Defined in:
lib/new_relic/noticed_error.rb

Overview

This class encapsulates an error that was noticed by New Relic in a managed app.

Constant Summary collapse

STRIPPED_EXCEPTION_REPLACEMENT_MESSAGE =
"Message removed by New Relic 'strip_exception_messages' setting"
UNKNOWN_ERROR_CLASS_NAME =
'Error'
NIL_ERROR_MESSAGE =
'<no message>'
USER_ATTRIBUTES =
'userAttributes'
AGENT_ATTRIBUTES =
'agentAttributes'
INTRINSIC_ATTRIBUTES =
'intrinsics'
DESTINATION =
NewRelic::Agent::AttributeFilter::DST_ERROR_COLLECTOR
AGENT_ATTRIBUTE_ERROR_GROUP =
:'error.group.name'
ERROR_PREFIX_KEY =
'error'
ERROR_MESSAGE_KEY =
"#{ERROR_PREFIX_KEY}.message"
ERROR_CLASS_KEY =
"#{ERROR_PREFIX_KEY}.class"
ERROR_EXPECTED_KEY =
"#{ERROR_PREFIX_KEY}.expected"

Constants included from CollectionHelper

CollectionHelper::DEFAULT_ARRAY_TRUNCATION_SIZE, CollectionHelper::DEFAULT_TRUNCATION_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CollectionHelper

normalize_params

Methods included from Coerce

boolean_int!, float, float!, int, int!, int_or_nil, log_failure, scalar, string, value_or_nil

Constructor Details

#initialize(path, exception, timestamp = Process.clock_gettime(Process::CLOCK_REALTIME), expected = false) ⇒ NoticedError

Returns a new instance of NoticedError.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/new_relic/noticed_error.rb', line 37

def initialize(path, exception, timestamp = Process.clock_gettime(Process::CLOCK_REALTIME), expected = false)
  @exception_id = exception.object_id
  @path = path

  # It's critical that we not hold onto the exception class constant in this
  # class. These objects get serialized for Resque to a process that might
  # not have the original exception class loaded, so do all processing now
  # while we have the actual exception!
  @is_internal = (exception.class < NewRelic::Agent::InternalAgentError)

  extract_class_name_and_message_from(exception)

  # clamp long messages to 4k so that we don't send a lot of
  # overhead across the wire
  @message = @message[0..4095] if @message.length > 4096

  # replace error message if enabled
  if NewRelic::Agent.config[:'strip_exception_messages.enabled'] &&
      !self.class.passes_message_allowlist(exception.class)
    @message = STRIPPED_EXCEPTION_REPLACEMENT_MESSAGE
  end

  @attributes_from_notice_error = nil
  @attributes = nil
  @timestamp = timestamp
  @expected = expected
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def attributes
  @attributes
end

#attributes_from_notice_errorObject

Returns the value of attribute attributes_from_notice_error.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def attributes_from_notice_error
  @attributes_from_notice_error
end

#error_groupObject

Returns the value of attribute error_group.



18
19
20
# File 'lib/new_relic/noticed_error.rb', line 18

def error_group
  @error_group
end

#exception_class_nameObject

Returns the value of attribute exception_class_name.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def exception_class_name
  @exception_class_name
end

#exception_idObject (readonly)

Returns the value of attribute exception_id.



18
19
20
# File 'lib/new_relic/noticed_error.rb', line 18

def exception_id
  @exception_id
end

#expectedObject

Returns the value of attribute expected.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def expected
  @expected
end

#file_nameObject

Returns the value of attribute file_name.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def file_name
  @file_name
end

#is_internalObject (readonly)

Returns the value of attribute is_internal.



18
19
20
# File 'lib/new_relic/noticed_error.rb', line 18

def is_internal
  @is_internal
end

#line_numberObject

Returns the value of attribute line_number.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def line_number
  @line_number
end

#messageObject

Returns the value of attribute message.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def message
  @message
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def path
  @path
end

#request_portObject

Returns the value of attribute request_port.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def request_port
  @request_port
end

#request_uriObject

Returns the value of attribute request_uri.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def request_uri
  @request_uri
end

#stack_traceObject

Returns the value of attribute stack_trace.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def stack_trace
  @stack_trace
end

#timestampObject

Returns the value of attribute timestamp.



13
14
15
# File 'lib/new_relic/noticed_error.rb', line 13

def timestamp
  @timestamp
end

Class Method Details

.passes_message_allowlist(exception_class) ⇒ Object



73
74
75
76
77
# File 'lib/new_relic/noticed_error.rb', line 73

def self.passes_message_allowlist(exception_class)
  NewRelic::Agent.config[:'strip_exception_messages.allowed_classes'].any? do |klass|
    exception_class <= klass
  end
end

Instance Method Details

#==(other) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/new_relic/noticed_error.rb', line 65

def ==(other)
  if other.respond_to?(:exception_id)
    exception_id == other.exception_id
  else
    false
  end
end

#agent_attributesObject



163
164
165
# File 'lib/new_relic/noticed_error.rb', line 163

def agent_attributes
  processed_attributes[AGENT_ATTRIBUTES]
end

#append_attributes(outgoing_params, outgoing_key, source_attributes) ⇒ Object



159
160
161
# File 'lib/new_relic/noticed_error.rb', line 159

def append_attributes(outgoing_params, outgoing_key, source_attributes)
  outgoing_params[outgoing_key] = source_attributes || {}
end

#base_parametersObject



103
104
105
106
107
108
109
110
# File 'lib/new_relic/noticed_error.rb', line 103

def base_parameters
  params = {}
  params[:file_name] = file_name if file_name
  params[:line_number] = line_number if line_number
  params[:stack_trace] = stack_trace if stack_trace
  params[ERROR_EXPECTED_KEY.to_sym] = expected
  params
end

#build_agent_attributes(merged_attributes) ⇒ Object



145
146
147
148
149
# File 'lib/new_relic/noticed_error.rb', line 145

def build_agent_attributes(merged_attributes)
  return NewRelic::EMPTY_HASH unless @attributes

  @attributes.agent_attributes_for(DESTINATION)
end

#build_error_attributesObject



137
138
139
140
141
142
143
# File 'lib/new_relic/noticed_error.rb', line 137

def build_error_attributes
  @attributes_from_notice_error ||= {}
  @attributes_from_notice_error[ERROR_MESSAGE_KEY] = string(message)
  @attributes_from_notice_error[ERROR_CLASS_KEY] = string(exception_class_name)

  @attributes_from_notice_error[ERROR_EXPECTED_KEY] = true if expected
end

#build_intrinsic_attributesObject



151
152
153
154
155
156
157
# File 'lib/new_relic/noticed_error.rb', line 151

def build_intrinsic_attributes
  if @attributes
    @attributes.intrinsic_attributes_for(DESTINATION)
  else
    NewRelic::EMPTY_HASH
  end
end

#custom_attributesObject



167
168
169
# File 'lib/new_relic/noticed_error.rb', line 167

def custom_attributes
  processed_attributes[USER_ATTRIBUTES]
end

#extract_class_name_and_message_from(exception) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/new_relic/noticed_error.rb', line 175

def extract_class_name_and_message_from(exception)
  if exception.nil?
    @exception_class_name = UNKNOWN_ERROR_CLASS_NAME
    @message = NIL_ERROR_MESSAGE
  elsif exception.is_a?(NewRelic::Agent::NoticeableError)
    @exception_class_name = exception.class_name
    @message = exception.message
  else
    if defined?(Rails::VERSION::MAJOR) && Rails::VERSION::MAJOR < 5 && exception.respond_to?(:original_exception)
      exception = exception.original_exception || exception
    end
    @exception_class_name = exception.is_a?(Exception) ? exception.class.name : UNKNOWN_ERROR_CLASS_NAME
    @message = exception.to_s
  end
end

#intrinsic_attributesObject



171
172
173
# File 'lib/new_relic/noticed_error.rb', line 171

def intrinsic_attributes
  processed_attributes[INTRINSIC_ATTRIBUTES]
end

#merge_custom_attributes_from_notice_error(merged_attributes) ⇒ Object



130
131
132
133
134
135
# File 'lib/new_relic/noticed_error.rb', line 130

def merge_custom_attributes_from_notice_error(merged_attributes)
  if @attributes_from_notice_error
    from_notice_error = NewRelic::NoticedError.normalize_params(@attributes_from_notice_error)
    merged_attributes.merge_custom_attributes(from_notice_error)
  end
end

#merge_custom_attributes_from_transaction(merged_attributes) ⇒ Object



123
124
125
126
127
128
# File 'lib/new_relic/noticed_error.rb', line 123

def merge_custom_attributes_from_transaction(merged_attributes)
  if @attributes
    from_transaction = @attributes.custom_attributes_for(DESTINATION)
    merged_attributes.merge_custom_attributes(from_transaction)
  end
end

#merged_custom_attributes(merged_attributes) ⇒ Object

We can get custom attributes from two sources–the transaction, which we hold in @attributes, or passed options to notice_error which show up in in our Attributes class for consistent handling



116
117
118
119
120
121
# File 'lib/new_relic/noticed_error.rb', line 116

def merged_custom_attributes(merged_attributes)
  merge_custom_attributes_from_transaction(merged_attributes)
  merge_custom_attributes_from_notice_error(merged_attributes)

  merged_attributes.custom_attributes_for(DESTINATION)
end

#processed_attributesObject

Note that we process attributes lazily and store the result. This is because there is a possibility that a noticed error will be discarded and not sent back as a traced error or TransactionError.



92
93
94
95
96
97
98
99
100
101
# File 'lib/new_relic/noticed_error.rb', line 92

def processed_attributes
  @processed_attributes ||= begin
    attributes = base_parameters
    merged_attributes = NewRelic::Agent::Attributes.new(NewRelic::Agent.instance.attribute_filter)
    append_attributes(attributes, USER_ATTRIBUTES, merged_custom_attributes(merged_attributes))
    append_attributes(attributes, AGENT_ATTRIBUTES, build_agent_attributes(merged_attributes))
    append_attributes(attributes, INTRINSIC_ATTRIBUTES, build_intrinsic_attributes)
    attributes
  end
end

#to_collector_array(encoder = nil) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/new_relic/noticed_error.rb', line 81

def to_collector_array(encoder = nil)
  [NewRelic::Helper.time_to_millis(timestamp),
    string(path),
    string(message),
    string(exception_class_name),
    processed_attributes]
end