Documentation
¶
Overview ¶
Package metrics provides token usage metrics collection and aggregation.
Index ¶
- type Accumulator
- func (a *Accumulator) Clear()
- func (a *Accumulator) Query(duration time.Duration) TimeSeriesResponse
- func (a *Accumulator) Record(serverName string, inputTokens, outputTokens int)
- func (a *Accumulator) RecordFormatSavings(serverName string, originalTokens, formattedTokens int)
- func (a *Accumulator) Snapshot() TokenUsage
- type DataPoint
- type FormatSavings
- type Observer
- type TimeSeriesResponse
- type TokenCounts
- type TokenUsage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Accumulator ¶
type Accumulator struct {
// contains filtered or unexported fields
}
Accumulator collects token usage metrics with thread-safe operations. Session totals use atomic counters. Historical data is stored in a ring buffer of pre-aggregated 1-minute time buckets.
func NewAccumulator ¶
func NewAccumulator(maxDataPoints int) *Accumulator
NewAccumulator creates a metrics accumulator with the given ring buffer capacity. Each slot holds one minute of aggregated data, so 10000 slots ≈ ~7 days.
func (*Accumulator) Clear ¶
func (a *Accumulator) Clear()
Clear resets all metrics — session totals, per-server totals, and history.
func (*Accumulator) Query ¶
func (a *Accumulator) Query(duration time.Duration) TimeSeriesResponse
Query returns historical time-series data for the given duration. For ranges > 6h, data points are downsampled to hourly buckets.
func (*Accumulator) Record ¶
func (a *Accumulator) Record(serverName string, inputTokens, outputTokens int)
Record adds a token usage observation from a tool call.
func (*Accumulator) RecordFormatSavings ¶
func (a *Accumulator) RecordFormatSavings(serverName string, originalTokens, formattedTokens int)
RecordFormatSavings records token counts before and after format conversion. Normal token usage tracking is handled separately by the ToolCallObserver; this method only tracks the format savings delta.
func (*Accumulator) Snapshot ¶
func (a *Accumulator) Snapshot() TokenUsage
Snapshot returns the current token usage summary.
type DataPoint ¶
type DataPoint struct {
Timestamp time.Time `json:"timestamp"`
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
TotalTokens int64 `json:"total_tokens"`
}
DataPoint is a single time-series data point with token counts.
type FormatSavings ¶
type FormatSavings struct {
OriginalTokens int64 `json:"original_tokens"`
FormattedTokens int64 `json:"formatted_tokens"`
SavedTokens int64 `json:"saved_tokens"`
SavingsPercent float64 `json:"savings_percent"`
}
FormatSavings tracks token savings from output formatting.
type Observer ¶
type Observer struct {
// contains filtered or unexported fields
}
Observer implements mcp.ToolCallObserver by counting tokens and recording them into an Accumulator.
func NewObserver ¶
func NewObserver(counter token.Counter, accumulator *Accumulator) *Observer
NewObserver creates a ToolCallObserver that counts tokens and records metrics.
func (*Observer) ObserveToolCall ¶
func (o *Observer) ObserveToolCall(serverName string, arguments map[string]any, result *mcp.ToolCallResult)
ObserveToolCall counts input/output tokens and records them.
type TimeSeriesResponse ¶
type TimeSeriesResponse struct {
Range string `json:"range"`
Interval string `json:"interval"`
Points []DataPoint `json:"data_points"`
PerServer map[string][]DataPoint `json:"per_server"`
}
TimeSeriesResponse is returned by the historical metrics endpoint.
type TokenCounts ¶
type TokenCounts struct {
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
TotalTokens int64 `json:"total_tokens"`
}
TokenCounts holds input/output/total token counts.
type TokenUsage ¶
type TokenUsage struct {
Session TokenCounts `json:"session"`
PerServer map[string]TokenCounts `json:"per_server"`
FormatSavings FormatSavings `json:"format_savings"`
}
TokenUsage is the top-level token usage snapshot returned by the API.