Class: NewRelic::Agent::RulesEngine::SegmentTermsRule

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

Constant Summary collapse

PREFIX_KEY =
'prefix'.freeze
TERMS_KEY =
'terms'.freeze
SEGMENT_PLACEHOLDER =
'*'.freeze
ADJACENT_PLACEHOLDERS_REGEX =
%r{((?:^|/)\*)(?:/\*)*}.freeze
ADJACENT_PLACEHOLDERS_REPLACEMENT =
'\1'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ SegmentTermsRule

Returns a new instance of SegmentTermsRule.



31
32
33
34
35
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 31

def initialize(options)
  @prefix = options[PREFIX_KEY]
  @terms = options[TERMS_KEY]
  @trim_range = (@prefix.size..-1)
end

Instance Attribute Details

#prefixObject (readonly)



15
16
17
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 15

def prefix
  @prefix
end

#termsObject (readonly)



15
16
17
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 15

def terms
  @terms
end

Class Method Details

.valid?(rule_spec) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 17

def self.valid?(rule_spec)
  rule_spec[PREFIX_KEY].kind_of?(String) &&
    rule_spec[TERMS_KEY].kind_of?(Array) &&
    valid_prefix_segment_count?(rule_spec[PREFIX_KEY])
end

.valid_prefix_segment_count?(prefix) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 23

def self.valid_prefix_segment_count?(prefix)
  count = prefix.count(SEGMENT_SEPARATOR)
  rindex = prefix.rindex(SEGMENT_SEPARATOR)

  (count == 2 && prefix[rindex + 1].nil?) ||
    (count == 1 && !prefix[rindex + 1].nil?)
end

Instance Method Details

#apply(string) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 51

def apply(string)
  rest = string[@trim_range]
  leading_slash = rest.slice!(LEADING_SLASH_REGEX)
  segments = rest.split(SEGMENT_SEPARATOR, -1)
  segments.map! { |s| @terms.include?(s) ? s : SEGMENT_PLACEHOLDER }
  transformed_suffix = collapse_adjacent_placeholder_segments(segments)

  "#{@prefix}#{leading_slash}#{transformed_suffix}"
end

#collapse_adjacent_placeholder_segments(segments) ⇒ Object



61
62
63
64
65
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 61

def collapse_adjacent_placeholder_segments(segments)
  joined = segments.join(SEGMENT_SEPARATOR)
  joined.gsub!(ADJACENT_PLACEHOLDERS_REGEX, ADJACENT_PLACEHOLDERS_REPLACEMENT)
  joined
end

#matches?(string) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 41

def matches?(string)
  string.start_with?(@prefix) &&
    (prefix_matches_on_segment_boundary?(string) || string.size == @prefix.size)
end

#prefix_matches_on_segment_boundary?(string) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 46

def prefix_matches_on_segment_boundary?(string)
  string.size > @prefix.size &&
    string[@prefix.chomp(SEGMENT_SEPARATOR).size].chr == SEGMENT_SEPARATOR
end

#terminal?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/new_relic/agent/rules_engine/segment_terms_rule.rb', line 37

def terminal?
  true
end