Ragex.MCP.Telemetry (Ragex v0.17.1)

View Source

Tracks MCP tool invocation patterns and latencies.

Backed by an ETS table for near-zero overhead (~1us per write). Persists to ~/.ragex/telemetry/<project_hash>.etf on shutdown and reloads on start.

Recorded Metrics (per tool)

  • :count -- total invocations
  • :total_time_us -- cumulative execution time in microseconds
  • :last_invoked -- DateTime of last call
  • :error_count -- number of failed invocations

Integration

Wrap any tool call with execute/3:

Telemetry.execute("semantic_search", fn -> Tools.call_tool(name, args) end)

Or use the :telemetry library events:

  • [:ragex, :tool, :start] -- emitted before tool execution
  • [:ragex, :tool, :stop] -- emitted after successful execution
  • [:ragex, :tool, :exception] -- emitted on error

Summary

Functions

Returns a specification to start this module under a supervisor.

Execute a tool function and record telemetry.

Get stats for all tools or a specific tool.

Get stats for a single tool.

Record a failed tool invocation.

Record a successful tool invocation.

Clear all telemetry data.

Return total invocations across all tools.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

execute(tool_name, fun)

@spec execute(String.t(), (-> term())) :: term()

Execute a tool function and record telemetry.

Returns the result of fun.() unchanged.

get_stats(opts \\ [])

@spec get_stats(keyword()) :: [map()]

Get stats for all tools or a specific tool.

Options

  • :sort_by -- :count | :avg_time | :total_time (default :count)

  • :period -- :all | :today | :last_hour (default :all)

get_tool_stats(tool_name)

@spec get_tool_stats(String.t()) :: map() | nil

Get stats for a single tool.

record_error(tool_name, elapsed_us)

@spec record_error(String.t(), non_neg_integer()) :: :ok

Record a failed tool invocation.

record_success(tool_name, elapsed_us)

@spec record_success(String.t(), non_neg_integer()) :: :ok

Record a successful tool invocation.

reset()

@spec reset() :: :ok

Clear all telemetry data.

start_link(opts \\ [])

total_invocations()

@spec total_invocations() :: non_neg_integer()

Return total invocations across all tools.