Class: Prescient::Agent::AuditLog

Inherits:
Object
  • Object
show all
Defined in:
lib/prescient/agent/audit_log.rb

Overview

Persists safe Agent telemetry events as newline-delimited JSON.

Constant Summary collapse

FIELDS =

Telemetry keys permitted in durable audit records.

Returns:

  • (Array<Symbol>)

    Safe event fields

%i[event loop loops_run actions success phase error].freeze

Instance Method Summary collapse

Constructor Details

#initialize(path: nil, io: nil) ⇒ AuditLog

Returns a new instance of AuditLog.

Parameters:

  • path (String, nil) (defaults to: nil)

    File path opened in append mode

  • io (#write, nil) (defaults to: nil)

    Writable stream owned by the caller

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/prescient/agent/audit_log.rb', line 17

def initialize(path: nil, io: nil)
  raise ArgumentError, "provide path or io, not both" if path && io
  raise ArgumentError, "path or io is required" unless path || io

  @io = io || File.open(path, "a")
  @lock = Monitor.new
end

Instance Method Details

#call(event) ⇒ void

This method returns an undefined value.

Persist one allowlisted telemetry event.

Parameters:

  • event (Hash)

    Agent event metadata



28
29
30
31
32
33
34
# File 'lib/prescient/agent/audit_log.rb', line 28

def call(event)
  record = event.slice(*FIELDS).merge(timestamp: Time.now.utc.iso8601)
  @lock.synchronize do
    @io.write("#{JSON.generate(record)}\n")
    @io.flush if @io.respond_to?(:flush)
  end
end