Class: TingYun::Metrics::MetricSpec

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

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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ting_yun/metrics/metric_spec.rb', line 18

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

#calleeIdObject

Returns the value of attribute calleeId.



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

def calleeId
  @calleeId
end

#calleeNameObject

Returns the value of attribute calleeName.



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

def calleeName
  @calleeName
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#<=>(o) ⇒ Object



79
80
81
82
83
# File 'lib/ting_yun/metrics/metric_spec.rb', line 79

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

#==(o) ⇒ Object



36
37
38
# File 'lib/ting_yun/metrics/metric_spec.rb', line 36

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

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/ting_yun/metrics/metric_spec.rb', line 40

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

#hashObject



44
45
46
# File 'lib/ting_yun/metrics/metric_spec.rb', line 44

def hash
  @name.hash ^ @scope.hash
end

#inspectObject



70
71
72
# File 'lib/ting_yun/metrics/metric_spec.rb', line 70

def inspect
  "#<TingYun::Metrics::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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ting_yun/metrics/metric_spec.rb', line 50

def sub(pattern, replacement, apply_to_scope = true)
  ::TingYun::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



74
75
76
77
# File 'lib/ting_yun/metrics/metric_spec.rb', line 74

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

#to_sObject



65
66
67
68
# File 'lib/ting_yun/metrics/metric_spec.rb', line 65

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