Module: Datadog::ErrorTracking::Filters Private

Defined in:
lib/datadog/error_tracking/filters.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Based on configuration, the TracePoint listening to :rescue or :raise may report more handled errors than we want to report. Therefore we need a function to filter the events. As the filter function both depends on configuration and is called numerous time, we generate it during during the initialization of the feature to have the best performance possible.

Class Method Summary collapse

Class Method Details

.datadog_code?(file_path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


34
35
36
# File 'lib/datadog/error_tracking/filters.rb', line 34

def datadog_code?(file_path)
  file_path.include?('lib/datadog/')
end

.file_included?(file_path, instrumented_files) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


43
44
45
# File 'lib/datadog/error_tracking/filters.rb', line 43

def file_included?(file_path, instrumented_files)
  instrumented_files.include?(file_path)
end

.generate_filter(to_instrument_scope, handled_errors_include = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generate the proc used in the TracePoint



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/datadog/error_tracking/filters.rb', line 48

def generate_filter(to_instrument_scope, handled_errors_include = nil)
  case to_instrument_scope
  # If DD_ERROR_TRACKING_HANDLED_ERRORS is set
  when 'all'
    proc { |file_path| !datadog_code?(file_path) }
  when 'user'
    # If DD_ERROR_TRACKING_HANDLED_ERRORS_INCLUDE is set
    if handled_errors_include
      proc { |file_path|
        user_code?(file_path) || file_included?(file_path, handled_errors_include)
      }
    else
      proc { |file_path| user_code?(file_path) }
    end
  when 'third_party'
    if handled_errors_include
      proc { |file_path|
        third_party_code?(file_path) || file_included?(file_path, handled_errors_include)
      }
    else
      proc { |file_path| third_party_code?(file_path) }
    end
  else
    # If only DD_ERROR_TRACKING_HANDLED_ERRORS_INCLUDE is set
    proc { |file_path| file_included?(file_path, handled_errors_include) }
  end
end

.get_gem_name(file_path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/datadog/error_tracking/filters.rb', line 16

def get_gem_name(file_path)
  regex = %r{gems/([^/]+)-\d}
  regex_match = regex.match(file_path)
  return unless regex_match

  gem_name = regex_match[1]

  begin
    Gem::Specification.find_by_name(gem_name) # steep:ignore
  rescue Gem::MissingSpecError
    nil
  end
end

.third_party_code?(file_path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/datadog/error_tracking/filters.rb', line 38

def third_party_code?(file_path)
  gem_name = get_gem_name(file_path)
  gem_name && gem_name != "datadog"
end

.user_code?(file_path) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


30
31
32
# File 'lib/datadog/error_tracking/filters.rb', line 30

def user_code?(file_path)
  !get_gem_name(file_path)
end