Class: Buildkite::TestCollector::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/buildkite/test_collector/tracer.rb

Overview

Traces the execution of an application by creating and storing spans of information.

This class contains two data structures:

  • A stack (called @stack) that traces the entering & leaving of each part of the application.

  • A tree made up of many Span nodes. Each Span is a node in the tree. Each span is also stored in the stack. The root of the tree is called @top and is stored at @stack.

When the trace is complete the stack MUST contain a single node @top, which is the root of the tree (see #finalize). The tree is converted into a hash in #as_json which recursively calls #as_json on all of it’s children.

Defined Under Namespace

Classes: MonotonicTime, Span

Instance Method Summary collapse

Constructor Details

#initialize(min_duration: nil) ⇒ Tracer

Returns a new instance of Tracer.



58
59
60
61
# File 'lib/buildkite/test_collector/tracer.rb', line 58

def initialize(min_duration: nil)
  @top = Span.new(:top, MonotonicTime.call, nil, {})
  @stack = [@top]
end

Instance Method Details

#backfill(section, duration, **detail) ⇒ Object



78
79
80
81
82
# File 'lib/buildkite/test_collector/tracer.rb', line 78

def backfill(section, duration, **detail)
  now = MonotonicTime.call
  new_entry = Span.new(section, now - duration, now, detail)
  current_span.children << new_entry if retain_span?(new_entry)
end

#current_spanObject



84
85
86
# File 'lib/buildkite/test_collector/tracer.rb', line 84

def current_span
  @stack.last
end

#enter(section, **detail) ⇒ Object



63
64
65
66
67
# File 'lib/buildkite/test_collector/tracer.rb', line 63

def enter(section, **detail)
  new_entry = Span.new(section, MonotonicTime.call, nil, detail)
  current_span.children << new_entry
  @stack << new_entry
end

#finalizeObject



88
89
90
91
92
# File 'lib/buildkite/test_collector/tracer.rb', line 88

def finalize
  raise "Stack not empty" unless @stack.size == 1
  @top.end_at = MonotonicTime.call
  self
end

#historyObject



94
95
96
# File 'lib/buildkite/test_collector/tracer.rb', line 94

def history
  @top.as_hash
end

#leaveObject



69
70
71
72
73
74
75
76
# File 'lib/buildkite/test_collector/tracer.rb', line 69

def leave
  current_span.end_at = MonotonicTime.call
  @stack.pop

  current_span.children.pop unless retain_span?(current_span.children.last)

  nil # avoid ambiguous return type/value
end