Class: NewRelic::Agent::ErrorFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/error_filter.rb

Overview

Handles loading of ignored and expected errors from the agent configuration, and determining at runtime whether an exception is ignored or expected.

Instance Method Summary collapse

Constructor Details

#initializeErrorFilter

Returns a new instance of ErrorFilter.



10
11
12
# File 'lib/new_relic/agent/error_filter.rb', line 10

def initialize
  reset
end

Instance Method Details

#expect(*args) ⇒ Object

See #ignore above.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/new_relic/agent/error_filter.rb', line 96

def expect(*args)
  args.each do |errors|
    case errors
    when Array
      errors.each { |e| expect(e) }
    when Integer
      @expected_status_codes << errors
    when Hash
      @expected_messages.update(errors)
      log_filter(:expected_messages, errors)
    when String
      if /^[\d\,\-]+$/.match?(errors)
        @expected_status_codes |= parse_status_codes(errors)
        log_filter(:expected_status_codes, errors)
      else
        new_expected_classes = errors.split(',').map!(&:strip)
        @expected_classes |= new_expected_classes
        log_filter(:expected_classes, new_expected_classes)
      end
    end
  end
end

#expected?(ex, status_code = nil) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/new_relic/agent/error_filter.rb', line 58

def expected?(ex, status_code = nil)
  @expected_classes.include?(ex.class.name) ||
    (@expected_messages.key?(ex.class.name) &&
    @expected_messages[ex.class.name].any? { |m| ex.message.include?(m) }) ||
    @expected_status_codes.include?(status_code.to_i)
end

#fetch_agent_config(cfg) ⇒ Object



65
66
67
# File 'lib/new_relic/agent/error_filter.rb', line 65

def fetch_agent_config(cfg)
  NewRelic::Agent.config[:"error_collector.#{cfg}"]
end

#ignore(*args) ⇒ Object

A generic method for adding ignore filters manually. This is kept for compatibility with the previous ErrorCollector#ignore method, and adds some flexibility for adding different ignore/expected error types by examining each argument.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/new_relic/agent/error_filter.rb', line 72

def ignore(*args)
  args.each do |errors|
    case errors
    when Array
      errors.each { |e| ignore(e) }
    when Integer
      @ignore_status_codes << errors
    when Hash
      @ignore_messages.update(errors)
      log_filter(:ignore_messages, errors)
    when String
      if /^[\d\,\-]+$/.match?(errors)
        @ignore_status_codes |= parse_status_codes(errors)
        log_filter(:ignore_status_codes, errors)
      else
        new_ignore_classes = errors.split(',').map!(&:strip)
        @ignore_classes |= new_ignore_classes
        log_filter(:ignore_classes, new_ignore_classes)
      end
    end
  end
end

#ignore?(ex, status_code = nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
# File 'lib/new_relic/agent/error_filter.rb', line 51

def ignore?(ex, status_code = nil)
  @ignore_classes.include?(ex.class.name) ||
    (@ignore_messages.key?(ex.class.name) &&
    @ignore_messages[ex.class.name].any? { |m| ex.message.include?(m) }) ||
    @ignore_status_codes.include?(status_code.to_i)
end

#load_allObject



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

def load_all
  %i[
    ignore_classes ignore_messages ignore_status_codes
    expected_classes expected_messages expected_status_codes
  ].each { |setting| load_from_config(setting) }
end

#load_from_config(setting, value = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/new_relic/agent/error_filter.rb', line 27

def load_from_config(setting, value = nil)
  errors = nil
  new_value = value || fetch_agent_config(setting.to_sym)

  return if new_value.nil? || (new_value.respond_to?(:empty?) && new_value.empty?)

  case setting.to_sym
  when :ignore_classes
    new_value = new_value.split(',').map!(&:strip) if new_value.is_a?(String)
    errors = @ignore_classes = new_value
  when :ignore_messages
    errors = @ignore_messages = new_value || {}
  when :ignore_status_codes
    errors = @ignore_status_codes = parse_status_codes(new_value) || []
  when :expected_classes
    errors = @expected_classes = new_value || []
  when :expected_messages
    errors = @expected_messages = new_value || {}
  when :expected_status_codes
    errors = @expected_status_codes = parse_status_codes(new_value) || []
  end
  log_filter(setting, errors) if errors
end

#resetObject



14
15
16
17
18
# File 'lib/new_relic/agent/error_filter.rb', line 14

def reset
  @ignore_classes, @expected_classes = [], []
  @ignore_messages, @expected_messages = {}, {}
  @ignore_status_codes, @expected_status_codes = [], []
end