Class: LogStash::Instrument::Metric

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

Overview

This class provide the interface between the code, the collector and the format of the recorded metric.

Defined Under Namespace

Classes: TimedExecution

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collector) ⇒ Metric

Returns a new instance of Metric.



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

def initialize(collector)
  @collector = collector
end

Instance Attribute Details

#collectorObject (readonly)

Returns the value of attribute collector.



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

def collector
  @collector
end

Class Method Details

.validate_key!(key) ⇒ Object



73
74
75
# File 'lib/logstash/instrument/metric.rb', line 73

def self.validate_key!(key)
  raise MetricNoKeyProvided if key.nil? || key.empty?
end

Instance Method Details

#decrement(namespace, key, value = 1) ⇒ Object



25
26
27
28
# File 'lib/logstash/instrument/metric.rb', line 25

def decrement(namespace, key, value = 1)
  self.class.validate_key!(key)
  collector.push(namespace, key, :counter, :decrement, value)
end

#gauge(namespace, key, value) ⇒ Object



30
31
32
33
# File 'lib/logstash/instrument/metric.rb', line 30

def gauge(namespace, key, value)
  self.class.validate_key!(key)
  collector.push(namespace, key, :gauge, :set, value)
end

#increment(namespace, key, value = 1) ⇒ Object



20
21
22
23
# File 'lib/logstash/instrument/metric.rb', line 20

def increment(namespace, key, value = 1)
  self.class.validate_key!(key)
  collector.push(namespace, key, :counter, :increment, value)
end

#namespace(name) ⇒ Object

This method return a metric instance tied to a specific namespace so instead of specifying the namespace on every call.

Example:

metric.increment(:namespace, :mykey, 200)
metric.increment(:namespace, :mykey_2, 200)

namespaced_metric = metric.namespace(:namespace)
namespaced_metric.increment(:mykey, 200)
namespaced_metric.increment(:mykey_2, 200)

“‘

Parameters:

  • name (Array<String>)

    Name of the namespace

  • name (String)

    Name of the namespace

Raises:



67
68
69
70
71
# File 'lib/logstash/instrument/metric.rb', line 67

def namespace(name)
  raise MetricNoNamespaceProvided if name.nil? || name.empty?

  NamespacedMetric.new(self, name)
end

#report_time(namespace, key, duration) ⇒ Object



48
49
50
51
# File 'lib/logstash/instrument/metric.rb', line 48

def report_time(namespace, key, duration)
  self.class.validate_key!(key)
  collector.push(namespace, key, :counter, :increment, duration)
end

#time(namespace, key) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/logstash/instrument/metric.rb', line 35

def time(namespace, key)
  self.class.validate_key!(key)

  if block_given?
    timer = TimedExecution.new(self, namespace, key)
    content = yield
    timer.stop
    return content
  else
    TimedExecution.new(self, namespace, key)
  end
end