metrics

package
v0.0.0-...-8acab51 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MeasurementAPICalls   = "api_calls"
	MeasurementTokenUsage = "token_usage"
	MeasurementLatency    = "latency"
	MeasurementSpeed      = "speed"
	MeasurementSystem     = "system"
	MeasurementProcess    = "process"
	MeasurementCounters   = "runtime_counters"
)

Measurement names for metrics.

View Source
const (
	TagModel     = "model"
	TagStatus    = "status"
	TagErrorType = "error_type"
	TagMetric    = "metric"
)

Tag keys for metrics.

View Source
const (
	FieldCount            = "count"
	FieldSuccess          = "success"
	FieldLatencyMs        = "latency_ms"
	FieldInputTokens      = "input_tokens"
	FieldOutputTokens     = "output_tokens"
	FieldTotalTokens      = "total_tokens"
	FieldCacheReadTokens  = "cache_read_tokens"
	FieldCacheWriteTokens = "cache_write_tokens"
	FieldCost             = "cost"
	FieldTokensPerSecond  = "tokens_per_second"
	FieldTTFT             = "ttft_ms"
	FieldCPUPercent       = "cpu_percent"
	FieldMemoryBytes      = "memory_bytes"
	FieldMemoryPercent    = "memory_percent"
	FieldDiskPercent      = "disk_percent"
)

Field keys for metrics.

View Source
const (
	ErrorTypeNone      = ""
	ErrorTypeTimeout   = "timeout"
	ErrorTypeRateLimit = "rate_limit"
	ErrorTypeError     = "error"
)

ErrorType constants for call records.

View Source
const (
	PeriodRealtime = "realtime" // Last 1 minute
	PeriodHourly   = "hourly"   // Last hour
	PeriodDaily    = "daily"    // Last 24 hours
	PeriodWeekly   = "weekly"   // Last 7 days
	PeriodMonthly  = "monthly"  // Last 30 days
)

Period constants for time-based queries.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallCollector

type CallCollector struct {
	// contains filtered or unexported fields
}

CallCollector collects and aggregates API call metrics.

func NewCallCollector

func NewCallCollector(maxRecords int, retentionTime time.Duration) *CallCollector

NewCallCollector creates a new CallCollector.

func (*CallCollector) GetCallStats

func (c *CallCollector) GetCallStats() *CallStats

GetCallStats returns current call statistics.

func (*CallCollector) GetCallStatsByPeriod

func (c *CallCollector) GetCallStatsByPeriod(period string) *CallStats

GetCallStatsByPeriod returns call statistics for a specific period.

func (*CallCollector) GetHourlyStats

func (c *CallCollector) GetHourlyStats() []HourlyStats

GetHourlyStats returns hourly statistics for the last 24 hours.

func (*CallCollector) GetModelStats

func (c *CallCollector) GetModelStats() []ModelStats

GetModelStats returns statistics for all models.

func (*CallCollector) GetModelStatsByName

func (c *CallCollector) GetModelStatsByName(model string) *ModelStats

GetModelStatsByName returns statistics for a specific model.

func (*CallCollector) LoadModelStats

func (c *CallCollector) LoadModelStats(stats []ModelStats)

LoadModelStats loads model statistics from persisted data.

func (*CallCollector) RecordCall

func (c *CallCollector) RecordCall(record CallRecord)

RecordCall records a single API call.

func (*CallCollector) Reset

func (c *CallCollector) Reset()

Reset resets all statistics.

type CallRecord

type CallRecord struct {
	ID        string    `json:"id"`
	Timestamp time.Time `json:"timestamp"`
	Model     string    `json:"model"`
	Success   bool      `json:"success"`
	ErrorType string    `json:"error_type,omitempty"` // timeout, rate_limit, error

	// Latency
	LatencyMs        float64 `json:"latency_ms"`
	TimeToFirstToken float64 `json:"ttft_ms,omitempty"`

	// Tokens
	InputTokens      int64 `json:"input_tokens"`
	OutputTokens     int64 `json:"output_tokens"`
	CacheReadTokens  int64 `json:"cache_read_tokens,omitempty"`
	CacheWriteTokens int64 `json:"cache_write_tokens,omitempty"`

	// Speed
	TokensPerSecond float64 `json:"tokens_per_second,omitempty"`
}

CallRecord represents a single API call record for tracking.

type CallStats

type CallStats struct {
	// Call counts
	TotalCalls      int64 `json:"total_calls"`
	SuccessfulCalls int64 `json:"successful_calls"`
	FailedCalls     int64 `json:"failed_calls"`

	// By error type
	TimeoutCalls   int64 `json:"timeout_calls"`
	RateLimitCalls int64 `json:"rate_limit_calls"`
	ErrorCalls     int64 `json:"error_calls"`

	// Success rate
	SuccessRate float64 `json:"success_rate"`
}

CallStats represents API call statistics.

type CallStatsResponse

type CallStatsResponse struct {
	Period string        `json:"period"`
	Stats  *CallStats    `json:"stats"`
	ByHour []HourlyStats `json:"by_hour,omitempty"`
}

CallStatsResponse represents the API response for call statistics.

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector collects and stores system metrics history

func NewCollector

func NewCollector(interval time.Duration, maxHistory int) *Collector

NewCollector creates a new metrics collector

func (*Collector) GetHistory

func (c *Collector) GetHistory(duration time.Duration) MetricsHistory

GetHistory returns metrics history for the specified duration

func (*Collector) GetLatest

func (c *Collector) GetLatest() *SystemMetrics

GetLatest returns the most recent metrics snapshot

func (*Collector) Start

func (c *Collector) Start()

Start begins collecting metrics at the configured interval

func (*Collector) Stop

func (c *Collector) Stop()

Stop stops the metrics collector

type ContinuousQuery

type ContinuousQuery struct {
	Name          string
	Database      string
	Query         string
	ResampleEvery time.Duration
	ResampleFor   time.Duration
}

ContinuousQuery defines a continuous query configuration.

type GenerationSpeed

type GenerationSpeed struct {
	// Token generation rate
	TokensPerSecond    float64 `json:"tokens_per_second"`
	AvgTokensPerSecond float64 `json:"avg_tokens_per_second"`
	MaxTokensPerSecond float64 `json:"max_tokens_per_second"`

	// Prefill rate (time to first token)
	PrefillSpeed        float64 `json:"prefill_speed"`
	TimeToFirstToken    float64 `json:"time_to_first_token_ms"`
	AvgTimeToFirstToken float64 `json:"avg_ttft_ms"`

	// Decode rate
	DecodeSpeed float64 `json:"decode_speed"`
}

GenerationSpeed represents generation speed statistics.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles metrics API endpoints.

func NewHandler

func NewHandler(writer *MetricsWriter) *Handler

NewHandler creates a new metrics handler.

func (*Handler) GetAll

func (h *Handler) GetAll(c echo.Context) error

GetAll handles GET /api/v1/metrics/all - aggregated metrics endpoint

func (*Handler) GetCacheStats

func (h *Handler) GetCacheStats(c echo.Context) error

GetCacheStats handles GET /api/v1/metrics/cache (deprecated — cache removed)

func (*Handler) GetCallStats

func (h *Handler) GetCallStats(c echo.Context) error

GetCallStats handles GET /api/v1/metrics/calls

func (*Handler) GetLatencyStats

func (h *Handler) GetLatencyStats(c echo.Context) error

GetLatencyStats handles GET /api/v1/metrics/latency

func (*Handler) GetModelSpeedStats

func (h *Handler) GetModelSpeedStats(c echo.Context) error

GetModelSpeedStats handles GET /api/v1/metrics/speed/models

func (*Handler) GetModelStats

func (h *Handler) GetModelStats(c echo.Context) error

GetModelStats handles GET /api/v1/metrics/models

func (*Handler) GetModelStatsByName

func (h *Handler) GetModelStatsByName(c echo.Context) error

GetModelStatsByName handles GET /api/v1/metrics/models/:model

func (*Handler) GetMyUsage

func (h *Handler) GetMyUsage(c echo.Context) error

GetMyUsage handles GET /api/v1/my/usage — returns current user's token usage.

func (*Handler) GetPricing

func (h *Handler) GetPricing(c echo.Context) error

GetPricing handles GET /api/v1/metrics/pricing

func (*Handler) GetPricingForModel

func (h *Handler) GetPricingForModel(c echo.Context) error

GetPricingForModel handles GET /api/v1/metrics/pricing/:model

func (*Handler) GetProcessMetrics

func (h *Handler) GetProcessMetrics(c echo.Context) error

GetProcessMetrics handles GET /api/v1/metrics/process

func (*Handler) GetResourceHistory

func (h *Handler) GetResourceHistory(c echo.Context) error

GetResourceHistory handles GET /api/v1/metrics/system/history

func (*Handler) GetSpeedStats

func (h *Handler) GetSpeedStats(c echo.Context) error

GetSpeedStats handles GET /api/v1/metrics/speed

func (*Handler) GetSummary

func (h *Handler) GetSummary(c echo.Context) error

GetSummary handles GET /api/v1/metrics/summary

func (*Handler) GetSystemMetrics

func (h *Handler) GetSystemMetrics(c echo.Context) error

GetSystemMetrics handles GET /api/v1/metrics/system

func (*Handler) GetTokenUsage

func (h *Handler) GetTokenUsage(c echo.Context) error

GetTokenUsage handles GET /api/v1/metrics/tokens

func (*Handler) GetUserTokenUsage

func (h *Handler) GetUserTokenUsage(c echo.Context) error

GetUserTokenUsage handles GET /api/v1/metrics/tokens/users

func (*Handler) GetUserTokenUsageByID

func (h *Handler) GetUserTokenUsageByID(c echo.Context) error

GetUserTokenUsageByID handles GET /api/v1/metrics/tokens/users/:user_id

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(g *echo.Group)

RegisterRoutes registers the metrics routes.

func (*Handler) ResetMetrics

func (h *Handler) ResetMetrics(c echo.Context) error

ResetMetrics handles POST /api/v1/metrics/reset

type HourlyStats

type HourlyStats struct {
	Hour        time.Time `json:"hour"`
	Calls       int64     `json:"calls"`
	SuccessRate float64   `json:"success_rate"`
}

HourlyStats represents hourly statistics.

type LatencySampleData

type LatencySampleData struct {
	Model       string    `json:"model"`
	SampleType  string    `json:"sample_type"` // "latency", "tps", "ttft", "decode"
	Samples     []float64 `json:"samples"`
	TotalValue  float64   `json:"total_value"`
	MinValue    float64   `json:"min_value"`
	MaxValue    float64   `json:"max_value"`
	SampleCount int64     `json:"sample_count"`
}

LatencySampleData represents latency sample data for persistence.

type LatencySampleExport

type LatencySampleExport struct {
	Model       string
	SampleType  string // "latency", "tps", "ttft", "decode"
	Samples     []float64
	TotalValue  float64
	MinValue    float64
	MaxValue    float64
	SampleCount int64
}

LatencySampleExport represents exported latency sample data.

type LatencyStats

type LatencyStats struct {
	// Basic statistics (ms)
	Min float64 `json:"min_ms"`
	Max float64 `json:"max_ms"`
	Avg float64 `json:"avg_ms"`

	// Percentiles (ms)
	P50 float64 `json:"p50_ms"`
	P90 float64 `json:"p90_ms"`
	P95 float64 `json:"p95_ms"`
	P99 float64 `json:"p99_ms"`

	// Sample count
	Samples int64 `json:"samples"`
}

LatencyStats represents latency statistics.

type LatencyTracker

type LatencyTracker struct {
	// contains filtered or unexported fields
}

LatencyTracker tracks latency and speed metrics.

func NewLatencyTracker

func NewLatencyTracker(maxSamples int) *LatencyTracker

NewLatencyTracker creates a new LatencyTracker.

func (*LatencyTracker) ExportLatencySamples

func (t *LatencyTracker) ExportLatencySamples() []LatencySampleExport

ExportLatencySamples exports all latency samples for persistence.

func (*LatencyTracker) GetAllModelLatencyStats

func (t *LatencyTracker) GetAllModelLatencyStats() map[string]*LatencyStats

GetAllModelLatencyStats returns latency statistics for all models.

func (*LatencyTracker) GetAllModelSpeedStats

func (t *LatencyTracker) GetAllModelSpeedStats() map[string]*GenerationSpeed

GetAllModelSpeedStats returns speed statistics for all models.

func (*LatencyTracker) GetLatencyStats

func (t *LatencyTracker) GetLatencyStats() *LatencyStats

GetLatencyStats returns global latency statistics.

func (*LatencyTracker) GetLatencyStatsByModel

func (t *LatencyTracker) GetLatencyStatsByModel(model string) *LatencyStats

GetLatencyStatsByModel returns latency statistics for a specific model.

func (*LatencyTracker) GetModelSpeedResponses

func (t *LatencyTracker) GetModelSpeedResponses() []ModelSpeedResponse

GetModelSpeedResponses returns speed responses for all models.

func (*LatencyTracker) GetSpeedPercentiles

func (t *LatencyTracker) GetSpeedPercentiles() *SpeedPercentiles

GetSpeedPercentiles returns speed percentiles.

func (*LatencyTracker) GetSpeedPercentilesByModel

func (t *LatencyTracker) GetSpeedPercentilesByModel(model string) *SpeedPercentiles

GetSpeedPercentilesByModel returns speed percentiles for a specific model.

func (*LatencyTracker) GetSpeedStats

func (t *LatencyTracker) GetSpeedStats() *GenerationSpeed

GetSpeedStats returns global speed statistics.

func (*LatencyTracker) GetSpeedStatsByModel

func (t *LatencyTracker) GetSpeedStatsByModel(model string) *GenerationSpeed

GetSpeedStatsByModel returns speed statistics for a specific model.

func (*LatencyTracker) LoadLatencySamples

func (t *LatencyTracker) LoadLatencySamples(exports []LatencySampleExport)

LoadLatencySamples loads latency samples from persisted data.

func (*LatencyTracker) RecordLatency

func (t *LatencyTracker) RecordLatency(model string, latencyMs float64)

RecordLatency records a latency sample.

func (*LatencyTracker) RecordSpeed

func (t *LatencyTracker) RecordSpeed(model string, tokensPerSecond, ttftMs, decodeSpeed float64)

RecordSpeed records speed metrics.

func (*LatencyTracker) RecordTimeBreakdown

func (t *LatencyTracker) RecordTimeBreakdown(model string, breakdown TimeBreakdown)

RecordTimeBreakdown records a time breakdown for a request.

func (*LatencyTracker) Reset

func (t *LatencyTracker) Reset()

Reset resets all latency and speed data.

type MetricsHistory

type MetricsHistory struct {
	Metrics         []SystemMetrics `json:"metrics"`
	IntervalSeconds int             `json:"interval_seconds"`
}

MetricsHistory contains historical metrics data

type MetricsQuery

type MetricsQuery struct {
	Period    string    `json:"period"`
	StartTime time.Time `json:"start_time,omitempty"`
	EndTime   time.Time `json:"end_time,omitempty"`
	Model     string    `json:"model,omitempty"`
	Limit     int       `json:"limit,omitempty"`
}

MetricsQuery represents a query for metrics data.

type MetricsStore

type MetricsStore interface {
	// Write writes a single point to the store.
	Write(ctx context.Context, point *Point) error

	// WriteBatch writes multiple points to the store.
	WriteBatch(ctx context.Context, points []*Point) error

	// Query executes a query and returns results.
	Query(ctx context.Context, query string) (*QueryResult, error)

	// QueryRange queries metrics within a time range.
	QueryRange(ctx context.Context, measurement string, start, end time.Time, aggregation string) (*QueryResult, error)

	// Close closes the store connection.
	Close() error
}

MetricsStore defines the interface for metrics storage.

type MetricsWriter

type MetricsWriter struct {
	// contains filtered or unexported fields
}

MetricsWriter coordinates metrics collection and storage.

func NewMetricsWriter

func NewMetricsWriter(store MetricsStore, config *WriterConfig) *MetricsWriter

NewMetricsWriter creates a new MetricsWriter.

func (*MetricsWriter) GetAllModelUsage

func (w *MetricsWriter) GetAllModelUsage() []ModelTokenUsage

GetAllModelUsage returns token usage for all models.

func (*MetricsWriter) GetAllUserUsage

func (w *MetricsWriter) GetAllUserUsage() []UserTokenUsage

GetAllUserUsage returns token usage for all users.

func (*MetricsWriter) GetCallStats

func (w *MetricsWriter) GetCallStats() *CallStats

GetCallStats returns current call statistics.

func (*MetricsWriter) GetCurrentProcessMetrics

func (w *MetricsWriter) GetCurrentProcessMetrics() (*ProcessMetrics, error)

GetCurrentProcessMetrics returns metrics for the current process.

func (*MetricsWriter) GetDB

func (w *MetricsWriter) GetDB() *sql.DB

GetDB returns the underlying *sql.DB for the metrics SQLite store. Returns nil if no SQLite store is configured.

func (*MetricsWriter) GetLatencyStats

func (w *MetricsWriter) GetLatencyStats() *LatencyStats

GetLatencyStats returns current latency statistics.

func (*MetricsWriter) GetModelStats

func (w *MetricsWriter) GetModelStats() []ModelStats

GetModelStats returns statistics for all models with cost data.

func (*MetricsWriter) GetReadDB

func (w *MetricsWriter) GetReadDB() *sql.DB

GetReadDB returns the underlying read *sql.DB for the metrics SQLite store. Returns nil if no SQLite store is configured.

func (*MetricsWriter) GetResourceHistory

func (w *MetricsWriter) GetResourceHistory() []ResourceHistory

GetResourceHistory returns resource history.

func (*MetricsWriter) GetSpeedStats

func (w *MetricsWriter) GetSpeedStats() *GenerationSpeed

GetSpeedStats returns current speed statistics.

func (*MetricsWriter) GetSystemMetrics

func (w *MetricsWriter) GetSystemMetrics() *SystemResourceMetrics

GetSystemMetrics returns current system metrics.

func (*MetricsWriter) GetTokenUsage

func (w *MetricsWriter) GetTokenUsage() *TokenUsage

GetTokenUsage returns current token usage.

func (*MetricsWriter) GetUserTokenUsage

func (w *MetricsWriter) GetUserTokenUsage(userID string) *UserTokenUsage

GetUserTokenUsage returns token usage for a specific user.

func (*MetricsWriter) RecordAPICall

func (w *MetricsWriter) RecordAPICall(model string, success bool, latencyMs float64, inputTokens, outputTokens, cacheRead, cacheWrite int64, errorType string)

RecordAPICall records an API call with all metrics.

func (*MetricsWriter) RecordAPICallForUser

func (w *MetricsWriter) RecordAPICallForUser(userID, model string, success bool, latencyMs float64, inputTokens, outputTokens, cacheRead, cacheWrite int64, errorType string)

RecordAPICallForUser records an API call with all metrics including user tracking.

func (*MetricsWriter) RecordCounter

func (w *MetricsWriter) RecordCounter(name string, value int64, tags map[string]string)

RecordCounter records a lightweight runtime counter with optional tags.

func (*MetricsWriter) RecordSpeed

func (w *MetricsWriter) RecordSpeed(model string, tokensPerSecond, ttftMs, decodeSpeed float64)

RecordSpeed records speed metrics.

func (*MetricsWriter) Reset

func (w *MetricsWriter) Reset()

Reset resets all metrics.

func (*MetricsWriter) Start

func (w *MetricsWriter) Start()

Start starts background metrics collection.

func (*MetricsWriter) Stop

func (w *MetricsWriter) Stop()

Stop stops background metrics collection.

type ModelComparison

type ModelComparison struct {
	Period         string               `json:"period"`
	Models         []ModelStats         `json:"models"`
	Recommendation *ModelRecommendation `json:"recommendation,omitempty"`
	Insights       []string             `json:"insights,omitempty"`
}

ModelComparison represents a comparison between models.

type ModelDataPoint

type ModelDataPoint struct {
	Timestamp     time.Time `json:"timestamp"`
	Calls         int64     `json:"calls"`
	SuccessRate   float64   `json:"success_rate"`
	AvgLatency    float64   `json:"avg_latency_ms"`
	TotalTokens   int64     `json:"total_tokens"`
	EstimatedCost float64   `json:"estimated_cost"`
}

ModelDataPoint represents a single data point in model statistics history.

type ModelRecommendation

type ModelRecommendation struct {
	BestPerformance string `json:"best_performance"` // Best quality output
	BestCostValue   string `json:"best_cost_value"`  // Best cost-effectiveness
	MostReliable    string `json:"most_reliable"`    // Most stable
	Fastest         string `json:"fastest"`          // Fastest response
}

ModelRecommendation represents model recommendations based on statistics.

type ModelSpeedResponse

type ModelSpeedResponse struct {
	Model       string            `json:"model"`
	Current     *SpeedStats       `json:"current"`
	Average     *SpeedStats       `json:"average"`
	Percentiles *SpeedPercentiles `json:"percentiles"`
}

ModelSpeedResponse represents speed response for a model.

type ModelStats

type ModelStats struct {
	Model           string  `json:"model"`
	Calls           int64   `json:"calls"`
	SuccessfulCalls int64   `json:"successful_calls"`
	FailedCalls     int64   `json:"failed_calls"`
	SuccessRate     float64 `json:"success_rate"`

	// Latency statistics (ms)
	AvgLatency float64 `json:"avg_latency_ms"`
	P50Latency float64 `json:"p50_latency_ms"`
	P95Latency float64 `json:"p95_latency_ms"`
	P99Latency float64 `json:"p99_latency_ms"`

	// Token statistics
	InputTokens      int64 `json:"input_tokens"`
	OutputTokens     int64 `json:"output_tokens"`
	TotalTokens      int64 `json:"total_tokens"`
	CacheReadTokens  int64 `json:"cache_read_tokens"`
	CacheWriteTokens int64 `json:"cache_write_tokens"`

	// Performance statistics
	AvgTokensPerSecond  float64 `json:"avg_tokens_per_second"`
	AvgTimeToFirstToken float64 `json:"avg_ttft_ms"`

	// Cost statistics
	EstimatedCost float64 `json:"estimated_cost"`
}

ModelStats represents statistics for a specific model.

type ModelStatsHistory

type ModelStatsHistory struct {
	Model      string           `json:"model"`
	Period     string           `json:"period"` // hourly, daily, monthly
	DataPoints []ModelDataPoint `json:"data_points"`
}

ModelStatsHistory represents historical statistics for a model.

type ModelStatsResponse

type ModelStatsResponse struct {
	Period  string        `json:"period"`
	Models  []ModelStats  `json:"models"`
	Summary *StatsSummary `json:"summary,omitempty"`
}

ModelStatsResponse represents the API response for model statistics.

type ModelTokenUsage

type ModelTokenUsage struct {
	Model            string  `json:"model"`
	InputTokens      int64   `json:"input_tokens"`
	OutputTokens     int64   `json:"output_tokens"`
	TotalTokens      int64   `json:"total_tokens"`
	CacheReadTokens  int64   `json:"cache_read_tokens"`
	CacheWriteTokens int64   `json:"cache_write_tokens"`
	EstimatedCost    float64 `json:"estimated_cost"`
}

ModelTokenUsage represents token usage for a specific model.

type Point

type Point struct {
	// Measurement is the metric name (e.g., "api_calls", "token_usage")
	Measurement string

	// Tags are indexed metadata (e.g., model, status)
	Tags map[string]string

	// Fields are the actual metric values
	Fields map[string]interface{}

	// Timestamp is when the metric was recorded
	Timestamp time.Time
}

Point represents a single metric data point.

func NewPoint

func NewPoint(measurement string) *Point

NewPoint creates a new Point with the given measurement.

func (*Point) AddField

func (p *Point) AddField(key string, value interface{}) *Point

AddField adds a field to the point.

func (*Point) AddTag

func (p *Point) AddTag(key, value string) *Point

AddTag adds a tag to the point.

func (*Point) SetTimestamp

func (p *Point) SetTimestamp(t time.Time) *Point

SetTimestamp sets the timestamp for the point.

type ProcessMetrics

type ProcessMetrics struct {
	// Process info
	PID     int    `json:"pid"`
	Command string `json:"command"`
	State   string `json:"state"` // running, sleeping, etc.

	// CPU usage
	CPUPercent float64 `json:"cpu_percent"`
	CPUTime    float64 `json:"cpu_time_s"`

	// Memory usage
	MemoryRSS     int64   `json:"memory_rss_bytes"`
	MemoryVMS     int64   `json:"memory_vms_bytes"`
	MemoryPercent float64 `json:"memory_percent"`

	// I/O statistics
	IOReadBytes  int64 `json:"io_read_bytes"`
	IOWriteBytes int64 `json:"io_write_bytes"`

	// Threads
	NumThreads int `json:"num_threads"`

	// Runtime
	StartTime time.Time     `json:"start_time"`
	Uptime    time.Duration `json:"uptime"`
}

ProcessMetrics represents CLI process metrics.

type ProcessMetricsResponse

type ProcessMetricsResponse struct {
	Processes []ProcessMetrics `json:"processes"`
	Summary   *ProcessSummary  `json:"summary"`
}

ProcessMetricsResponse represents the API response for process metrics.

type ProcessSummary

type ProcessSummary struct {
	TotalProcesses   int     `json:"total_processes"`
	TotalCPUPercent  float64 `json:"total_cpu_percent"`
	TotalMemoryBytes int64   `json:"total_memory_bytes"`
}

ProcessSummary represents a summary of process metrics.

type QueryResult

type QueryResult struct {
	// Series contains the result series
	Series []Series
}

QueryResult represents the result of a query.

type ResourceHistory

type ResourceHistory struct {
	Timestamp time.Time `json:"timestamp"`
	CPU       float64   `json:"cpu_percent"`
	Memory    float64   `json:"memory_percent"`
	Disk      float64   `json:"disk_percent"`
}

ResourceHistory represents resource history data point.

type RetentionPolicy

type RetentionPolicy struct {
	Name     string
	Duration time.Duration
	Default  bool
}

RetentionPolicy defines a retention policy configuration.

type SQLiteStore

type SQLiteStore struct {
	// contains filtered or unexported fields
}

SQLiteStore implements MetricsStore using SQLite for persistence.

func NewSQLiteStore

func NewSQLiteStore(dbPath string) (*SQLiteStore, error)

NewSQLiteStore creates a new SQLite-based metrics store.

func NewSQLiteStoreWithDB

func NewSQLiteStoreWithDB(db *sql.DB) (*SQLiteStore, error)

NewSQLiteStoreWithDB reuses an existing SQLite database for metrics persistence.

func NewSQLiteStoreWithReadDB

func NewSQLiteStoreWithReadDB(writeDB, readDB *sql.DB) (*SQLiteStore, error)

NewSQLiteStoreWithReadDB reuses existing SQLite write/read handles for metrics persistence.

func (*SQLiteStore) Cleanup

func (s *SQLiteStore) Cleanup(ctx context.Context, retention time.Duration) error

Cleanup removes old metrics data.

func (*SQLiteStore) Close

func (s *SQLiteStore) Close() error

Close closes the database connection when this store owns it.

func (*SQLiteStore) LoadLatencySamples

func (s *SQLiteStore) LoadLatencySamples(ctx context.Context) ([]LatencySampleData, error)

LoadLatencySamples loads latency samples from the database.

func (*SQLiteStore) LoadModelMetrics

func (s *SQLiteStore) LoadModelMetrics(ctx context.Context) ([]ModelStats, error)

LoadModelMetrics loads model metrics from the database.

func (*SQLiteStore) LoadModelTokenUsage

func (s *SQLiteStore) LoadModelTokenUsage(ctx context.Context) ([]ModelTokenUsage, error)

LoadModelTokenUsage loads token usage for all models.

func (*SQLiteStore) LoadTokenUsage

func (s *SQLiteStore) LoadTokenUsage(ctx context.Context) (*TokenUsage, error)

LoadTokenUsage loads global token usage.

func (*SQLiteStore) Query

func (s *SQLiteStore) Query(ctx context.Context, query string) (*QueryResult, error)

Query executes a query (not fully implemented for SQLite).

func (*SQLiteStore) QueryRange

func (s *SQLiteStore) QueryRange(ctx context.Context, measurement string, start, end time.Time, aggregation string) (*QueryResult, error)

QueryRange queries metrics within a time range.

func (*SQLiteStore) SaveLatencySamples

func (s *SQLiteStore) SaveLatencySamples(ctx context.Context, samples []LatencySampleData) error

SaveLatencySamples saves latency samples to the database.

func (*SQLiteStore) SaveModelMetrics

func (s *SQLiteStore) SaveModelMetrics(ctx context.Context, stats []ModelStats) error

SaveModelMetrics saves or updates model metrics.

func (*SQLiteStore) SaveModelTokenUsage

func (s *SQLiteStore) SaveModelTokenUsage(ctx context.Context, usages []ModelTokenUsage) error

SaveModelTokenUsage saves token usage for all models.

func (*SQLiteStore) SaveTokenUsage

func (s *SQLiteStore) SaveTokenUsage(ctx context.Context, usage *TokenUsage) error

SaveTokenUsage saves global token usage.

func (*SQLiteStore) Write

func (s *SQLiteStore) Write(ctx context.Context, point *Point) error

Write writes a single point to the store.

func (*SQLiteStore) WriteBatch

func (s *SQLiteStore) WriteBatch(ctx context.Context, points []*Point) error

WriteBatch writes multiple points to the store.

type Series

type Series struct {
	// Name is the measurement name
	Name string

	// Tags are the series tags
	Tags map[string]string

	// Columns are the column names
	Columns []string

	// Values are the data rows
	Values [][]interface{}
}

Series represents a single series in the query result.

type SpeedPercentiles

type SpeedPercentiles struct {
	TTFTP50Ms float64 `json:"ttft_p50_ms"`
	TTFTP95Ms float64 `json:"ttft_p95_ms"`
	TTFTP99Ms float64 `json:"ttft_p99_ms"`
	TPSP50    float64 `json:"tps_p50"`
	TPSP95    float64 `json:"tps_p95"`
	TPSP99    float64 `json:"tps_p99"`
}

SpeedPercentiles represents speed percentiles.

type SpeedResponse

type SpeedResponse struct {
	Current     *SpeedStats       `json:"current"`
	Average     *SpeedStats       `json:"average"`
	Percentiles *SpeedPercentiles `json:"percentiles"`
}

SpeedResponse represents the API response for speed statistics.

type SpeedStats

type SpeedStats struct {
	TokensPerSecond    float64 `json:"tokens_per_second"`
	TimeToFirstTokenMs float64 `json:"time_to_first_token_ms"`
	DecodeSpeed        float64 `json:"decode_speed"`
}

SpeedStats represents speed statistics.

type StatsSummary

type StatsSummary struct {
	TotalCalls  int64   `json:"total_calls"`
	TotalTokens int64   `json:"total_tokens"`
	TotalCost   float64 `json:"total_cost"`
}

StatsSummary represents a summary of statistics.

type StoreConfig

type StoreConfig struct {
	// Connection
	URL      string
	Database string
	Username string
	Password string

	// Retention policies
	RetentionPolicies []RetentionPolicy

	// Collection settings
	CollectionInterval time.Duration
	BatchSize          int
	FlushInterval      time.Duration
}

StoreConfig contains configuration for the metrics store.

func DefaultStoreConfig

func DefaultStoreConfig() *StoreConfig

DefaultStoreConfig returns the default store configuration.

type SystemMetrics

type SystemMetrics struct {
	Timestamp                      time.Time `json:"timestamp"`
	CPUPercent                     float64   `json:"cpu_percent"`
	MemoryUsedBytes                uint64    `json:"memory_used_bytes"`
	MemoryRSSBytes                 uint64    `json:"memory_rss_bytes"`
	MemoryTotalBytes               uint64    `json:"memory_total_bytes"`
	Goroutines                     int       `json:"goroutines"`
	GCPauseNs                      uint64    `json:"gc_pause_ns"`
	HeapAllocBytes                 uint64    `json:"heap_alloc_bytes"`
	HeapSysBytes                   uint64    `json:"heap_sys_bytes"`
	StackInuseBytes                uint64    `json:"stack_inuse_bytes"`
	ConversationCacheEntries       int       `json:"conversation_cache_entries,omitempty"`
	ConversationCacheBytes         uint64    `json:"conversation_cache_bytes,omitempty"`
	WarmupCacheEntries             int       `json:"warmup_cache_entries,omitempty"`
	WarmupCacheBytes               uint64    `json:"warmup_cache_bytes,omitempty"`
	PromptToolSurfaceRefs          int       `json:"prompt_tool_surface_refs,omitempty"`
	PromptToolSurfaceSharedEntries int       `json:"prompt_tool_surface_shared_entries,omitempty"`
	PromptToolSurfaceSharedBytes   uint64    `json:"prompt_tool_surface_shared_bytes,omitempty"`
	ProviderAffinityEntries        int       `json:"provider_affinity_entries,omitempty"`
}

SystemMetrics represents a snapshot of system metrics at a point in time

type SystemMonitor

type SystemMonitor struct {
	// contains filtered or unexported fields
}

SystemMonitor monitors system resources.

func NewSystemMonitor

func NewSystemMonitor(maxHistory int, diskPath string) *SystemMonitor

NewSystemMonitor creates a new SystemMonitor.

func (*SystemMonitor) Collect

func (m *SystemMonitor) Collect() error

Collect collects current system metrics.

func (*SystemMonitor) CollectProcessMetrics

func (m *SystemMonitor) CollectProcessMetrics(pid int32) (*ProcessMetrics, error)

CollectProcessMetrics collects metrics for a specific process.

func (*SystemMonitor) GetAllProcessMetrics

func (m *SystemMonitor) GetAllProcessMetrics() []ProcessMetrics

GetAllProcessMetrics returns metrics for all tracked processes.

func (*SystemMonitor) GetCurrentProcessMetrics

func (m *SystemMonitor) GetCurrentProcessMetrics() (*ProcessMetrics, error)

GetCurrentProcessMetrics returns metrics for the current process.

func (*SystemMonitor) GetProcessMetrics

func (m *SystemMonitor) GetProcessMetrics(pid int32) *ProcessMetrics

GetProcessMetrics returns metrics for a specific process.

func (*SystemMonitor) GetResourceHistory

func (m *SystemMonitor) GetResourceHistory() []ResourceHistory

GetResourceHistory returns resource history in chronological order.

func (*SystemMonitor) GetSystemMetrics

func (m *SystemMonitor) GetSystemMetrics() *SystemResourceMetrics

GetSystemMetrics returns current system metrics.

func (*SystemMonitor) Reset

func (m *SystemMonitor) Reset()

Reset clears all collected metrics.

type SystemResourceMetrics

type SystemResourceMetrics struct {
	// CPU
	CPUCount        int     `json:"cpu_count"`
	CPUUsagePercent float64 `json:"cpu_usage_percent"`
	LoadAvg1        float64 `json:"load_avg_1"`
	LoadAvg5        float64 `json:"load_avg_5"`
	LoadAvg15       float64 `json:"load_avg_15"`

	// Memory
	MemoryTotal   int64   `json:"memory_total_bytes"`
	MemoryUsed    int64   `json:"memory_used_bytes"`
	MemoryFree    int64   `json:"memory_free_bytes"`
	MemoryPercent float64 `json:"memory_percent"`

	// Disk
	DiskTotal   int64   `json:"disk_total_bytes"`
	DiskUsed    int64   `json:"disk_used_bytes"`
	DiskFree    int64   `json:"disk_free_bytes"`
	DiskPercent float64 `json:"disk_percent"`

	// Network
	NetworkBytesSent int64 `json:"network_bytes_sent"`
	NetworkBytesRecv int64 `json:"network_bytes_recv"`
}

SystemResourceMetrics represents system resource metrics.

type TimeBreakdown

type TimeBreakdown struct {
	QueueTime        time.Duration `json:"queue_time_ms"`
	TimeToFirstToken time.Duration `json:"ttft_ms"`
	GenerationTime   time.Duration `json:"generation_time_ms"`
	TotalTime        time.Duration `json:"total_time_ms"`
}

TimeBreakdown represents time breakdown for a request.

type TimeBreakdownTracker

type TimeBreakdownTracker struct {
	// contains filtered or unexported fields
}

TimeBreakdownTracker tracks time breakdown for requests.

func NewTimeBreakdownTracker

func NewTimeBreakdownTracker(maxSamples int) *TimeBreakdownTracker

NewTimeBreakdownTracker creates a new TimeBreakdownTracker.

func (*TimeBreakdownTracker) GetAverageBreakdown

func (t *TimeBreakdownTracker) GetAverageBreakdown() *TimeBreakdown

GetAverageBreakdown returns the average time breakdown.

func (*TimeBreakdownTracker) GetAverageBreakdownByModel

func (t *TimeBreakdownTracker) GetAverageBreakdownByModel(model string) *TimeBreakdown

GetAverageBreakdownByModel returns the average time breakdown for a model.

func (*TimeBreakdownTracker) RecordBreakdown

func (t *TimeBreakdownTracker) RecordBreakdown(model string, breakdown TimeBreakdown)

RecordBreakdown records a time breakdown.

func (*TimeBreakdownTracker) Reset

func (t *TimeBreakdownTracker) Reset()

Reset resets all breakdown data.

type TokenPricing

type TokenPricing struct {
	// Model matching pattern (supports wildcards)
	Pattern string `json:"pattern" yaml:"pattern"` // e.g., "opus*", "sonnet*", "haiku*"

	// Price per million tokens (USD)
	InputPrice  float64 `json:"input_price" yaml:"input"`
	OutputPrice float64 `json:"output_price" yaml:"output"`
	CacheRead   float64 `json:"cache_read" yaml:"cache_read"`
	CacheWrite  float64 `json:"cache_write" yaml:"cache_write"`
}

TokenPricing represents token pricing configuration.

func DefaultTokenPricing

func DefaultTokenPricing() []TokenPricing

DefaultTokenPricing returns the default token pricing configuration.

type TokenTracker

type TokenTracker struct {
	// contains filtered or unexported fields
}

TokenTracker tracks token usage and calculates costs.

func NewTokenTracker

func NewTokenTracker() *TokenTracker

NewTokenTracker creates a new TokenTracker with default pricing.

func NewTokenTrackerWithPricing

func NewTokenTrackerWithPricing(pricing []TokenPricing) *TokenTracker

NewTokenTrackerWithPricing creates a new TokenTracker with custom pricing.

func (*TokenTracker) CalculateCost

func (t *TokenTracker) CalculateCost(model string, inputTokens, outputTokens, cacheRead, cacheWrite int64) float64

CalculateCost calculates the cost for given token usage.

func (*TokenTracker) GetAllModelUsage

func (t *TokenTracker) GetAllModelUsage() []ModelTokenUsage

GetAllModelUsage returns token usage for all models.

func (*TokenTracker) GetAllUserUsage

func (t *TokenTracker) GetAllUserUsage() []UserTokenUsage

GetAllUserUsage returns token usage for all users.

func (*TokenTracker) GetPricing

func (t *TokenTracker) GetPricing() []TokenPricing

GetPricing returns the pricing configuration.

func (*TokenTracker) GetPricingForModel

func (t *TokenTracker) GetPricingForModel(model string) *TokenPricing

GetPricingForModel returns the pricing for a specific model.

func (*TokenTracker) GetTokenUsage

func (t *TokenTracker) GetTokenUsage() *TokenUsage

GetTokenUsage returns global token usage.

func (*TokenTracker) GetTokenUsageByModel

func (t *TokenTracker) GetTokenUsageByModel(model string) *TokenUsage

GetTokenUsageByModel returns token usage for a specific model.

func (*TokenTracker) GetUserTokenUsage

func (t *TokenTracker) GetUserTokenUsage(userID string) *UserTokenUsage

GetUserTokenUsage returns token usage for a specific user.

func (*TokenTracker) LoadModelUsages

func (t *TokenTracker) LoadModelUsages(usages []ModelTokenUsage)

LoadModelUsages loads model token usages from persisted data.

func (*TokenTracker) LoadUsage

func (t *TokenTracker) LoadUsage(usage *TokenUsage)

LoadUsage loads global token usage from persisted data.

func (*TokenTracker) RecordTokenUsage

func (t *TokenTracker) RecordTokenUsage(model string, inputTokens, outputTokens, cacheRead, cacheWrite int64)

RecordTokenUsage records token usage for a call.

func (*TokenTracker) RecordTokenUsageForUser

func (t *TokenTracker) RecordTokenUsageForUser(userID, model string, inputTokens, outputTokens, cacheRead, cacheWrite int64)

RecordTokenUsageForUser records token usage for a call with user tracking.

func (*TokenTracker) Reset

func (t *TokenTracker) Reset()

Reset resets all token usage.

func (*TokenTracker) SetPricing

func (t *TokenTracker) SetPricing(pricing []TokenPricing)

SetPricing updates the pricing configuration.

type TokenUsage

type TokenUsage struct {
	// Input/Output
	InputTokens  int64 `json:"input_tokens"`
	OutputTokens int64 `json:"output_tokens"`
	TotalTokens  int64 `json:"total_tokens"`

	// Cache related
	CacheReadTokens  int64 `json:"cache_read_tokens"`
	CacheWriteTokens int64 `json:"cache_write_tokens"`

	// Cost estimation (USD)
	EstimatedCost float64 `json:"estimated_cost"`
}

TokenUsage represents token usage statistics.

type TokenUsageResponse

type TokenUsageResponse struct {
	Period  string            `json:"period"`
	Usage   *TokenUsage       `json:"usage"`
	ByModel []ModelTokenUsage `json:"by_model,omitempty"`
}

TokenUsageResponse represents the API response for token usage.

type UserTokenUsage

type UserTokenUsage struct {
	UserID           string  `json:"user_id"`
	InputTokens      int64   `json:"input_tokens"`
	OutputTokens     int64   `json:"output_tokens"`
	TotalTokens      int64   `json:"total_tokens"`
	CacheReadTokens  int64   `json:"cache_read_tokens"`
	CacheWriteTokens int64   `json:"cache_write_tokens"`
	EstimatedCost    float64 `json:"estimated_cost"`
	RequestCount     int64   `json:"request_count"`
}

UserTokenUsage represents token usage for a specific user.

type UserTokenUsageResponse

type UserTokenUsageResponse struct {
	Period  string            `json:"period"`
	Users   []UserTokenUsage  `json:"users"`
	Summary *UserUsageSummary `json:"summary,omitempty"`
}

UserTokenUsageResponse represents the API response for user token usage.

type UserUsageSummary

type UserUsageSummary struct {
	TotalUsers    int     `json:"total_users"`
	TotalTokens   int64   `json:"total_tokens"`
	TotalCost     float64 `json:"total_cost"`
	TotalRequests int64   `json:"total_requests"`
}

UserUsageSummary represents a summary of user token usage.

type WriterConfig

type WriterConfig struct {
	// How often to collect and write metrics
	CollectionInterval time.Duration

	// Maximum samples to keep in memory
	MaxSamples int

	// Enable system metrics collection
	EnableSystemMetrics bool

	// System metrics collection interval
	SystemMetricsInterval time.Duration

	// Disk path for disk usage monitoring
	DiskPath string

	// SQLite database path for persistence when using a dedicated metrics DB.
	SQLiteDBPath string

	// SharedSQLiteDB reuses an existing SQLite database instead of opening metrics.db.
	SharedSQLiteDB *sql.DB

	// SharedSQLiteReadDB optionally provides a separate reader when
	// SharedSQLiteDB points at a writer-only pool.
	SharedSQLiteReadDB *sql.DB

	// Persistence save interval
	PersistenceInterval time.Duration
}

WriterConfig contains configuration for the metrics writer.

func DefaultWriterConfig

func DefaultWriterConfig() *WriterConfig

DefaultWriterConfig returns the default writer configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL