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

Constant Summary collapse

MAX_LENGTH =

the maximum length of a metric name or metric scope

255
LENGTH_RANGE =
(0...MAX_LENGTH)
EMPTY_SCOPE =
''.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MetricSpec.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/new_relic/metric_spec.rb', line 15

def initialize(metric_name = '', metric_scope = nil)
  if metric_name.to_s.length > MAX_LENGTH
    @name = metric_name.to_s[LENGTH_RANGE]
  else
    @name = metric_name.to_s
  end

  if metric_scope
    if metric_scope.to_s.length > MAX_LENGTH
      @scope = metric_scope.to_s[LENGTH_RANGE]
    else
      @scope = metric_scope.to_s
    end
  else
    @scope = EMPTY_SCOPE
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#<=>(o) ⇒ Object



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

def <=>(o)
  namecmp = self.name <=> o.name
  return namecmp if namecmp != 0

  return (self.scope || '') <=> (o.scope || '')
end

#==(o) ⇒ Object



33
34
35
# File 'lib/new_relic/metric_spec.rb', line 33

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(o)
  @name == o.name && @scope == o.scope
end

#hashObject



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

def hash
  [@name, @scope].hash
end

#inspectObject



51
52
53
# File 'lib/new_relic/metric_spec.rb', line 51

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

#to_json(*a) ⇒ Object



55
56
57
58
# File 'lib/new_relic/metric_spec.rb', line 55

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

#to_sObject



45
46
47
48
49
# File 'lib/new_relic/metric_spec.rb', line 45

def to_s
  return name if scope.empty?

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