Class: Spectator::MeterId

Inherits:
Object
  • Object
show all
Defined in:
lib/spectator/meter_id.rb

Overview

Identifier for a meter or Measure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, maybe_tags = nil) ⇒ MeterId

Returns a new instance of MeterId.



5
6
7
8
9
10
11
12
# File 'lib/spectator/meter_id.rb', line 5

def initialize(name, maybe_tags = nil)
  tags = maybe_tags.nil? ? {} : maybe_tags
  @name = name.to_sym
  @tags = {}
  tags.each { |k, v| @tags[k.to_sym] = v.to_sym }
  @tags.freeze
  @key = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/spectator/meter_id.rb', line 4

def name
  @name
end

#tagsObject (readonly)

Returns the value of attribute tags.



4
5
6
# File 'lib/spectator/meter_id.rb', line 4

def tags
  @tags
end

Instance Method Details

#==(other) ⇒ Object

Compare our id and tags against another MeterId



48
49
50
# File 'lib/spectator/meter_id.rb', line 48

def ==(other)
  other.name == @name && other.tags == @tags
end

#keyObject

lazyily compute a key to be used in hashes for efficiency



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/spectator/meter_id.rb', line 27

def key
  if @key.nil?
    hash_key = @name.to_s
    @key = hash_key
    keys = @tags.keys
    keys.sort
    keys.each do |k|
      v = tags[k]
      hash_key += "|#{k}|#{v}"
    end
    @key = hash_key
  end
  @key
end

#to_sObject

A string representation for debugging purposes



43
44
45
# File 'lib/spectator/meter_id.rb', line 43

def to_s
  "MeterId{name=#{@name}, tags=#{@tags}}"
end

#with_stat(stat_value) ⇒ Object

Create a new MeterId with key=statistic and the given value



22
23
24
# File 'lib/spectator/meter_id.rb', line 22

def with_stat(stat_value)
  with_tag(:statistic, stat_value)
end

#with_tag(key, value) ⇒ Object

Create a new MeterId with a given key and value



15
16
17
18
19
# File 'lib/spectator/meter_id.rb', line 15

def with_tag(key, value)
  new_tags = @tags.dup
  new_tags[key] = value
  MeterId.new(@name, new_tags)
end