newrelic_rpm 7.0.0 → 8.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/CHANGELOG.md +187 -0
  4. data/CONTRIBUTING.md +13 -2
  5. data/README.md +5 -1
  6. data/lib/new_relic/agent/adaptive_sampler.rb +2 -2
  7. data/lib/new_relic/agent/agent.rb +8 -7
  8. data/lib/new_relic/agent/commands/thread_profiler_session.rb +7 -3
  9. data/lib/new_relic/agent/configuration/default_source.rb +223 -158
  10. data/lib/new_relic/agent/configuration/event_harvest_config.rb +28 -12
  11. data/lib/new_relic/agent/configuration/manager.rb +1 -2
  12. data/lib/new_relic/agent/configuration/server_source.rb +3 -2
  13. data/lib/new_relic/agent/configuration/yaml_source.rb +22 -1
  14. data/lib/new_relic/agent/connect/request_builder.rb +4 -2
  15. data/lib/new_relic/agent/custom_event_aggregator.rb +2 -1
  16. data/lib/new_relic/agent/database.rb +5 -2
  17. data/lib/new_relic/agent/datastores/mongo.rb +5 -10
  18. data/lib/new_relic/agent/datastores.rb +7 -7
  19. data/lib/new_relic/agent/distributed_tracing/cross_app_payload.rb +5 -5
  20. data/lib/new_relic/agent/distributed_tracing/cross_app_tracing.rb +10 -6
  21. data/lib/new_relic/agent/distributed_tracing/distributed_trace_attributes.rb +0 -1
  22. data/lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb +1 -1
  23. data/lib/new_relic/agent/distributed_tracing/trace_context_payload.rb +1 -1
  24. data/lib/new_relic/agent/error_collector.rb +52 -37
  25. data/lib/new_relic/agent/error_filter.rb +175 -0
  26. data/lib/new_relic/agent/event_loop.rb +6 -6
  27. data/lib/new_relic/agent/external.rb +0 -32
  28. data/lib/new_relic/agent/http_clients/abstract.rb +2 -2
  29. data/lib/new_relic/agent/http_clients/net_http_wrappers.rb +7 -1
  30. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +1 -1
  31. data/lib/new_relic/agent/instrumentation/active_merchant.rb +3 -3
  32. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +1 -3
  33. data/lib/new_relic/agent/instrumentation/bunny/instrumentation.rb +1 -1
  34. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +2 -1
  35. data/lib/new_relic/agent/instrumentation/delayed_job/instrumentation.rb +4 -4
  36. data/lib/new_relic/agent/instrumentation/excon.rb +4 -23
  37. data/lib/new_relic/agent/instrumentation/logger/chain.rb +21 -0
  38. data/lib/new_relic/agent/instrumentation/logger/instrumentation.rb +59 -0
  39. data/lib/new_relic/agent/instrumentation/logger/prepend.rb +13 -0
  40. data/lib/new_relic/agent/instrumentation/logger.rb +25 -0
  41. data/lib/new_relic/agent/instrumentation/mongo.rb +3 -141
  42. data/lib/new_relic/agent/instrumentation/net_http.rb +6 -1
  43. data/lib/new_relic/agent/instrumentation/padrino/chain.rb +23 -19
  44. data/lib/new_relic/agent/instrumentation/padrino/prepend.rb +1 -1
  45. data/lib/new_relic/agent/instrumentation/padrino.rb +4 -2
  46. data/lib/new_relic/agent/instrumentation/queue_time.rb +4 -4
  47. data/lib/new_relic/agent/instrumentation/rack/chain.rb +3 -2
  48. data/lib/new_relic/agent/instrumentation/rack/prepend.rb +2 -1
  49. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +5 -5
  50. data/lib/new_relic/agent/instrumentation/rails/action_web_service.rb +1 -1
  51. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +5 -41
  52. data/lib/new_relic/agent/instrumentation/redis.rb +4 -0
  53. data/lib/new_relic/agent/instrumentation/resque/helper.rb +19 -0
  54. data/lib/new_relic/agent/instrumentation/resque/instrumentation.rb +5 -3
  55. data/lib/new_relic/agent/instrumentation/resque.rb +13 -4
  56. data/lib/new_relic/agent/instrumentation/sidekiq.rb +6 -1
  57. data/lib/new_relic/agent/instrumentation/sinatra/instrumentation.rb +8 -2
  58. data/lib/new_relic/agent/javascript_instrumentor.rb +15 -10
  59. data/lib/new_relic/agent/messaging.rb +10 -24
  60. data/lib/new_relic/agent/method_tracer.rb +137 -138
  61. data/lib/new_relic/agent/monitors/cross_app_monitor.rb +6 -1
  62. data/lib/new_relic/agent/new_relic_service.rb +16 -12
  63. data/lib/new_relic/agent/pipe_channel_manager.rb +10 -6
  64. data/lib/new_relic/agent/pipe_service.rb +1 -1
  65. data/lib/new_relic/agent/samplers/cpu_sampler.rb +1 -1
  66. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +9 -6
  67. data/lib/new_relic/agent/span_event_aggregator.rb +2 -2
  68. data/lib/new_relic/agent/sql_sampler.rb +3 -3
  69. data/lib/new_relic/agent/stats_engine/stats_hash.rb +1 -1
  70. data/lib/new_relic/agent/stats_engine.rb +1 -1
  71. data/lib/new_relic/agent/threading/backtrace_service.rb +4 -5
  72. data/lib/new_relic/agent/threading/thread_profile.rb +1 -1
  73. data/lib/new_relic/agent/tracer.rb +15 -37
  74. data/lib/new_relic/agent/transaction/abstract_segment.rb +3 -3
  75. data/lib/new_relic/agent/transaction/message_broker_segment.rb +5 -11
  76. data/lib/new_relic/agent/transaction.rb +7 -28
  77. data/lib/new_relic/agent/transaction_time_aggregator.rb +5 -5
  78. data/lib/new_relic/agent/vm/snapshot.rb +1 -1
  79. data/lib/new_relic/agent/worker_loop.rb +5 -5
  80. data/lib/new_relic/agent.rb +10 -7
  81. data/lib/new_relic/cli/commands/deployments.rb +2 -2
  82. data/lib/new_relic/constants.rb +0 -4
  83. data/lib/new_relic/noticed_error.rb +4 -4
  84. data/lib/new_relic/version.rb +2 -2
  85. data/lib/newrelic_rpm.rb +10 -34
  86. data/lib/tasks/all.rb +1 -1
  87. data/lib/tasks/config.html.erb +14 -25
  88. data/lib/tasks/config.rake +8 -7
  89. data/newrelic.yml +594 -3
  90. data/newrelic_rpm.gemspec +1 -1
  91. data/test/agent_helper.rb +27 -2
  92. metadata +9 -9
  93. data/lib/new_relic/agent/datastores/mongo/statement_formatter.rb +0 -53
  94. data/lib/new_relic/agent/instrumentation/excon/connection.rb +0 -49
  95. data/lib/new_relic/agent/instrumentation/merb/controller.rb +0 -44
  96. data/lib/new_relic/agent/instrumentation/merb/errors.rb +0 -33
  97. data/lib/new_relic/agent/supported_versions.rb +0 -275
  98. data/lib/new_relic/control/frameworks/merb.rb +0 -29
@@ -13,12 +13,13 @@ module NewRelic
13
13
  :analytic_event_data => :'analytics_events.max_samples_stored',
14
14
  :custom_event_data => :'custom_insights_events.max_samples_stored',
15
15
  :error_event_data => :'error_collector.max_event_samples_stored',
16
- :span_event_data => :'span_events.max_samples_stored'
17
16
  }
18
17
 
19
18
  def from_config(config)
20
- {:harvest_limits => EVENT_HARVEST_CONFIG_KEY_MAPPING.inject({}) do
21
- |connect_payload, (connect_payload_key, config_key)|
19
+ {:harvest_limits =>
20
+ EVENT_HARVEST_CONFIG_KEY_MAPPING.merge(
21
+ :span_event_data => :'span_events.max_samples_stored'
22
+ ).inject({}) do |connect_payload, (connect_payload_key, config_key)|
22
23
  connect_payload[connect_payload_key] = config[config_key]
23
24
  connect_payload
24
25
  end
@@ -27,16 +28,31 @@ module NewRelic
27
28
 
28
29
  def to_config_hash(connect_reply)
29
30
  event_harvest_interval = connect_reply['event_harvest_config']['report_period_ms'] / 1000
30
- config_hash = EVENT_HARVEST_CONFIG_KEY_MAPPING.inject({}) do
31
- |event_harvest_config, (connect_payload_key, config_key)|
32
- if harvest_limit = connect_reply['event_harvest_config']['harvest_limits'][connect_payload_key.to_s]
33
- event_harvest_config[config_key] = harvest_limit
34
- report_period_key = :"event_report_period.#{connect_payload_key}"
35
- event_harvest_config[report_period_key] = event_harvest_interval
36
- end
37
- event_harvest_config
38
- end
31
+ config_hash = transform_event_harvest_config_keys(connect_reply, event_harvest_interval)
39
32
  config_hash[:event_report_period] = event_harvest_interval
33
+ config_hash = transform_span_event_harvest_config(config_hash, connect_reply)
34
+ config_hash
35
+ end
36
+
37
+ private
38
+
39
+ def transform_event_harvest_config_keys(connect_reply, event_harvest_interval)
40
+ EVENT_HARVEST_CONFIG_KEY_MAPPING.inject({}) do |event_harvest_config, (connect_payload_key, config_key)|
41
+ if harvest_limit = connect_reply['event_harvest_config']['harvest_limits'][connect_payload_key.to_s]
42
+ event_harvest_config[config_key] = harvest_limit
43
+ report_period_key = :"event_report_period.#{connect_payload_key}"
44
+ event_harvest_config[report_period_key] = event_harvest_interval
45
+ end
46
+ event_harvest_config
47
+ end
48
+ end
49
+
50
+ def transform_span_event_harvest_config(config_hash, connect_reply)
51
+ if span_harvest = connect_reply['span_event_harvest_config']
52
+ config_hash[:'span_events.max_samples_stored'] = span_harvest['harvest_limit'] if span_harvest['harvest_limit']
53
+ config_hash[:'event_report_period.span_event_data'] = span_harvest['report_period_ms'] if span_harvest['report_period_ms']
54
+ end
55
+
40
56
  config_hash
41
57
  end
42
58
  end
@@ -15,7 +15,6 @@ module NewRelic
15
15
  module Agent
16
16
  module Configuration
17
17
  class Manager
18
-
19
18
  # Defining these explicitly saves object allocations that we incur
20
19
  # if we use Forwardable and def_delegators.
21
20
  def [](key)
@@ -80,6 +79,7 @@ module NewRelic
80
79
  was_finished = finished_configuring?
81
80
 
82
81
  invoke_callbacks(:add, source)
82
+
83
83
  case source
84
84
  when SecurityPolicySource then @security_policy_source = source
85
85
  when HighSecuritySource then @high_security_source = source
@@ -159,7 +159,6 @@ module NewRelic
159
159
  def invoke_callbacks(direction, source)
160
160
  return unless source
161
161
  source.keys.each do |key|
162
-
163
162
  if @cache[key] != source[key]
164
163
  @callbacks[key].each do |proc|
165
164
  if direction == :add
@@ -79,8 +79,9 @@ module NewRelic
79
79
  :'analytics_events.max_samples_stored' => 'Supportability/EventHarvest/AnalyticEventData/HarvestLimit',
80
80
  :'custom_insights_events.max_samples_stored' => 'Supportability/EventHarvest/CustomEventData/HarvestLimit',
81
81
  :'error_collector.max_event_samples_stored'=> 'Supportability/EventHarvest/ErrorEventData/HarvestLimit',
82
- :'span_events.max_samples_stored'=> 'Supportability/EventHarvest/SpanEventData/HarvestLimit',
83
- :event_report_period => 'Supportability/EventHarvest/ReportPeriod'
82
+ :'span_events.max_samples_stored' => 'Supportability/SpanEvent/Limit',
83
+ :event_report_period => 'Supportability/EventHarvest/ReportPeriod',
84
+ :'event_report_period.span_event_data' => 'Supportability/SpanEvent/ReportPeriod',
84
85
  }
85
86
 
86
87
  def add_event_harvest_config(merged_settings, connect_reply)
@@ -11,6 +11,10 @@ module NewRelic
11
11
  attr_accessor :file_path, :failures
12
12
  attr_reader :generated_for_user, :license_key
13
13
 
14
+ # These are configuration options that have a value of a Hash
15
+ # This is used in YamlSource#dot_flattened prevent flattening these values
16
+ CONFIG_WITH_HASH_VALUE = ['expected_messages', 'ignore_messages']
17
+
14
18
  def initialize(path, env)
15
19
  @path = path
16
20
  config = {}
@@ -99,7 +103,12 @@ module NewRelic
99
103
 
100
104
  def process_yaml(file, env, config, path)
101
105
  if file
102
- confighash = YAML.load(file)
106
+ confighash = if YAML.respond_to?(:unsafe_load)
107
+ YAML.unsafe_load(file)
108
+ else
109
+ YAML.load(file)
110
+ end
111
+
103
112
  unless confighash.key?(env)
104
113
  log_failure("Config file at #{path} doesn't include a '#{env}' section!")
105
114
  end
@@ -141,6 +150,18 @@ module NewRelic
141
150
  ::NewRelic::Agent.logger.error(*messages)
142
151
  messages.each { |message| @failures << message }
143
152
  end
153
+
154
+ def dot_flattened(nested_hash, names=[], result={})
155
+ nested_hash.each do |key, val|
156
+ next if val == nil
157
+ if val.respond_to?(:has_key?) && !CONFIG_WITH_HASH_VALUE.include?(key)
158
+ dot_flattened(val, names + [key], result)
159
+ else
160
+ result[(names + [key]).join('.')] = val
161
+ end
162
+ end
163
+ result
164
+ end
144
165
  end
145
166
  end
146
167
  end
@@ -48,8 +48,10 @@ module NewRelic
48
48
  environment_report
49
49
  end
50
50
 
51
- def environment_metadata
52
- ENV.select {|k, v| k =~ /^NEW_RELIC_METADATA_/}
51
+ def environment_metadata
52
+ env_copy = {}
53
+ ENV.keys.each {|k| env_copy[k] = ENV[k] if k =~ /^NEW_RELIC_METADATA_/}
54
+ env_copy
53
55
  end
54
56
 
55
57
  def local_host
@@ -46,7 +46,8 @@ module NewRelic
46
46
 
47
47
  def create_event(type, priority, attributes)
48
48
  [
49
- { TYPE => type, TIMESTAMP => Time.now.to_i,
49
+ { TYPE => type,
50
+ TIMESTAMP => Process.clock_gettime(Process::CLOCK_REALTIME).to_i,
50
51
  PRIORITY => priority
51
52
  },
52
53
  AttributeProcessing.flatten_and_coerce(attributes)
@@ -228,9 +228,12 @@ module NewRelic
228
228
  def explain
229
229
  return unless explainable?
230
230
  handle_exception_in_explain do
231
- start = Time.now
231
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
232
232
  plan = @explainer.call(self)
233
- ::NewRelic::Agent.record_metric("Supportability/Database/execute_explain_plan", Time.now - start)
233
+ ::NewRelic::Agent.record_metric(
234
+ "Supportability/Database/execute_explain_plan",
235
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
236
+ )
234
237
  return process_resultset(plan, adapter) if plan
235
238
  end
236
239
  end
@@ -7,22 +7,17 @@ module NewRelic
7
7
  module Datastores
8
8
  module Mongo
9
9
  def self.is_supported_version?
10
- # No version constant in < 2.0 versions of Mongo :(
11
- defined?(::Mongo) && (defined?(::Mongo::MongoClient) || is_monitoring_enabled?)
12
- end
13
-
14
- def self.is_monitoring_enabled?
15
- defined?(::Mongo::Monitoring) # @since 2.1.0
10
+ defined?(::Mongo) && is_monitoring_enabled?
16
11
  end
17
12
 
18
13
  def self.is_unsupported_2x?
19
- defined?(::Mongo::VERSION) && Gem::Version.new(::Mongo::VERSION).segments[0] == 2 &&
14
+ defined?(::Mongo::VERSION) &&
15
+ Gem::Version.new(::Mongo::VERSION).segments[0] == 2 &&
20
16
  !self.is_monitoring_enabled?
21
17
  end
22
18
 
23
- def self.is_version_1_10_or_later?
24
- # Again, no VERSION constant in 1.x, so we have to rely on constant checks
25
- defined?(::Mongo::CollectionOperationWriter)
19
+ def self.is_monitoring_enabled?
20
+ defined?(::Mongo::Monitoring) # @since 2.1.0
26
21
  end
27
22
  end
28
23
  end
@@ -21,7 +21,7 @@ module NewRelic
21
21
  # query content into Transaction Traces. Use wrap if you want to provide
22
22
  # that functionality.
23
23
  #
24
- # @param [Class] clazz the class to instrument
24
+ # @param [Class] klass the class to instrument
25
25
  #
26
26
  # @param [String, Symbol] method_name the name of instance method to
27
27
  # instrument
@@ -33,16 +33,16 @@ module NewRelic
33
33
  #
34
34
  # @api public
35
35
  #
36
- def self.trace(clazz, method_name, product, operation = method_name)
36
+ def self.trace(klass, method_name, product, operation = method_name)
37
37
  NewRelic::Agent.record_api_supportability_metric(:trace)
38
38
 
39
- clazz.class_eval do
39
+ klass.class_eval do
40
40
  method_name_without_newrelic = "#{method_name}_without_newrelic"
41
41
 
42
- if NewRelic::Helper.instance_methods_include?(clazz, method_name) &&
43
- !NewRelic::Helper.instance_methods_include?(clazz, method_name_without_newrelic)
42
+ if NewRelic::Helper.instance_methods_include?(klass, method_name) &&
43
+ !NewRelic::Helper.instance_methods_include?(klass, method_name_without_newrelic)
44
44
 
45
- visibility = NewRelic::Helper.instance_method_visibility(clazz, method_name)
45
+ visibility = NewRelic::Helper.instance_method_visibility(klass, method_name)
46
46
 
47
47
  alias_method method_name_without_newrelic, method_name
48
48
 
@@ -123,7 +123,7 @@ module NewRelic
123
123
  ensure
124
124
  begin
125
125
  if callback
126
- elapsed_time = (Time.now - segment.start_time).to_f
126
+ elapsed_time = Process.clock_gettime(Process::CLOCK_REALTIME) - segment.start_time
127
127
  callback.call(result, segment.name, elapsed_time)
128
128
  end
129
129
  ensure
@@ -19,15 +19,15 @@ module NewRelic
19
19
  end
20
20
 
21
21
  def as_json_array(content_length)
22
- queue_time_in_seconds = [transaction.queue_time.to_f, 0.0].max
23
- start_time_in_seconds = [transaction.start_time.to_f, 0.0].max
24
- app_time_in_seconds = Time.now.to_f - start_time_in_seconds
22
+ queue_time_in_seconds = [transaction.queue_time, 0.0].max
23
+ start_time_in_seconds = [transaction.start_time, 0.0].max
24
+ app_time_in_seconds = Process.clock_gettime(Process::CLOCK_REALTIME) - start_time_in_seconds
25
25
 
26
26
  [
27
27
  NewRelic::Agent.config[:cross_process_id],
28
28
  transaction.best_name,
29
- queue_time_in_seconds.to_f,
30
- app_time_in_seconds.to_f,
29
+ queue_time_in_seconds,
30
+ app_time_in_seconds,
31
31
  content_length,
32
32
  transaction.guid,
33
33
  false
@@ -54,6 +54,7 @@ module NewRelic
54
54
 
55
55
  def insert_cross_app_header headers
56
56
  return unless CrossAppTracing.cross_app_enabled?
57
+
57
58
  @is_cross_app_caller = true
58
59
  txn_guid = transaction.guid
59
60
  trip_id = cat_trip_id
@@ -65,17 +66,20 @@ module NewRelic
65
66
  def add_message_cat_headers headers
66
67
  return unless CrossAppTracing.cross_app_enabled?
67
68
  @is_cross_app_caller = true
68
- insert_message_headers headers,
69
- transaction.guid,
70
- cat_trip_id,
71
- cat_path_hash,
69
+ insert_message_headers headers,
70
+ transaction.guid,
71
+ cat_trip_id,
72
+ cat_path_hash,
72
73
  transaction.raw_synthetics_header
73
74
  end
74
75
 
75
76
  def record_cross_app_metrics
76
77
  if (id = cross_app_payload && cross_app_payload.id)
77
- app_time_in_seconds = [Time.now.to_f - transaction.start_time.to_f, 0.0].max
78
- NewRelic::Agent.record_metric "ClientApplication/#{id}/all", app_time_in_seconds
78
+ app_time_in_seconds = [
79
+ Process.clock_gettime(Process::CLOCK_REALTIME) - transaction.start_time,
80
+ 0.0
81
+ ].max
82
+ NewRelic::Agent.record_metric("ClientApplication/#{id}/all", app_time_in_seconds)
79
83
  end
80
84
  end
81
85
 
@@ -64,7 +64,6 @@ module NewRelic
64
64
  destination[PARENT_TYPE_KEY] = trace_payload.parent_type
65
65
  destination[PARENT_APP_KEY] = trace_payload.parent_app_id
66
66
  destination[PARENT_ACCOUNT_ID_KEY] = trace_payload.parent_account_id
67
-
68
67
  destination[PARENT_TRANSPORT_DURATION_KEY] = transaction.calculate_transport_duration trace_payload
69
68
 
70
69
  if parent_transaction_id = transaction.distributed_tracer.parent_transaction_id
@@ -50,7 +50,7 @@ module NewRelic
50
50
 
51
51
  payload.id = current_segment_id(transaction)
52
52
  payload.transaction_id = transaction.guid
53
- payload.timestamp = (Time.now.to_f * 1000).round
53
+ payload.timestamp = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
54
54
  payload.trace_id = transaction.trace_id
55
55
  payload.sampled = transaction.sampled?
56
56
  payload.priority = transaction.priority
@@ -57,7 +57,7 @@ module NewRelic
57
57
  private
58
58
 
59
59
  def now_ms
60
- (Time.now.to_f * 1000).round
60
+ Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
61
61
  end
62
62
 
63
63
  def handle_invalid_payload error: nil, message: nil
@@ -22,23 +22,18 @@ module NewRelic
22
22
  @error_trace_aggregator = ErrorTraceAggregator.new(MAX_ERROR_QUEUE_LENGTH)
23
23
  @error_event_aggregator = ErrorEventAggregator.new events
24
24
 
25
- # lookup of exception class names to ignore. Hash for fast access
26
- @ignore = {}
27
-
28
- initialize_ignored_errors(Agent.config[:'error_collector.ignore_errors'])
29
-
30
- Agent.config.register_callback(:'error_collector.ignore_errors') do |ignore_errors|
31
- initialize_ignored_errors(ignore_errors)
25
+ @error_filter = NewRelic::Agent::ErrorFilter.new
26
+
27
+ %w(
28
+ ignore_errors ignore_classes ignore_messages ignore_status_codes
29
+ expected_classes expected_messages expected_status_codes
30
+ ).each do |w|
31
+ Agent.config.register_callback(:"error_collector.#{w}") do |value|
32
+ @error_filter.load_from_config(w, value)
33
+ end
32
34
  end
33
35
  end
34
36
 
35
- def initialize_ignored_errors(ignore_errors)
36
- @ignore.clear
37
- ignore_errors = ignore_errors.split(",") if ignore_errors.is_a? String
38
- ignore_errors.each { |error| error.strip! }
39
- ignore(ignore_errors)
40
- end
41
-
42
37
  def enabled?
43
38
  error_trace_aggregator.enabled? || error_event_aggregator.enabled?
44
39
  end
@@ -76,30 +71,39 @@ module NewRelic
76
71
  defined?(@ignore_filter) ? @ignore_filter : nil
77
72
  end
78
73
 
79
- # errors is an array of Exception Class Names
80
- #
81
74
  def ignore(errors)
82
- errors.each do |error|
83
- @ignore[error] = true
84
- ::NewRelic::Agent.logger.debug("Ignoring errors of type '#{error}'")
85
- end
75
+ @error_filter.ignore(errors)
76
+ end
77
+
78
+ def ignore?(ex, status_code = nil)
79
+ @error_filter.ignore?(ex, status_code)
80
+ end
81
+
82
+ def expect(errors)
83
+ @error_filter.expect(errors)
84
+ end
85
+
86
+ def expected?(ex, status_code = nil)
87
+ @error_filter.expected?(ex, status_code)
88
+ end
89
+
90
+ def load_error_filters
91
+ @error_filter.load_all
92
+ end
93
+
94
+ def reset_error_filters
95
+ @error_filter.reset
86
96
  end
87
97
 
88
98
  # Checks the provided error against the error filter, if there
89
99
  # is an error filter
90
- def filtered_by_error_filter?(error)
100
+ def ignored_by_filter_proc?(error)
91
101
  respond_to?(:ignore_filter_proc) && !ignore_filter_proc(error)
92
102
  end
93
103
 
94
- # Checks the array of error names and the error filter against
95
- # the provided error
96
- def filtered_error?(error)
97
- @ignore[error.class.name] || filtered_by_error_filter?(error)
98
- end
99
-
100
104
  # an error is ignored if it is nil or if it is filtered
101
- def error_is_ignored?(error)
102
- error && filtered_error?(error)
105
+ def error_is_ignored?(error, status_code = nil)
106
+ error && (@error_filter.ignore?(error, status_code) || ignored_by_filter_proc?(error))
103
107
  rescue => e
104
108
  NewRelic::Agent.logger.error("Error '#{error}' will NOT be ignored. Exception '#{e}' while determining whether to ignore or not.", e)
105
109
  false
@@ -174,11 +178,18 @@ module NewRelic
174
178
  end
175
179
  end
176
180
 
177
- def skip_notice_error?(exception)
181
+ def increment_expected_error_count!(state, exception)
182
+ stats_engine = NewRelic::Agent.agent.stats_engine
183
+ stats_engine.record_unscoped_metrics(state, ['ErrorsExpected/all']) do |stats|
184
+ stats.increment_count
185
+ end
186
+ end
187
+
188
+ def skip_notice_error?(exception, status_code = nil)
178
189
  disabled? ||
179
- error_is_ignored?(exception) ||
180
190
  exception.nil? ||
181
- exception_tagged_with?(EXCEPTION_TAG_IVAR, exception)
191
+ exception_tagged_with?(EXCEPTION_TAG_IVAR, exception) ||
192
+ error_is_ignored?(exception, status_code)
182
193
  end
183
194
 
184
195
  # calls a method on an object, if it responds to it - used for
@@ -210,13 +221,17 @@ module NewRelic
210
221
 
211
222
  # See NewRelic::Agent.notice_error for options and commentary
212
223
  def notice_error(exception, options={}, span_id=nil)
213
- return if skip_notice_error?(exception)
224
+ state = ::NewRelic::Agent::Tracer.state
225
+ transaction = state.current_transaction
226
+ status_code = transaction ? transaction.http_response_code : nil
214
227
 
215
- tag_exception(exception)
228
+ return if skip_notice_error?(exception, status_code)
216
229
 
217
- state = ::NewRelic::Agent::Tracer.state
230
+ tag_exception(exception)
218
231
 
219
- unless options[:expected]
232
+ if options[:expected] || @error_filter.expected?(exception, status_code)
233
+ increment_expected_error_count!(state, exception)
234
+ else
220
235
  increment_error_count!(state, exception, options)
221
236
  end
222
237
 
@@ -258,7 +273,7 @@ module NewRelic
258
273
  noticed_error.line_number = sense_method(exception, :line_number)
259
274
  noticed_error.stack_trace = truncate_trace(extract_stack_trace(exception))
260
275
 
261
- noticed_error.expected = !! options.delete(:expected)
276
+ noticed_error.expected = !!options.delete(:expected) || expected?(exception)
262
277
 
263
278
  noticed_error.attributes_from_notice_error = options.delete(:custom_params) || {}
264
279
 
@@ -0,0 +1,175 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
4
+
5
+ module NewRelic
6
+ module Agent
7
+
8
+ # Handles loading of ignored and expected errors from the agent configuration, and
9
+ # determining at runtime whether an exception is ignored or expected.
10
+ class ErrorFilter
11
+
12
+ def initialize
13
+ reset
14
+ end
15
+
16
+ def reset
17
+ @ignore_classes, @expected_classes = [], []
18
+ @ignore_messages, @expected_messages = {}, {}
19
+ @ignore_status_codes, @expected_status_codes = [], []
20
+ end
21
+
22
+ def load_all
23
+ %i(
24
+ ignore_errors ignore_classes ignore_messages ignore_status_codes
25
+ expected_classes expected_messages expected_status_codes
26
+ ).each { |setting| load_from_config(setting) }
27
+ end
28
+
29
+ def load_from_config(setting, value = nil)
30
+ errors = nil
31
+ new_value = value || fetch_agent_config(setting.to_sym)
32
+
33
+ return if new_value.nil? || (new_value.respond_to?(:empty?) && new_value.empty?)
34
+
35
+ case setting.to_sym
36
+ when :ignore_errors, :ignore_classes
37
+ new_value = new_value.split(',').map!(&:strip) if new_value.is_a?(String)
38
+ errors = @ignore_classes = new_value
39
+ when :ignore_messages
40
+ errors = @ignore_messages = new_value || {}
41
+ when :ignore_status_codes
42
+ errors = @ignore_status_codes = parse_status_codes(new_value) || []
43
+ when :expected_classes
44
+ errors = @expected_classes = new_value || []
45
+ when :expected_messages
46
+ errors = @expected_messages = new_value || {}
47
+ when :expected_status_codes
48
+ errors = @expected_status_codes = parse_status_codes(new_value) || []
49
+ end
50
+ log_filter(setting, errors) if errors
51
+ end
52
+
53
+ def ignore?(ex, status_code = nil)
54
+ @ignore_classes.include?(ex.class.name) ||
55
+ (@ignore_messages.keys.include?(ex.class.name) &&
56
+ @ignore_messages[ex.class.name].any? { |m| ex.message.include?(m) }) ||
57
+ @ignore_status_codes.include?(status_code.to_i)
58
+ end
59
+
60
+ def expected?(ex, status_code = nil)
61
+ @expected_classes.include?(ex.class.name) ||
62
+ (@expected_messages.keys.include?(ex.class.name) &&
63
+ @expected_messages[ex.class.name].any? { |m| ex.message.include?(m) }) ||
64
+ @expected_status_codes.include?(status_code.to_i)
65
+ end
66
+
67
+ def fetch_agent_config(cfg)
68
+ NewRelic::Agent.config[:"error_collector.#{cfg}"]
69
+ end
70
+
71
+ # A generic method for adding ignore filters manually. This is kept for compatibility
72
+ # with the previous ErrorCollector#ignore method, and adds some flexibility for adding
73
+ # different ignore/expected error types by examining each argument.
74
+ def ignore(*args)
75
+ args.each do |errors|
76
+ case errors
77
+ when Array
78
+ errors.each { |e| ignore(e) }
79
+ when Integer
80
+ @ignore_status_codes << errors
81
+ when Hash
82
+ @ignore_messages.update(errors)
83
+ log_filter(:ignore_messages, errors)
84
+ when String
85
+ if errors.match(/^[\d\,\-]+$/)
86
+ @ignore_status_codes |= parse_status_codes(errors)
87
+ log_filter(:ignore_status_codes, errors)
88
+ else
89
+ new_ignore_classes = errors.split(',').map!(&:strip)
90
+ @ignore_classes |= new_ignore_classes
91
+ log_filter(:ignore_classes, new_ignore_classes)
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ # See #ignore above.
98
+ def expect(*args)
99
+ args.each do |errors|
100
+ case errors
101
+ when Array
102
+ errors.each { |e| expect(e) }
103
+ when Integer
104
+ @expected_status_codes << errors
105
+ when Hash
106
+ @expected_messages.update(errors)
107
+ log_filter(:expected_messages, errors)
108
+ when String
109
+ if errors.match(/^[\d\,\-]+$/)
110
+ @expected_status_codes |= parse_status_codes(errors)
111
+ log_filter(:expected_status_codes, errors)
112
+ else
113
+ new_expected_classes = errors.split(',').map!(&:strip)
114
+ @expected_classes |= new_expected_classes
115
+ log_filter(:expected_classes, new_expected_classes)
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ private
122
+
123
+ def log_filter(setting, errors)
124
+ case setting
125
+ when :ignore_errors, :ignore_classes
126
+ errors.each do |error|
127
+ ::NewRelic::Agent.logger.debug("Ignoring errors of type '#{error}'")
128
+ end
129
+ when :ignore_messages
130
+ errors.each do |error,messages|
131
+ ::NewRelic::Agent.logger.debug("Ignoring errors of type '#{error}' with messages: #{messages.join(',')}")
132
+ end
133
+ when :ignore_status_codes
134
+ ::NewRelic::Agent.logger.debug("Ignoring errors associated with status codes: #{errors}")
135
+ when :expected_classes
136
+ errors.each do |error|
137
+ ::NewRelic::Agent.logger.debug("Expecting errors of type '#{error}'")
138
+ end
139
+ when :expected_messages
140
+ errors.each do |error,messages|
141
+ ::NewRelic::Agent.logger.debug("Expecting errors of type '#{error}' with messages: #{messages.join(',')}")
142
+ end
143
+ when :expected_status_codes
144
+ ::NewRelic::Agent.logger.debug("Expecting errors associated with status codes: #{errors}")
145
+ end
146
+ end
147
+
148
+ def parse_status_codes(codes)
149
+ code_list = case codes
150
+ when String
151
+ codes.split(',')
152
+ when Integer
153
+ [codes]
154
+ else
155
+ codes
156
+ end
157
+ result = []
158
+ code_list.each do |code|
159
+ result << code && next if code.is_a?(Integer)
160
+ m = code.match(/(\d{3})(-\d{3})?/)
161
+ if m.nil? || m[1].nil?
162
+ ::NewRelic::Agent.logger.warn("Invalid HTTP status code: '#{code}'; ignoring config")
163
+ next
164
+ end
165
+ if m[2]
166
+ result += (m[1]..m[2].tr('-', '')).to_a.map(&:to_i)
167
+ else
168
+ result << m[1].to_i
169
+ end
170
+ end
171
+ result.uniq
172
+ end
173
+ end
174
+ end
175
+ end