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
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.



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

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

Instance Attribute Details

#prefixObject (readonly)



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

def prefix
  @prefix
end

#termsObject (readonly)



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

def terms
  @terms
end

Class Method Details

.valid?(rule_spec) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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



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

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 : ASTERISK }
  transformed_suffix = collapse_adjacent_placeholder_segments(segments)

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

#collapse_adjacent_placeholder_segments(segments) ⇒ Object



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

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)


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

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)


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

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)


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

def terminal?
  true
end