Class: Dommy::Rack::Trace::Ndjson

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/rack/trace/ndjson.rb

Overview

Serializes a Trace's event stream as NDJSON (trace-ndjson.md v2): one compact JSON object per line, the authoritative order carried by seq, the op-specific data flattened onto the line. This is the on-disk contract the standalone trace viewer (dommylizer) reads — neither side depends on the other, only on this shape.

An instance holds one document's context (level, the artifact emission fields by seq, wall_time/metadata, end_wall_ms); #document renders the whole stream for a given status:

File.write("run.trace.ndjson",
Trace::Ndjson.new(events, level: :verbose, end_wall_ms: now).document(status: "failed"))

An event's internal type is the line's op; seq/t/action_seq map straight across. The one rename is :dom, whose mutation kind lives under the reserved key op internally and is emitted as kind.

Constant Summary collapse

VERSION =
2

Instance Method Summary collapse

Constructor Details

#initialize(events, level:, artifacts: {}, wall_time: nil, metadata: nil, end_wall_ms: nil) ⇒ Ndjson

artifacts maps an artifact event's seq to its emission fields ({path:} when externalized, {content:, encoding:} when inline).



29
30
31
32
33
34
35
36
# File 'lib/dommy/rack/trace/ndjson.rb', line 29

def initialize(events, level:, artifacts: {}, wall_time: nil, metadata: nil, end_wall_ms: nil)
  @events = events
  @level = level
  @artifacts = artifacts
  @wall_time = wall_time
  @metadata = 
  @end_wall_ms = end_wall_ms
end

Instance Method Details

#document(status:) ⇒ Object

The whole document: a trace_start line, one line per event, a trace_end line. Returns a newline-terminated String (each line standalone JSON).



40
41
42
43
# File 'lib/dommy/rack/trace/ndjson.rb', line 40

def document(status:)
  lines = [start_line, *@events.map { |event| event_line(event) }, end_line(status)]
  "#{lines.map { |hash| ::JSON.generate(hash) }.join("\n")}\n"
end

#event_line(event) ⇒ Object

One event's line. Public so the type->op mapping can be unit-tested.



46
47
48
49
50
51
# File 'lib/dommy/rack/trace/ndjson.rb', line 46

def event_line(event)
  line = {seq: event.seq, op: op_for(event.type), t: event.t, wall_ms: event.wall_ms}
  line[:action] = event.action_seq if event.action_seq
  line.merge!(payload(event))
  line
end