Class: ClaudeMemory::OTel::SettingsWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_memory/otel/settings_writer.rb

Overview

Idempotent reader/writer for the OTel-related env block in .claude/settings.json. Each method returns Core::Result so the CLI can render uniform success/failure output.

Settings file shape (Claude Code reads this on session start):

{
  "env": {
    "CLAUDE_CODE_ENABLE_TELEMETRY": "1",
    "OTEL_EXPORTER_OTLP_PROTOCOL": "http/json",
    "OTEL_EXPORTER_OTLP_ENDPOINT": "http://127.0.0.1:3377",
    "OTEL_METRICS_EXPORTER": "otlp",
    "OTEL_LOGS_EXPORTER": "otlp"
  }
}

Traces and prompt-content opt-ins write additional keys; #disable! clears every key this module owns and leaves the rest of the file untouched.

Constant Summary collapse

DEFAULT_PORT =
3377
OWNED_KEYS =
%w[
  CLAUDE_CODE_ENABLE_TELEMETRY
  OTEL_EXPORTER_OTLP_PROTOCOL
  OTEL_EXPORTER_OTLP_ENDPOINT
  OTEL_METRICS_EXPORTER
  OTEL_LOGS_EXPORTER
  OTEL_TRACES_EXPORTER
  OTEL_LOG_USER_PROMPTS
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(claude_dir, port: DEFAULT_PORT) ⇒ SettingsWriter

Returns a new instance of SettingsWriter.



41
42
43
44
45
# File 'lib/claude_memory/otel/settings_writer.rb', line 41

def initialize(claude_dir, port: DEFAULT_PORT)
  @claude_dir = claude_dir
  @settings_path = File.join(@claude_dir, "settings.json")
  @port = port
end

Instance Attribute Details

#settings_pathObject (readonly)

Returns the value of attribute settings_path.



47
48
49
# File 'lib/claude_memory/otel/settings_writer.rb', line 47

def settings_path
  @settings_path
end

Instance Method Details

#capture_prompts!Object



77
78
79
80
81
# File 'lib/claude_memory/otel/settings_writer.rb', line 77

def capture_prompts!
  update_env do |env|
    env["OTEL_LOG_USER_PROMPTS"] = "1"
  end
end

#current_envObject

Read-only accessor — returns the current OTel-related env values so the CLI’s –status subcommand and the dashboard header can render what’s configured without re-implementing JSON parsing.



92
93
94
# File 'lib/claude_memory/otel/settings_writer.rb', line 92

def current_env
  load_settings.fetch("env", {}).slice(*OWNED_KEYS)
end

#disable!Object



59
60
61
62
63
# File 'lib/claude_memory/otel/settings_writer.rb', line 59

def disable!
  update_env do |env|
    OWNED_KEYS.each { |key| env.delete(key) }
  end
end

#disable_capture_prompts!Object



83
84
85
86
87
# File 'lib/claude_memory/otel/settings_writer.rb', line 83

def disable_capture_prompts!
  update_env do |env|
    env.delete("OTEL_LOG_USER_PROMPTS")
  end
end

#disable_traces!Object



71
72
73
74
75
# File 'lib/claude_memory/otel/settings_writer.rb', line 71

def disable_traces!
  update_env do |env|
    env["OTEL_TRACES_EXPORTER"] = "none"
  end
end

#enable!Object



49
50
51
52
53
54
55
56
57
# File 'lib/claude_memory/otel/settings_writer.rb', line 49

def enable!
  update_env do |env|
    env["CLAUDE_CODE_ENABLE_TELEMETRY"] = "1"
    env["OTEL_EXPORTER_OTLP_PROTOCOL"] = "http/json"
    env["OTEL_EXPORTER_OTLP_ENDPOINT"] = "http://127.0.0.1:#{@port}"
    env["OTEL_METRICS_EXPORTER"] = "otlp"
    env["OTEL_LOGS_EXPORTER"] = "otlp"
  end
end

#enable_traces!Object



65
66
67
68
69
# File 'lib/claude_memory/otel/settings_writer.rb', line 65

def enable_traces!
  update_env do |env|
    env["OTEL_TRACES_EXPORTER"] = "otlp"
  end
end