Class: Statue::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/statue/metric.rb

Constant Summary collapse

TYPES =
{
  c:  'increment',
  ms: 'measure',
  g:  'gauge',
  kv: 'key/value',
  s:  'set'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Metric

Returns a new instance of Metric.



30
31
32
33
34
35
36
37
# File 'lib/statue/metric.rb', line 30

def initialize(options = {})
  @type  = options.fetch(:type)
  @name  = options.fetch(:name)
  @value = options.fetch(:value)
  @type_description = TYPES[type] or raise ArgumentError, "invalid type: #{type}"
  @sample_rate = options[:sample_rate] || 1.0
  @full_name   = Statue.namespace ? "#{Statue.namespace}.#{@name}" : @name
end

Instance Attribute Details

#full_nameObject (readonly)

Returns the value of attribute full_name.



14
15
16
# File 'lib/statue/metric.rb', line 14

def full_name
  @full_name
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/statue/metric.rb', line 13

def name
  @name
end

#sample_rateObject

Returns the value of attribute sample_rate.



13
14
15
# File 'lib/statue/metric.rb', line 13

def sample_rate
  @sample_rate
end

#typeObject

Returns the value of attribute type.



13
14
15
# File 'lib/statue/metric.rb', line 13

def type
  @type
end

#type_descriptionObject (readonly)

Returns the value of attribute type_description.



14
15
16
# File 'lib/statue/metric.rb', line 14

def type_description
  @type_description
end

#valueObject

Returns the value of attribute value.



13
14
15
# File 'lib/statue/metric.rb', line 13

def value
  @value
end

Class Method Details

.counter(name, value = 1, options = {}) ⇒ Object



16
17
18
# File 'lib/statue/metric.rb', line 16

def self.counter(name, value = 1, options = {})
  new(options.merge(type: :c, value: value, name: name))
end

.gauge(name, value, options = {}) ⇒ Object



20
21
22
# File 'lib/statue/metric.rb', line 20

def self.gauge(name, value, options = {})
  new(options.merge(type: :g, value: value, name: name))
end

.measure(name, options = {}, &block) ⇒ Object



24
25
26
27
28
# File 'lib/statue/metric.rb', line 24

def self.measure(name, options = {}, &block)
  duration = options.delete(:duration)
  value = duration || Statue::Clock.duration_in_ms(&block)
  new(options.merge(type: :ms, value: value, name: name))
end

Instance Method Details

#inspectObject



45
46
47
# File 'lib/statue/metric.rb', line 45

def inspect
  "#<StatsD::Instrument::Metric #{full_name} #{type_description}(#{value})@#{sample_rate}>"
end

#to_sObject



39
40
41
42
43
# File 'lib/statue/metric.rb', line 39

def to_s
  str = "#{full_name}:#{value}|#{type}"
  str << "|@#{sample_rate}" if sample_rate != 1.0
  str
end