Class: OrangeTap::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/orange_tap/worker.rb

Overview

Runs on a dedicated Thread created by Session#open. Drains the session's Queue, reconstructs a 3-layer span tree (session root / per-thread / per-method) from CALL/RETURN pairs, and writes a single OTLP/JSON file.

The Queue instance itself is the session boundary: there is no session_id tag on Event, because a Worker only ever drains events produced by the TracePoints of the Session that spawned it.

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Constructor Details

#initialize(ctx) ⇒ Worker

Returns a new instance of Worker.



18
19
20
21
22
23
24
# File 'lib/orange_tap/worker.rb', line 18

def initialize(ctx)
  @ctx = ctx
  @stacks = Hash.new { |h, k| h[k] = [] } # thread_id => [PendingSpan, ...]
  @thread_spans = {}                       # thread_id => PendingSpan
  @completed = []                          # confirmed method-layer spans
  @session_span = nil
end

Instance Method Details

#runObject

Returns the full path of the written JSON file. Session#stop receives this via Thread#value.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/orange_tap/worker.rb', line 28

def run
  @session_span = new_session_span

  loop do
    event = @ctx.queue.pop
    break if event.nil? # Queue#close makes pop return nil once drained

    case event.type
    when :call then on_call(event)
    when :return then on_return(event)
    end
  end

  finalize_dangling_spans
  write_json
end