logstore

package
v1.1.52 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: Apache-2.0 Imports: 19 Imported by: 4

Documentation

Overview

Package logstore provides a logs store for Bifrost.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = fmt.Errorf("log not found")
)

Functions

This section is empty.

Types

type CleanerConfig added in v1.1.36

type CleanerConfig struct {
	RetentionDays int
}

CleanerConfig holds configuration for the log cleaner

type Config

type Config struct {
	Enabled       bool         `json:"enabled"`
	Type          LogStoreType `json:"type"`
	RetentionDays int          `json:"retention_days"`
	Config        any          `json:"config"`
}

Config represents the configuration for the logs store.

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(data []byte) error

UnmarshalJSON is the custom unmarshal logic for Config

type Log

type Log struct {
	ID                    string    `gorm:"primaryKey;type:varchar(255)" json:"id"`
	ParentRequestID       *string   `gorm:"type:varchar(255)" json:"parent_request_id"`
	Timestamp             time.Time `gorm:"index;not null" json:"timestamp"`
	Object                string    `gorm:"type:varchar(255);index;not null;column:object_type" json:"object"` // text.completion, chat.completion, or embedding
	Provider              string    `gorm:"type:varchar(255);index;not null" json:"provider"`
	Model                 string    `gorm:"type:varchar(255);index;not null" json:"model"`
	NumberOfRetries       int       `gorm:"default:0" json:"number_of_retries"`
	FallbackIndex         int       `gorm:"default:0" json:"fallback_index"`
	SelectedKeyID         string    `gorm:"type:varchar(255);index:idx_logs_selected_key_id" json:"selected_key_id"`
	SelectedKeyName       string    `gorm:"type:varchar(255)" json:"selected_key_name"`
	VirtualKeyID          *string   `gorm:"type:varchar(255);index:idx_logs_virtual_key_id" json:"virtual_key_id"`
	VirtualKeyName        *string   `gorm:"type:varchar(255)" json:"virtual_key_name"`
	InputHistory          string    `gorm:"type:text" json:"-"` // JSON serialized []schemas.ChatMessage
	ResponsesInputHistory string    `gorm:"type:text" json:"-"` // JSON serialized []schemas.ResponsesMessage
	OutputMessage         string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.ChatMessage
	ResponsesOutput       string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.ResponsesMessage
	EmbeddingOutput       string    `gorm:"type:text" json:"-"` // JSON serialized [][]float32
	Params                string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.ModelParameters
	Tools                 string    `gorm:"type:text" json:"-"` // JSON serialized []schemas.Tool
	ToolCalls             string    `gorm:"type:text" json:"-"` // JSON serialized []schemas.ToolCall (For backward compatibility, tool calls are now in the content)
	SpeechInput           string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.SpeechInput
	TranscriptionInput    string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.TranscriptionInput
	SpeechOutput          string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostSpeech
	TranscriptionOutput   string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostTranscribe
	CacheDebug            string    `gorm:"type:text" json:"-"` // JSON serialized *schemas.BifrostCacheDebug
	Latency               *float64  `gorm:"index:idx_logs_latency" json:"latency,omitempty"`
	TokenUsage            string    `gorm:"type:text" json:"-"`                            // JSON serialized *schemas.LLMUsage
	Cost                  *float64  `gorm:"index" json:"cost,omitempty"`                   // Cost in dollars (total cost of the request - includes cache lookup cost)
	Status                string    `gorm:"type:varchar(50);index;not null" json:"status"` // "processing", "success", or "error"
	ErrorDetails          string    `gorm:"type:text" json:"-"`                            // JSON serialized *schemas.BifrostError
	Stream                bool      `gorm:"default:false" json:"stream"`                   // true if this was a streaming response
	ContentSummary        string    `gorm:"type:text" json:"-"`
	RawRequest            string    `gorm:"type:text" json:"raw_request"`  // Populated when `send-back-raw-request` is on
	RawResponse           string    `gorm:"type:text" json:"raw_response"` // Populated when `send-back-raw-response` is on

	// Denormalized token fields for easier querying
	PromptTokens     int `gorm:"default:0" json:"-"`
	CompletionTokens int `gorm:"default:0" json:"-"`
	TotalTokens      int `gorm:"index:idx_logs_total_tokens;default:0" json:"-"`

	CreatedAt time.Time `gorm:"index;not null" json:"created_at"`

	// Virtual fields for JSON output - these will be populated when needed
	InputHistoryParsed          []schemas.ChatMessage                  `gorm:"-" json:"input_history,omitempty"`
	ResponsesInputHistoryParsed []schemas.ResponsesMessage             `gorm:"-" json:"responses_input_history,omitempty"`
	OutputMessageParsed         *schemas.ChatMessage                   `gorm:"-" json:"output_message,omitempty"`
	ResponsesOutputParsed       []schemas.ResponsesMessage             `gorm:"-" json:"responses_output,omitempty"`
	EmbeddingOutputParsed       []schemas.EmbeddingData                `gorm:"-" json:"embedding_output,omitempty"`
	ParamsParsed                interface{}                            `gorm:"-" json:"params,omitempty"`
	ToolsParsed                 []schemas.ChatTool                     `gorm:"-" json:"tools,omitempty"`
	ToolCallsParsed             []schemas.ChatAssistantMessageToolCall `gorm:"-" json:"tool_calls,omitempty"` // For backward compatibility, tool calls are now in the content
	TokenUsageParsed            *schemas.BifrostLLMUsage               `gorm:"-" json:"token_usage,omitempty"`
	ErrorDetailsParsed          *schemas.BifrostError                  `gorm:"-" json:"error_details,omitempty"`
	SpeechInputParsed           *schemas.SpeechInput                   `gorm:"-" json:"speech_input,omitempty"`
	TranscriptionInputParsed    *schemas.TranscriptionInput            `gorm:"-" json:"transcription_input,omitempty"`
	SpeechOutputParsed          *schemas.BifrostSpeechResponse         `gorm:"-" json:"speech_output,omitempty"`
	TranscriptionOutputParsed   *schemas.BifrostTranscriptionResponse  `gorm:"-" json:"transcription_output,omitempty"`
	CacheDebugParsed            *schemas.BifrostCacheDebug             `gorm:"-" json:"cache_debug,omitempty"`

	// Populated in handlers after find using the virtual key id and key id
	VirtualKey  *tables.TableVirtualKey `gorm:"-" json:"virtual_key,omitempty"`  // redacted
	SelectedKey *schemas.Key            `gorm:"-" json:"selected_key,omitempty"` // redacted
}

Log represents a complete log entry for a request/response cycle This is the GORM model with appropriate tags

func (*Log) AfterFind

func (l *Log) AfterFind(tx *gorm.DB) error

AfterFind GORM hook to deserialize JSON fields

func (*Log) BeforeCreate

func (l *Log) BeforeCreate(tx *gorm.DB) error

BeforeCreate GORM hook to set created_at and serialize JSON fields

func (*Log) BeforeSave

func (l *Log) BeforeSave(tx *gorm.DB) error

BeforeSave GORM hook to serialize JSON fields

func (*Log) BuildContentSummary

func (l *Log) BuildContentSummary() string

BuildContentSummary creates a searchable text summary

func (*Log) DeserializeFields

func (l *Log) DeserializeFields() error

DeserializeFields converts JSON strings back to Go structs

func (*Log) SerializeFields

func (l *Log) SerializeFields() error

SerializeFields converts Go structs to JSON strings for storage

func (Log) TableName

func (Log) TableName() string

TableName sets the table name for GORM

type LogRetentionManager added in v1.1.36

type LogRetentionManager interface {
	DeleteLogsBatch(ctx context.Context, cutoff time.Time, batchSize int) (deletedCount int64, err error)
}

LogRetentionManager defines the interface for managing log retention and deletion

type LogStore

type LogStore interface {
	Ping(ctx context.Context) error
	Create(ctx context.Context, entry *Log) error
	CreateIfNotExists(ctx context.Context, entry *Log) error
	FindFirst(ctx context.Context, query any, fields ...string) (*Log, error)
	FindAll(ctx context.Context, query any, fields ...string) ([]*Log, error)
	HasLogs(ctx context.Context) (bool, error)
	SearchLogs(ctx context.Context, filters SearchFilters, pagination PaginationOptions) (*SearchResult, error)
	GetStats(ctx context.Context, filters SearchFilters) (*SearchStats, error)
	Update(ctx context.Context, id string, entry any) error
	BulkUpdateCost(ctx context.Context, updates map[string]float64) error
	Flush(ctx context.Context, since time.Time) error
	Close(ctx context.Context) error
	DeleteLog(ctx context.Context, id string) error
	DeleteLogs(ctx context.Context, ids []string) error
	DeleteLogsBatch(ctx context.Context, cutoff time.Time, batchSize int) (deletedCount int64, err error)
}

LogStore is the interface for the log store.

func NewLogStore

func NewLogStore(ctx context.Context, config *Config, logger schemas.Logger) (LogStore, error)

NewLogStore creates a new log store based on the configuration.

type LogStoreType

type LogStoreType string

LogStoreType represents the type of log store.

const (
	LogStoreTypeSQLite   LogStoreType = "sqlite"
	LogStoreTypePostgres LogStoreType = "postgres"
)

LogStoreTypeSQLite is the type of log store for SQLite.

type LogsCleaner added in v1.1.36

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

LogsCleaner manages the cleanup of old logs

func NewLogsCleaner added in v1.1.36

func NewLogsCleaner(manager LogRetentionManager, config CleanerConfig, logger schemas.Logger) *LogsCleaner

NewLogsCleaner creates a new LogsCleaner instance

func (*LogsCleaner) StartCleanupRoutine added in v1.1.36

func (c *LogsCleaner) StartCleanupRoutine()

StartCleanupRoutine starts a goroutine that periodically cleans up old logs

func (*LogsCleaner) StopCleanupRoutine added in v1.1.36

func (c *LogsCleaner) StopCleanupRoutine()

StopCleanupRoutine gracefully stops the cleanup goroutine

type PaginationOptions

type PaginationOptions struct {
	Limit  int    `json:"limit"`
	Offset int    `json:"offset"`
	SortBy string `json:"sort_by"` // "timestamp", "latency", "tokens", "cost"
	Order  string `json:"order"`   // "asc", "desc"
}

PaginationOptions represents pagination parameters

type PostgresConfig added in v1.1.0

type PostgresConfig struct {
	Host         string `json:"host"`
	Port         string `json:"port"`
	User         string `json:"user"`
	Password     string `json:"password"`
	DBName       string `json:"db_name"`
	SSLMode      string `json:"ssl_mode"`
	MaxIdleConns int    `json:"max_idle_conns"`
	MaxOpenConns int    `json:"max_open_conns"`
}

PostgresConfig represents the configuration for a Postgres database.

type RDBLogStore added in v1.1.0

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

RDBLogStore represents a log store that uses a SQLite database.

func (*RDBLogStore) BulkUpdateCost added in v1.1.49

func (s *RDBLogStore) BulkUpdateCost(ctx context.Context, updates map[string]float64) error

func (*RDBLogStore) Close added in v1.1.0

func (s *RDBLogStore) Close(ctx context.Context) error

Close closes the log store.

func (*RDBLogStore) Create added in v1.1.0

func (s *RDBLogStore) Create(ctx context.Context, entry *Log) error

Create inserts a new log entry into the database.

func (*RDBLogStore) CreateIfNotExists added in v1.1.40

func (s *RDBLogStore) CreateIfNotExists(ctx context.Context, entry *Log) error

CreateIfNotExists inserts a new log entry only if it doesn't already exist. Uses ON CONFLICT DO NOTHING to handle duplicate key errors gracefully.

func (*RDBLogStore) DeleteLog added in v1.1.36

func (s *RDBLogStore) DeleteLog(ctx context.Context, id string) error

DeleteLog deletes a log entry from the database by its ID.

func (*RDBLogStore) DeleteLogs added in v1.1.36

func (s *RDBLogStore) DeleteLogs(ctx context.Context, ids []string) error

DeleteLogs deletes multiple log entries from the database by their IDs.

func (*RDBLogStore) DeleteLogsBatch added in v1.1.36

func (s *RDBLogStore) DeleteLogsBatch(ctx context.Context, cutoff time.Time, batchSize int) (deletedCount int64, err error)

DeleteLogsBatch deletes logs older than the cutoff time in batches.

func (*RDBLogStore) FindAll added in v1.1.0

func (s *RDBLogStore) FindAll(ctx context.Context, query any, fields ...string) ([]*Log, error)

FindAll finds all log entries from the database.

func (*RDBLogStore) FindFirst added in v1.1.0

func (s *RDBLogStore) FindFirst(ctx context.Context, query any, fields ...string) (*Log, error)

FindFirst gets a log entry from the database.

func (*RDBLogStore) Flush added in v1.1.0

func (s *RDBLogStore) Flush(ctx context.Context, since time.Time) error

Flush deletes old log entries from the database.

func (*RDBLogStore) GetStats added in v1.1.31

func (s *RDBLogStore) GetStats(ctx context.Context, filters SearchFilters) (*SearchStats, error)

GetStats calculates statistics for logs matching the given filters.

func (*RDBLogStore) HasLogs added in v1.1.36

func (s *RDBLogStore) HasLogs(ctx context.Context) (bool, error)

HasLogs checks if there are any logs in the database.

func (*RDBLogStore) Ping added in v1.1.8

func (s *RDBLogStore) Ping(ctx context.Context) error

Ping checks if the database is reachable.

func (*RDBLogStore) SearchLogs added in v1.1.0

func (s *RDBLogStore) SearchLogs(ctx context.Context, filters SearchFilters, pagination PaginationOptions) (*SearchResult, error)

SearchLogs searches for logs in the database without calculating statistics.

func (*RDBLogStore) Update added in v1.1.0

func (s *RDBLogStore) Update(ctx context.Context, id string, entry any) error

Update updates a log entry in the database.

type SQLiteConfig

type SQLiteConfig struct {
	Path string `json:"path"`
}

SQLiteConfig represents the configuration for a SQLite database.

type SearchFilters

type SearchFilters struct {
	Providers       []string   `json:"providers,omitempty"`
	Models          []string   `json:"models,omitempty"`
	Status          []string   `json:"status,omitempty"`
	Objects         []string   `json:"objects,omitempty"` // For filtering by request type (chat.completion, text.completion, embedding)
	SelectedKeyIDs  []string   `json:"selected_key_ids,omitempty"`
	VirtualKeyIDs   []string   `json:"virtual_key_ids,omitempty"`
	StartTime       *time.Time `json:"start_time,omitempty"`
	EndTime         *time.Time `json:"end_time,omitempty"`
	MinLatency      *float64   `json:"min_latency,omitempty"`
	MaxLatency      *float64   `json:"max_latency,omitempty"`
	MinTokens       *int       `json:"min_tokens,omitempty"`
	MaxTokens       *int       `json:"max_tokens,omitempty"`
	MinCost         *float64   `json:"min_cost,omitempty"`
	MaxCost         *float64   `json:"max_cost,omitempty"`
	MissingCostOnly bool       `json:"missing_cost_only,omitempty"`
	ContentSearch   string     `json:"content_search,omitempty"`
}

SearchFilters represents the available filters for log searches

type SearchResult

type SearchResult struct {
	Logs       []Log             `json:"logs"`
	Pagination PaginationOptions `json:"pagination"`
	Stats      SearchStats       `json:"stats"`
	HasLogs    bool              `json:"has_logs"`
}

SearchResult represents the result of a log search

type SearchStats

type SearchStats struct {
	TotalRequests  int64   `json:"total_requests"`
	SuccessRate    float64 `json:"success_rate"`    // Percentage of successful requests
	AverageLatency float64 `json:"average_latency"` // Average latency in milliseconds
	TotalTokens    int64   `json:"total_tokens"`    // Total tokens used
	TotalCost      float64 `json:"total_cost"`      // Total cost in dollars
}

type SortBy

type SortBy string
const (
	SortByTimestamp SortBy = "timestamp"
	SortByLatency   SortBy = "latency"
	SortByTokens    SortBy = "tokens"
	SortByCost      SortBy = "cost"
)

type SortOrder

type SortOrder string
const (
	SortAsc  SortOrder = "asc"
	SortDesc SortOrder = "desc"
)

Jump to

Keyboard shortcuts

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