metrics

package
v0.60.3 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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 {
	AgentStats   map[string]*AgentStat `json:"agent_stats"`
	ErrorsByType map[string]int64      `json:"errors_by_type"`
	RequestCount int64                 `json:"request_count"`
	SuccessCount int64                 `json:"success_count"`
	LatencyP50   time.Duration         `json:"latency_p50"`
	LatencyP95   time.Duration         `json:"latency_p95"`
}

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)
}

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.

func (*Persister) Flush

func (p *Persister) Flush(ctx context.Context) error

Flush immediately persists all completed hour buckets to the database.

func (*Persister) Start

func (p *Persister) Start()

Start begins the background persistence and cleanup tasks.

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) Flush

func (s *Service) Flush(ctx context.Context) error

Flush forces an immediate flush of metrics to the database.

func (*Service) GetStats

func (s *Service) GetStats(ctx context.Context, timeRange TimeRange) (*AgentMetrics, error)

GetStats retrieves aggregated statistics for the given time range.

func (*Service) HasPersistence

func (s *Service) HasPersistence() bool

HasPersistence returns true if metrics persistence is enabled.

func (*Service) RecordRequest

func (s *Service) RecordRequest(_ context.Context, agentType string, latency time.Duration, success bool)

RecordRequest records an agent request metric.

func (*Service) RecordToolCall

func (s *Service) RecordToolCall(_ context.Context, toolName string, latency time.Duration, success bool)

RecordToolCall records a tool call metric.

type TimeRange

type TimeRange struct {
	Start time.Time `json:"start"`
	End   time.Time `json:"end"`
}

TimeRange represents a time range for querying metrics.

type ToolSnapshot

type ToolSnapshot struct {
	HourBucket   time.Time
	ToolName     string
	CallCount    int64
	SuccessCount int64
	LatencySumMs int64
}

ToolSnapshot represents a snapshot of tool metrics for persistence.

Jump to

Keyboard shortcuts

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