Class: CloseYourIt::LogBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/closeyourit/log_buffer.rb

Overview

Buffer in-memory thread-safe dei log: accumula i LogEvent e li flusha in batch verso /logs quando raggiungono logs_batch_size, allo scadere di logs_flush_interval (timer), o allo shutdown. Riduce le richieste HTTP — i log sono alto-volume, a differenza di errori/metriche uno-a-uno.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, configuration:) ⇒ LogBuffer

Returns a new instance of LogBuffer.



12
13
14
15
16
17
18
# File 'lib/closeyourit/log_buffer.rb', line 12

def initialize(client:, configuration:)
  @client = client
  @configuration = configuration
  @mutex = Mutex.new
  @events = []
  @timer = nil
end

Instance Attribute Details

#timerObject (readonly)

esposto per i test (verifica dell'intervallo configurato)



10
11
12
# File 'lib/closeyourit/log_buffer.rb', line 10

def timer
  @timer
end

Instance Method Details

#add(event) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/closeyourit/log_buffer.rb', line 20

def add(event)
  reached_batch = false
  @mutex.synchronize do
    @events << event
    ensure_timer
    reached_batch = @events.size >= @configuration.logs_batch_size.to_i
  end
  flush if reached_batch
end

#flushObject



30
31
32
33
34
35
# File 'lib/closeyourit/log_buffer.rb', line 30

def flush
  batch = drain
  return if batch.empty?

  @client.flush_logs(batch)
end

#shutdownObject



37
38
39
40
# File 'lib/closeyourit/log_buffer.rb', line 37

def shutdown
  @timer&.shutdown
  flush
end