class Airbrake::TimedTrace

TimedTrace represents a chunk of code performance of which was measured and stored under a label. The chunk is called a “span”.

@example

timed_trace = TimedTrace.new
timed_trace.span('http request') do
  http.get('example.com')
end
timed_trace.spans #=> { 'http request' => 0.123 }

@api public @since v4.3.0

Public Class Methods

new() click to toggle source
# File lib/airbrake-ruby/timed_trace.rb, line 21
def initialize
  @spans = {}
end
span(label, &block) click to toggle source

@param [String] label @return [Airbrake::TimedTrace]

# File lib/airbrake-ruby/timed_trace.rb, line 17
def self.span(label, &block)
  new.tap { |timed_trace| timed_trace.span(label, &block) }
end

Public Instance Methods

span(label) { || ... } click to toggle source

@param [String] label @return [Boolean]

# File lib/airbrake-ruby/timed_trace.rb, line 27
def span(label)
  start_span(label)
  yield
  stop_span(label)
end
spans() click to toggle source

@return [Hash<String=>Float>]

# File lib/airbrake-ruby/timed_trace.rb, line 52
def spans
  @spans.transform_values(&:duration)
end
start_span(label) click to toggle source

@param [String] label @return [Boolean]

# File lib/airbrake-ruby/timed_trace.rb, line 35
def start_span(label)
  return false if @spans.key?(label)

  @spans[label] = Airbrake::Benchmark.new
  true
end
stop_span(label) click to toggle source

@param [String] label @return [Boolean]

# File lib/airbrake-ruby/timed_trace.rb, line 44
def stop_span(label)
  return false unless @spans.key?(label)

  @spans[label].stop
  true
end