Class: LogStash::Instrument::Metric::TimedExecution

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

Overview

Allow to calculate the execution of a block of code. This class support 2 differents syntax a block or the return of the object itself, but in the later case the metric won’t be recorded Until we call ‘#stop`.

See Also:

Constant Summary collapse

MILLISECONDS =
1_000.0.freeze

Instance Method Summary collapse

Constructor Details

#initialize(metric, namespace, key) ⇒ TimedExecution

Returns a new instance of TimedExecution.



87
88
89
90
91
92
# File 'lib/logstash/instrument/metric.rb', line 87

def initialize(metric, namespace, key)
  @metric = metric
  @namespace = namespace
  @key = key
  start
end

Instance Method Details

#startObject



94
95
96
# File 'lib/logstash/instrument/metric.rb', line 94

def start
  @start_time = Time.now
end

#stopObject



98
99
100
101
102
# File 'lib/logstash/instrument/metric.rb', line 98

def stop
  execution_time = (MILLISECONDS * (Time.now - @start_time)).to_i
  @metric.report_time(@namespace, @key, execution_time)
  execution_time
end