logstore

package
v1.1.33 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2025 License: Apache-2.0 Imports: 16 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 Config

type Config struct {
	Enabled bool         `json:"enabled"`
	Type    LogStoreType `json:"type"`
	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:"-"`                            // For content search
	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 LogStore

type LogStore interface {
	Ping(ctx context.Context) error
	Create(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)
	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
	Flush(ctx context.Context, since time.Time) error
	Close(ctx context.Context) 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 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"`
}

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

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