Class: Kward::TelemetryLogger
- Inherits:
-
Object
- Object
- Kward::TelemetryLogger
- Defined in:
- lib/kward/telemetry/logger.rb
Overview
Append-only JSONL telemetry logger with secret-conscious error payloads.
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
- #duration_ms(started_at) ⇒ Object
- #enabled?(category) ⇒ Boolean
- #enabled_categories ⇒ Object
-
#initialize(config_path: ConfigFiles.config_path, log_dir: nil, max_bytes: DEFAULT_MAX_BYTES, clock: Time, monotonic_clock: Process, error_output: $stderr) ⇒ TelemetryLogger
constructor
Creates an object for telemetry event logging.
- #log(category, event, payload = {}) ⇒ Object
- #log_directory ⇒ Object
- #monotonic_now ⇒ Object
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
Creates an object for telemetry event logging.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kward/telemetry/logger.rb', line 22 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
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kward/telemetry/logger.rb', line 73 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..to_s)[0, 500] end payload end |
Instance Method Details
#duration_ms(started_at) ⇒ Object
65 66 67 |
# File 'lib/kward/telemetry/logger.rb', line 65 def duration_ms(started_at) ((monotonic_now - started_at.to_f) * 1000).round(1) end |
#enabled?(category) ⇒ Boolean
33 34 35 36 |
# File 'lib/kward/telemetry/logger.rb', line 33 def enabled?(category) settings = current_settings settings["enabled"] && settings[category.to_s] end |
#enabled_categories ⇒ Object
38 39 40 41 42 43 |
# File 'lib/kward/telemetry/logger.rb', line 38 def enabled_categories settings = current_settings return [] unless settings["enabled"] CATEGORIES.select { |category| settings[category] } end |
#log(category, event, payload = {}) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/kward/telemetry/logger.rb', line 49 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_directory ⇒ Object
45 46 47 |
# File 'lib/kward/telemetry/logger.rb', line 45 def log_directory log_dir end |
#monotonic_now ⇒ Object
69 70 71 |
# File 'lib/kward/telemetry/logger.rb', line 69 def monotonic_now @monotonic_clock.clock_gettime(Process::CLOCK_MONOTONIC) end |