Class: ClaudeMemory::MCP::Telemetry

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_memory/mcp/telemetry.rb

Overview

Records MCP tool invocations into the project database for usage stats. Timing and error capture wrap the tool call; the insert is synchronous and best-effort — telemetry failures are swallowed so they never break a real tool response.

Instance Method Summary collapse

Constructor Details

#initialize(store_or_manager) ⇒ Telemetry

Returns a new instance of Telemetry.



10
11
12
# File 'lib/claude_memory/mcp/telemetry.rb', line 10

def initialize(store_or_manager)
  @store_or_manager = store_or_manager
end

Instance Method Details

#record(tool_name, arguments) ⇒ Object

Time a tool invocation and record the outcome. Yields to the caller and returns whatever the block returns; re-raises any exception after recording it as an error.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/claude_memory/mcp/telemetry.rb', line 17

def record(tool_name, arguments)
  started = monotonic_ms
  begin
    result = yield
  rescue => e
    duration = monotonic_ms - started
    write(
      tool_name: tool_name,
      duration_ms: duration,
      result_count: nil,
      scope: extract_scope(arguments),
      error_class: e.class.name
    )
    raise
  end

  duration = monotonic_ms - started
  write(
    tool_name: tool_name,
    duration_ms: duration,
    result_count: extract_result_count(result),
    scope: extract_scope(arguments),
    error_class: nil
  )
  result
end