Class: Chronicle::ApiLogs::Buffer

Inherits:
Object
  • Object
show all
Defined in:
app/services/chronicle/api_logs/buffer.rb

Overview

Append-only, per-process file buffer for API log payloads.

Each Puma/SolidQueue worker writes to its own ‘api_logs.pid.jsonl` file, so no cross-process locking is needed. The Flusher reads all PID files when it runs.

Constant Summary collapse

FILE_PREFIX =
'api_logs'.freeze
FILE_EXT =
'jsonl'.freeze

Class Method Summary collapse

Class Method Details

.append(payload) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'app/services/chronicle/api_logs/buffer.rb', line 16

def append(payload)
  dir = buffer_dir
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  path = current_file_path
  File.open(path, 'a') { |f| f.puts(payload.to_json) }

  maybe_flush(path)
end

.buffer_dirObject



26
27
28
29
30
31
32
33
34
35
# File 'app/services/chronicle/api_logs/buffer.rb', line 26

def buffer_dir
  configured = Chronicle.config.api_log_buffer_dir
  return configured if configured.present?

  if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
    Rails.root.join('tmp/chronicle').to_s
  else
    File.join(Dir.tmpdir, 'chronicle')
  end
end

.current_file_pathObject



37
38
39
# File 'app/services/chronicle/api_logs/buffer.rb', line 37

def current_file_path
  File.join(buffer_dir, "#{FILE_PREFIX}.#{Process.pid}.#{FILE_EXT}")
end

.line_count(path) ⇒ Object



41
42
43
44
45
46
# File 'app/services/chronicle/api_logs/buffer.rb', line 41

def line_count(path)
  return 0 unless File.exist?(path)
  count = 0
  File.foreach(path) { count += 1 }
  count
end