Class: Airbrake::TimedTrace

Inherits:
Object
  • Object
show all
Defined in:
lib/airbrake-ruby/timed_trace.rb

Overview

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

Examples:

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

Since:

  • v4.3.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimedTrace

Returns a new instance of TimedTrace.

Since:

  • v4.3.0



21
22
23
# File 'lib/airbrake-ruby/timed_trace.rb', line 21

def initialize
  @spans = {}
end

Class Method Details

.span(label, &block) ⇒ Airbrake::TimedTrace

Parameters:

  • label (String)

Returns:

Since:

  • v4.3.0



17
18
19
# File 'lib/airbrake-ruby/timed_trace.rb', line 17

def self.span(label, &block)
  new.tap { |timed_trace| timed_trace.span(label, &block) }
end

Instance Method Details

#span(label) ⇒ Boolean

Parameters:

  • label (String)

Returns:

  • (Boolean)

Since:

  • v4.3.0



27
28
29
30
31
# File 'lib/airbrake-ruby/timed_trace.rb', line 27

def span(label)
  start_span(label)
  yield
  stop_span(label)
end

#spansHash<String=>Float>

Returns:

  • (Hash<String=>Float>)

Since:

  • v4.3.0



52
53
54
# File 'lib/airbrake-ruby/timed_trace.rb', line 52

def spans
  @spans.transform_values(&:duration)
end

#start_span(label) ⇒ Boolean

Parameters:

  • label (String)

Returns:

  • (Boolean)

Since:

  • v4.3.0



35
36
37
38
39
40
# 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) ⇒ Boolean

Parameters:

  • label (String)

Returns:

  • (Boolean)

Since:

  • v4.3.0



44
45
46
47
48
49
# File 'lib/airbrake-ruby/timed_trace.rb', line 44

def stop_span(label)
  return false unless @spans.key?(label)

  @spans[label].stop
  true
end