Documentation
¶
Overview ¶
Package metrics provides the evaluation metrics service interface for AI agents. This interface is consumed by Team B (Assistant+Schedule) and Team C (Memo Enhancement).
Index ¶
- Variables
- type AgentMetrics
- type AgentSnapshot
- type AgentStat
- type Aggregator
- func (a *Aggregator) FlushAgentMetrics(beforeHour time.Time) []*AgentSnapshot
- func (a *Aggregator) FlushToolMetrics(beforeHour time.Time) []*ToolSnapshot
- func (a *Aggregator) GetCurrentStats() *AgentMetrics
- func (a *Aggregator) RecordAgentRequest(agentType string, latency time.Duration, success bool)
- func (a *Aggregator) RecordToolCall(toolName string, latency time.Duration, success bool)
- type MetricsService
- type MockMetricsService
- func (m *MockMetricsService) Clear()
- func (m *MockMetricsService) GetStats(ctx context.Context, timeRange TimeRange) (*AgentMetrics, error)
- func (m *MockMetricsService) RecordRequest(ctx context.Context, agentType string, latency time.Duration, success bool)
- func (m *MockMetricsService) RecordToolCall(ctx context.Context, toolName string, latency time.Duration, success bool)
- type Persister
- type PersisterConfig
- type Service
- func (s *Service) Close()
- func (s *Service) Flush(ctx context.Context) error
- func (s *Service) GetStats(ctx context.Context, timeRange TimeRange) (*AgentMetrics, error)
- func (s *Service) HasPersistence() bool
- func (s *Service) RecordRequest(_ context.Context, agentType string, latency time.Duration, success bool)
- func (s *Service) RecordToolCall(_ context.Context, toolName string, latency time.Duration, success bool)
- type TimeRange
- type ToolSnapshot
Constants ¶
This section is empty.
Variables ¶
var ErrMetricsNotConfigured = errors.New("metrics persistence not configured (requires PostgreSQL)")
ErrMetricsNotConfigured is returned when metrics persistence is not configured.
Functions ¶
This section is empty.
Types ¶
type AgentMetrics ¶
type AgentMetrics struct {
RequestCount int64 `json:"request_count"`
SuccessCount int64 `json:"success_count"`
LatencyP50 time.Duration `json:"latency_p50"`
LatencyP95 time.Duration `json:"latency_p95"`
AgentStats map[string]*AgentStat `json:"agent_stats"`
ErrorsByType map[string]int64 `json:"errors_by_type"`
}
AgentMetrics represents aggregated agent metrics.
type AgentSnapshot ¶
type AgentSnapshot struct {
HourBucket time.Time
AgentType string
RequestCount int64
SuccessCount int64
LatencySumMs int64
LatencyP50Ms int32
LatencyP95Ms int32
}
AgentSnapshot represents a snapshot of agent metrics for persistence.
type AgentStat ¶
type AgentStat struct {
Count int64 `json:"count"`
SuccessRate float32 `json:"success_rate"`
AvgLatency time.Duration `json:"avg_latency"`
}
AgentStat represents statistics for a single agent.
type Aggregator ¶
type Aggregator struct {
// contains filtered or unexported fields
}
Aggregator aggregates metrics in memory before persisting to database.
func NewAggregator ¶
func NewAggregator() *Aggregator
NewAggregator creates a new metrics aggregator.
func (*Aggregator) FlushAgentMetrics ¶
func (a *Aggregator) FlushAgentMetrics(beforeHour time.Time) []*AgentSnapshot
FlushAgentMetrics returns and clears all agent metrics for the given hour. Returns nil if no metrics exist for hours before the current hour.
func (*Aggregator) FlushToolMetrics ¶
func (a *Aggregator) FlushToolMetrics(beforeHour time.Time) []*ToolSnapshot
FlushToolMetrics returns and clears all tool metrics for hours before the given time.
func (*Aggregator) GetCurrentStats ¶
func (a *Aggregator) GetCurrentStats() *AgentMetrics
GetCurrentStats returns aggregated stats from memory for the current hour.
func (*Aggregator) RecordAgentRequest ¶
func (a *Aggregator) RecordAgentRequest(agentType string, latency time.Duration, success bool)
RecordAgentRequest records a single agent request.
func (*Aggregator) RecordToolCall ¶
func (a *Aggregator) RecordToolCall(toolName string, latency time.Duration, success bool)
RecordToolCall records a single tool call.
type MetricsService ¶
type MetricsService interface {
// RecordRequest records request metrics.
RecordRequest(ctx context.Context, agentType string, latency time.Duration, success bool)
// RecordToolCall records tool call metrics.
RecordToolCall(ctx context.Context, toolName string, latency time.Duration, success bool)
// GetStats retrieves statistics data.
GetStats(ctx context.Context, timeRange TimeRange) (*AgentMetrics, error)
}
MetricsService defines the evaluation metrics service interface. Consumers: Team B (Assistant+Schedule), Team C (Memo Enhancement)
type MockMetricsService ¶
type MockMetricsService struct {
// contains filtered or unexported fields
}
MockMetricsService is a mock implementation of MetricsService for testing.
func NewMockMetricsService ¶
func NewMockMetricsService() *MockMetricsService
NewMockMetricsService creates a new MockMetricsService.
func (*MockMetricsService) Clear ¶
func (m *MockMetricsService) Clear()
Clear removes all recorded metrics (for testing).
func (*MockMetricsService) GetStats ¶
func (m *MockMetricsService) GetStats(ctx context.Context, timeRange TimeRange) (*AgentMetrics, error)
GetStats retrieves statistics data.
func (*MockMetricsService) RecordRequest ¶
func (m *MockMetricsService) RecordRequest(ctx context.Context, agentType string, latency time.Duration, success bool)
RecordRequest records request metrics.
func (*MockMetricsService) RecordToolCall ¶
func (m *MockMetricsService) RecordToolCall(ctx context.Context, toolName string, latency time.Duration, success bool)
RecordToolCall records tool call metrics.
type Persister ¶
type Persister struct {
// contains filtered or unexported fields
}
Persister handles periodic persistence of aggregated metrics to the database.
func NewPersister ¶
func NewPersister(s *store.Store, agg *Aggregator, cfg PersisterConfig) *Persister
NewPersister creates a new metrics persister.
func (*Persister) Close ¶
func (p *Persister) Close()
Close stops the persister and waits for goroutines to finish.
type PersisterConfig ¶
type PersisterConfig struct {
FlushInterval time.Duration // How often to flush metrics to DB (default: 1 hour)
RetentionPeriod time.Duration // How long to keep metrics (default: 30 days)
CleanupInterval time.Duration // How often to run cleanup (default: 24 hours)
}
PersisterConfig configures the metrics persister.
func DefaultPersisterConfig ¶
func DefaultPersisterConfig() PersisterConfig
DefaultPersisterConfig returns default persister configuration.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service implements the MetricsService interface with real storage.
func NewService ¶
func NewService(s *store.Store, cfg PersisterConfig) *Service
NewService creates a new metrics service. If store is nil, metrics will only be aggregated in memory (no persistence).
func (*Service) Close ¶
func (s *Service) Close()
Close stops the metrics service and flushes remaining data.
func (*Service) HasPersistence ¶
HasPersistence returns true if metrics persistence is enabled.