Class: NewRelic::Agent::RulesEngine

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/new_relic/agent/rules_engine.rb,
lib/new_relic/agent/rules_engine/replacement_rule.rb,
lib/new_relic/agent/rules_engine/segment_terms_rule.rb

Defined Under Namespace

Classes: ReplacementRule, SegmentTermsRule

Constant Summary collapse

SEGMENT_SEPARATOR =
'/'.freeze
LEADING_SLASH_REGEX =
%r{^/}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules = [], segment_term_rules = []) ⇒ RulesEngine

Returns a new instance of RulesEngine.



57
58
59
60
# File 'lib/new_relic/agent/rules_engine.rb', line 57

def initialize(rules = [], segment_term_rules = [])
  @rules = rules.sort
  @segment_term_rules = segment_term_rules
end

Class Method Details

.create_metric_rules(connect_response) ⇒ Object



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

def self.create_metric_rules(connect_response)
  specs = connect_response['metric_name_rules'] || []
  rules = specs.map { |spec| ReplacementRule.new(spec) }
  self.new(rules)
end

.create_transaction_rules(connect_response) ⇒ Object



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

def self.create_transaction_rules(connect_response)
  txn_name_specs = connect_response['transaction_name_rules'] || []
  segment_rule_specs = connect_response['transaction_segment_terms'] || []

  txn_name_rules = txn_name_specs.map { |s| ReplacementRule.new(s) }

  segment_rules = []

  segment_rule_specs.each do |spec|
    if spec[SegmentTermsRule::PREFIX_KEY] && SegmentTermsRule.valid?(spec)
      # Build segment_rules in reverse order from which they're provided,
      # so that when we eliminate duplicates with #uniq!, we retain the last
      # instances of repeated rules.
      segment_rules.unshift(SegmentTermsRule.new(spec))
    end
  end

  reject_rules_with_duplicate_prefixes!(segment_rules)

  segment_rules.reverse! # Reset the rules to their original order.

  self.new(txn_name_rules, segment_rules)
end

.reject_rules_with_duplicate_prefixes!(rules) ⇒ Object

When multiple rules share the same prefix, only apply the rule with the last instance of the prefix. Note that the incoming rules are in reverse order to facilitate this.



53
54
55
# File 'lib/new_relic/agent/rules_engine.rb', line 53

def self.reject_rules_with_duplicate_prefixes!(rules)
  rules.uniq! { |rule| rule.prefix }
end

Instance Method Details

#apply_rules(rules, string) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/new_relic/agent/rules_engine.rb', line 62

def apply_rules(rules, string)
  rules.each do |rule|
    if rule.matches?(string)
      string = rule.apply(string)
      break if rule.terminal?
    end
  end
  string
end

#rename(original_string) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/new_relic/agent/rules_engine.rb', line 72

def rename(original_string)
  renamed = apply_rules(@rules, original_string)
  return nil unless renamed

  renamed = apply_rules(@segment_term_rules, renamed)
  renamed
end