Class: Julewire::Core::Diagnostics::Tail

Inherits:
Object
  • Object
show all
Defined in:
lib/julewire/core/diagnostics/tail.rb,
lib/julewire/core/diagnostics/tail/renderer.rb

Defined Under Namespace

Classes: Entry, Renderer

Constant Summary collapse

DEFAULT_CAPACITY =
200
DEFAULT_NAME =
:tail
COUNTER_KEYS =
%i[captured failures].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: DEFAULT_NAME, capacity: DEFAULT_CAPACITY, formatter: Records::Formatter.new, renderer: Renderer.new, serializer: nil) ⇒ Tail

Returns a new instance of Tail.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/julewire/core/diagnostics/tail.rb', line 22

def initialize(
  name: DEFAULT_NAME,
  capacity: DEFAULT_CAPACITY,
  formatter: Records::Formatter.new,
  renderer: Renderer.new,
  serializer: nil
)
  @name = Core.normalize_name(name)
  @capacity = Validation.validate_integer_limit!(capacity, name: :capacity, positive: true)
  Validation.validate_callable!(formatter, name: :formatter)
  Validation.validate_callable!(renderer, name: :renderer)
  @formatter = formatter
  @renderer = renderer
  @serializer = serializer
  @serializer_pool_key = :"julewire_core_tail_serializers_#{object_id}"
  initialize_state
end

Instance Attribute Details

#capacityObject (readonly)

Returns the value of attribute capacity.



20
21
22
# File 'lib/julewire/core/diagnostics/tail.rb', line 20

def capacity
  @capacity
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/julewire/core/diagnostics/tail.rb', line 20

def name
  @name
end

Class Method Details

.attach!(runtime = Julewire) ⇒ Object



13
14
15
16
17
# File 'lib/julewire/core/diagnostics/tail.rb', line 13

def attach!(runtime = Julewire, **)
  destination = new(**)
  runtime.configure { it.destinations.add(destination) }
  destination
end

Instance Method Details

#after_fork!Object



86
87
88
89
# File 'lib/julewire/core/diagnostics/tail.rb', line 86

def after_fork!
  initialize_state
  self
end

#clearObject



74
75
76
77
78
79
80
# File 'lib/julewire/core/diagnostics/tail.rb', line 74

def clear
  @mutex.synchronize do
    @entries.clear
  end
  @health.clear_degraded!
  self
end

#closeObject



84
# File 'lib/julewire/core/diagnostics/tail.rb', line 84

def close(*) = self

#emit(record) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/julewire/core/diagnostics/tail.rb', line 40

def emit(record)
  snapshot = snapshot_record(record)
  @mutex.synchronize do
    @sequence += 1
    entry = Entry.new(@sequence, Time.now.utc, snapshot)
    @entries << entry
    @entries.shift while @entries.length > @capacity
  end
  @health.increment(:captured)
  nil
rescue StandardError => e
  record_failure(e, record)
  nil
end

#entries(limit: nil) ⇒ Object



55
56
57
58
59
# File 'lib/julewire/core/diagnostics/tail.rb', line 55

def entries(limit: nil)
  limit = normalize_limit(limit)
  snapshot = @mutex.synchronize { @entries.dup }
  limit ? snapshot.last(limit) : snapshot
end

#flushObject



82
# File 'lib/julewire/core/diagnostics/tail.rb', line 82

def flush(*) = self

#healthObject



91
92
93
94
# File 'lib/julewire/core/diagnostics/tail.rb', line 91

def health
  size = @mutex.synchronize { @entries.length }
  @health.snapshot(capacity: @capacity, size: size)
end

#records(limit: nil) ⇒ Object



61
62
63
# File 'lib/julewire/core/diagnostics/tail.rb', line 61

def records(limit: nil)
  entries(limit: limit).map(&:record)
end

#render(limit: nil, color: false) ⇒ Object



65
66
67
# File 'lib/julewire/core/diagnostics/tail.rb', line 65

def render(limit: nil, color: false)
  @renderer.call(entries(limit: limit), color: color)
end

#write(io = $stdout, limit: nil, color: nil) ⇒ Object



69
70
71
72
# File 'lib/julewire/core/diagnostics/tail.rb', line 69

def write(io = $stdout, limit: nil, color: nil)
  io.write(render(limit: limit, color: color.nil? ? io.tty? : color))
  io
end