Class: NewRelic::MetricSpec

Inherits:
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MetricSpec.



7
8
9
10
# File 'lib/new_relic/metric_spec.rb', line 7

def initialize (metric_name, metric_scope = '')
  self.name = metric_name
  self.scope = metric_scope
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/new_relic/metric_spec.rb', line 4

def name
  @name
end

#scopeObject

Returns the value of attribute scope.



5
6
7
# File 'lib/new_relic/metric_spec.rb', line 5

def scope
  @scope
end

Instance Method Details

#<=>(o) ⇒ Object



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

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

#==(o) ⇒ Object



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

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/new_relic/metric_spec.rb', line 16

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

#hashObject



23
24
25
26
27
# File 'lib/new_relic/metric_spec.rb', line 23

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

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

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



30
31
32
33
34
35
36
# File 'lib/new_relic/metric_spec.rb', line 30

def sub(pattern, replacement, apply_to_scope = true)
  return nil if name !~ pattern && 
                (!apply_to_scope || scope.nil? || scope !~ pattern)
  new_name = name.sub(pattern, replacement)
  new_scope = (scope && scope.sub(pattern, replacement)) 
  self.class.new new_name, new_scope
end

#to_json(*a) ⇒ Object



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

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

#to_sObject



38
39
40
# File 'lib/new_relic/metric_spec.rb', line 38

def to_s
  "#{name}:#{scope}"
end