Class: NewRelic::MetricSpec

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

Overview

this struct uniquely defines a metric, optionally inside the call scope of another metric

Defined Under Namespace

Classes: InvalidScopeSettingError

Constant Summary collapse

MAX_LENGTH =

the maximum length of a metric name or metric scope

255
LENGTH_RANGE =
(0...MAX_LENGTH)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric_name = '', metric_scope = nil) ⇒ MetricSpec

Need a “zero-arg” constructor so it can be instantiated from java (using jruby) for sending responses to ruby agents from the java collector.



19
20
21
22
23
24
25
26
# File 'lib/new_relic/metric_spec.rb', line 19

def initialize(metric_name = '', metric_scope = nil)
  self.name = (metric_name || '') && metric_name.to_s[LENGTH_RANGE]
  if metric_scope
    self.scope = metric_scope && metric_scope.to_s[LENGTH_RANGE]
  else
    self.scope = ''
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#scopeObject

Returns the value of attribute scope.



11
12
13
# File 'lib/new_relic/metric_spec.rb', line 11

def scope
  @scope
end

Instance Method Details

#<=>(o) ⇒ Object



95
96
97
98
99
# File 'lib/new_relic/metric_spec.rb', line 95

def <=>(o)
  namecmp = self.name <=> o.name
  return namecmp if namecmp != 0
  return (self.scope || '') <=> (o.scope || '')
end

#==(o) ⇒ Object



48
49
50
# File 'lib/new_relic/metric_spec.rb', line 48

def ==(o)
  self.eql?(o)
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/new_relic/metric_spec.rb', line 52

def eql? o
  self.class == o.class &&
  name.eql?(o.name) &&
  # coerce scope to a string and compare
   scope.to_s == o.scope.to_s
end

#hashObject



59
60
61
62
63
# File 'lib/new_relic/metric_spec.rb', line 59

def hash
  h = name.hash
  h ^= scope.hash unless scope.nil?
  h
end

#inspectObject



86
87
88
# File 'lib/new_relic/metric_spec.rb', line 86

def inspect
  "#<NewRelic::MetricSpec '#{name}':'#{scope}'>"
end

#sub(pattern, replacement, apply_to_scope = true) ⇒ Object

return a new metric spec if the given regex matches the name or scope.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/new_relic/metric_spec.rb', line 66

def sub(pattern, replacement, apply_to_scope = true)
  ::NewRelic::Agent.logger.warn("The sub method on metric specs is deprecated") rescue nil
  return nil if name !~ pattern &&
   (!apply_to_scope || scope.nil? || scope !~ pattern)
  new_name = name.sub(pattern, replacement)[LENGTH_RANGE]

  if apply_to_scope
    new_scope = (scope && scope.sub(pattern, replacement)[LENGTH_RANGE])
  else
    new_scope = scope
  end

  self.class.new new_name, new_scope
end

#to_json(*a) ⇒ Object



90
91
92
93
# File 'lib/new_relic/metric_spec.rb', line 90

def to_json(*a)
  {'name' => name,
  'scope' => scope}.to_json(*a)
end

#to_sObject



81
82
83
84
# File 'lib/new_relic/metric_spec.rb', line 81

def to_s
  return name if scope.empty?
  "#{name}:#{scope}"
end

#truncate!Object

truncates the name and scope to the MAX_LENGTH



43
44
45
46
# File 'lib/new_relic/metric_spec.rb', line 43

def truncate!
  self.name = name[LENGTH_RANGE] if name && name.size > MAX_LENGTH
  self.scope = scope[LENGTH_RANGE] if scope && scope.size > MAX_LENGTH
end