Class: Conduit::Inspector

Inherits:
Object
  • Object
show all
Defined in:
lib/conduit/inspector.rb

Overview

Attach to a Conduit::Stream to log every layer of activity to an IO. Intended for development/debugging only.

stream = Conduit.new(parser: ->(d) { JSON.parse(d) })
Conduit::Inspector.attach(stream)

Pass io: to redirect (e.g. a StringIO in tests, a file, $stderr).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, io:) ⇒ Inspector

Returns a new instance of Inspector.



18
19
20
21
22
# File 'lib/conduit/inspector.rb', line 18

def initialize(stream, io:)
  @stream = stream
  @io     = io
  @counts = Hash.new(0)
end

Instance Attribute Details

#countsObject (readonly)

Returns the value of attribute counts.



16
17
18
# File 'lib/conduit/inspector.rb', line 16

def counts
  @counts
end

Class Method Details

.attach(stream, io: $stdout) ⇒ Object



12
13
14
# File 'lib/conduit/inspector.rb', line 12

def self.attach(stream, io: $stdout)
  new(stream, io: io).attach
end

Instance Method Details

#attachObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/conduit/inspector.rb', line 24

def attach
  log_chunks
  log_frames
  log_fields
  log_pings
  log_events
  log_parsed
  log_errors
  self
end

#summaryObject

Print a one-line summary of everything seen so far.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/conduit/inspector.rb', line 36

def summary
  @io.puts(
    "[SUMMARY] " \
    "chunks=#{@counts[:chunk]} " \
    "frames=#{@counts[:frame]} " \
    "events=#{@counts[:event]} " \
    "parsed=#{@counts[:parsed]} " \
    "pings=#{@counts[:ping]} " \
    "fields=#{@counts[:field]} " \
    "errors=#{@counts[:error]} " \
    "last_event_id=#{@stream.last_event_id.inspect} " \
    "retry_ms=#{@stream.retry_ms.inspect}"
  )
end