Class: Kward::TelemetryLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/telemetry/logger.rb

Constant Summary collapse

CATEGORIES =
%w[tokens performance tools errors].freeze
ENV_KEYS =
{
  "enabled" => "KWARD_LOGGING",
  "tokens" => "KWARD_LOGGING_TOKENS",
  "performance" => "KWARD_LOGGING_PERFORMANCE",
  "tools" => "KWARD_LOGGING_TOOLS",
  "errors" => "KWARD_LOGGING_ERRORS"
}.freeze
DEFAULT_MAX_BYTES =
10 * 1024 * 1024

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path: ConfigFiles.config_path, log_dir: nil, max_bytes: DEFAULT_MAX_BYTES, clock: Time, monotonic_clock: Process, error_output: $stderr) ⇒ TelemetryLogger

Returns a new instance of TelemetryLogger.



19
20
21
22
23
24
25
26
27
28
# File 'lib/kward/telemetry/logger.rb', line 19

def initialize(config_path: ConfigFiles.config_path, log_dir: nil, max_bytes: DEFAULT_MAX_BYTES, clock: Time, monotonic_clock: Process, error_output: $stderr)
  @config_path = config_path
  @log_dir = log_dir
  @max_bytes = max_bytes.to_i.positive? ? max_bytes.to_i : DEFAULT_MAX_BYTES
  @clock = clock
  @monotonic_clock = monotonic_clock
  @error_output = error_output
  @mutex = Mutex.new
  @warned = false
end

Class Method Details

.error_payload(error) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kward/telemetry/logger.rb', line 70

def self.error_payload(error)
  payload = { "error_class" => error.class.name }
  if error.respond_to?(:provider) && error.respond_to?(:code)
    payload["provider"] = error.provider
    payload["error_code"] = error.code
    payload["error_message"] = "#{error.provider} request failed: #{error.code}"
  else
    payload["error_message"] = RPC::Redactor.redact_string(error.message.to_s)[0, 500]
  end
  payload
end

Instance Method Details

#duration_ms(started_at) ⇒ Object



62
63
64
# File 'lib/kward/telemetry/logger.rb', line 62

def duration_ms(started_at)
  ((monotonic_now - started_at.to_f) * 1000).round(1)
end

#enabled?(category) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/kward/telemetry/logger.rb', line 30

def enabled?(category)
  settings = current_settings
  settings["enabled"] && settings[category.to_s]
end

#enabled_categoriesObject



35
36
37
38
39
40
# File 'lib/kward/telemetry/logger.rb', line 35

def enabled_categories
  settings = current_settings
  return [] unless settings["enabled"]

  CATEGORIES.select { |category| settings[category] }
end

#log(category, event, payload = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kward/telemetry/logger.rb', line 46

def log(category, event, payload = {})
  category = category.to_s
  return false unless enabled?(category)

  record = sanitize_record(payload).merge(
    "timestamp" => @clock.now.utc.iso8601(3),
    "category" => category,
    "event" => event.to_s
  )
  write_record(record)
  true
rescue StandardError => e
  warn_once(e)
  false
end

#log_directoryObject



42
43
44
# File 'lib/kward/telemetry/logger.rb', line 42

def log_directory
  log_dir
end

#monotonic_nowObject



66
67
68
# File 'lib/kward/telemetry/logger.rb', line 66

def monotonic_now
  @monotonic_clock.clock_gettime(Process::CLOCK_MONOTONIC)
end