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 compaction].freeze
- ENV_KEYS =
{ "enabled" => "KWARD_LOGGING", "tokens" => "KWARD_LOGGING_TOKENS", "performance" => "KWARD_LOGGING_PERFORMANCE", "tools" => "KWARD_LOGGING_TOOLS", "errors" => "KWARD_LOGGING_ERRORS", "compaction" => "KWARD_LOGGING_COMPACTION" }.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.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/kward/telemetry/logger.rb', line 23 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
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/kward/telemetry/logger.rb', line 74 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
66 67 68 |
# File 'lib/kward/telemetry/logger.rb', line 66 def duration_ms(started_at) ((monotonic_now - started_at.to_f) * 1000).round(1) end |
#enabled?(category) ⇒ Boolean
34 35 36 37 |
# File 'lib/kward/telemetry/logger.rb', line 34 def enabled?(category) settings = current_settings settings["enabled"] && settings[category.to_s] end |
#enabled_categories ⇒ Object
39 40 41 42 43 44 |
# File 'lib/kward/telemetry/logger.rb', line 39 def enabled_categories settings = current_settings return [] unless settings["enabled"] CATEGORIES.select { |category| settings[category] } end |
#log(category, event, payload = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/kward/telemetry/logger.rb', line 50 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
46 47 48 |
# File 'lib/kward/telemetry/logger.rb', line 46 def log_directory log_dir end |
#monotonic_now ⇒ Object
70 71 72 |
# File 'lib/kward/telemetry/logger.rb', line 70 def monotonic_now @monotonic_clock.clock_gettime(Process::CLOCK_MONOTONIC) end |