logging

package module
v1.4.21 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: Apache-2.0 Imports: 16 Imported by: 2

Documentation

Overview

Package logging provides a GORM-based logging plugin for Bifrost. This plugin stores comprehensive logs of all requests and responses with search, filter, and pagination capabilities.

Package logging provides database operations for the GORM-based logging plugin

Package logging provides utility functions and interfaces for the GORM-based logging plugin

Index

Constants

View Source
const (
	PluginName = "logging"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.3.27

type Config struct {
	DisableContentLogging *bool `json:"disable_content_logging"`
}

type InitialLogData

type InitialLogData struct {
	Provider              string
	Model                 string
	Object                string
	InputHistory          []schemas.ChatMessage
	ResponsesInputHistory []schemas.ResponsesMessage
	Params                interface{}
	SpeechInput           *schemas.SpeechInput
	TranscriptionInput    *schemas.TranscriptionInput
	ImageGenerationInput  *schemas.ImageGenerationInput
	Tools                 []schemas.ChatTool
}

InitialLogData contains data for initial log entry creation

type KeyPair added in v1.3.24

type KeyPair struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

KeyPair represents an ID-Name pair for keys

type LogCallback

type LogCallback func(ctx context.Context, logEntry *logstore.Log)

LogCallback is a function that gets called when a new log entry is created

type LogManager

type LogManager interface {
	// Search searches for log entries based on filters and pagination
	Search(ctx context.Context, filters *logstore.SearchFilters, pagination *logstore.PaginationOptions) (*logstore.SearchResult, error)

	// GetStats calculates statistics for logs matching the given filters
	GetStats(ctx context.Context, filters *logstore.SearchFilters) (*logstore.SearchStats, error)

	// GetHistogram returns time-bucketed request counts for the given filters
	GetHistogram(ctx context.Context, filters *logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.HistogramResult, error)

	// GetTokenHistogram returns time-bucketed token usage for the given filters
	GetTokenHistogram(ctx context.Context, filters *logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.TokenHistogramResult, error)

	// GetCostHistogram returns time-bucketed cost data with model breakdown for the given filters
	GetCostHistogram(ctx context.Context, filters *logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.CostHistogramResult, error)

	// GetModelHistogram returns time-bucketed model usage with success/error breakdown for the given filters
	GetModelHistogram(ctx context.Context, filters *logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.ModelHistogramResult, error)

	// Get the number of dropped requests
	GetDroppedRequests(ctx context.Context) int64

	// GetAvailableModels returns all unique models from logs
	GetAvailableModels(ctx context.Context) []string

	// GetAvailableSelectedKeys returns all unique selected key ID-Name pairs from logs
	GetAvailableSelectedKeys(ctx context.Context) []KeyPair

	// GetAvailableVirtualKeys returns all unique virtual key ID-Name pairs from logs
	GetAvailableVirtualKeys(ctx context.Context) []KeyPair

	// GetAvailableRoutingRules returns all unique routing rule ID-Name pairs from logs
	GetAvailableRoutingRules(ctx context.Context) []KeyPair

	// GetAvailableRoutingEngines returns all unique routing engine types from logs
	GetAvailableRoutingEngines(ctx context.Context) []string

	// DeleteLog deletes a log entry by its ID
	DeleteLog(ctx context.Context, id string) error

	// DeleteLogs deletes multiple log entries by their IDs
	DeleteLogs(ctx context.Context, ids []string) error

	// RecalculateCosts recomputes missing costs for logs matching the filters
	RecalculateCosts(ctx context.Context, filters *logstore.SearchFilters, limit int) (*RecalculateCostResult, error)

	// MCP Tool Log methods
	// SearchMCPToolLogs searches for MCP tool log entries based on filters and pagination
	SearchMCPToolLogs(ctx context.Context, filters *logstore.MCPToolLogSearchFilters, pagination *logstore.PaginationOptions) (*logstore.MCPToolLogSearchResult, error)

	// GetMCPToolLogStats calculates statistics for MCP tool logs matching the given filters
	GetMCPToolLogStats(ctx context.Context, filters *logstore.MCPToolLogSearchFilters) (*logstore.MCPToolLogStats, error)

	// GetAvailableToolNames returns all unique tool names from MCP tool logs
	GetAvailableToolNames(ctx context.Context) ([]string, error)

	// GetAvailableServerLabels returns all unique server labels from MCP tool logs
	GetAvailableServerLabels(ctx context.Context) ([]string, error)

	// GetAvailableMCPVirtualKeys returns all unique virtual key ID-Name pairs from MCP tool logs
	GetAvailableMCPVirtualKeys(ctx context.Context) []KeyPair

	// DeleteMCPToolLogs deletes multiple MCP tool log entries by their IDs
	DeleteMCPToolLogs(ctx context.Context, ids []string) error
}

LogManager defines the main interface that combines all logging functionality

type LogMessage

type LogMessage struct {
	Operation          LogOperation
	RequestID          string                             // Unique ID for the request
	ParentRequestID    string                             // Unique ID for the parent request (used for fallback requests)
	NumberOfRetries    int                                // Number of retries
	FallbackIndex      int                                // Fallback index
	SelectedKeyID      string                             // Selected key ID
	SelectedKeyName    string                             // Selected key name
	VirtualKeyID       string                             // Virtual key ID
	VirtualKeyName     string                             // Virtual key name
	RoutingEnginesUsed []string                           // List of routing engines used
	RoutingRuleID      string                             // Routing rule ID
	RoutingRuleName    string                             // Routing rule name
	Timestamp          time.Time                          // Of the preHook/postHook call
	Latency            int64                              // For latency updates
	InitialData        *InitialLogData                    // For create operations
	SemanticCacheDebug *schemas.BifrostCacheDebug         // For semantic cache operations
	UpdateData         *UpdateLogData                     // For update operations
	StreamResponse     *streaming.ProcessedStreamResponse // For streaming delta updates
	RoutingEngineLogs  string                             // Formatted routing engine decision logs
}

LogMessage represents a message in the logging queue

type LogOperation

type LogOperation string

LogOperation represents the type of logging operation

const (
	LogOperationCreate       LogOperation = "create"
	LogOperationUpdate       LogOperation = "update"
	LogOperationStreamUpdate LogOperation = "stream_update"
)

type LoggerPlugin

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

LoggerPlugin implements the schemas.LLMPlugin and schemas.MCPPlugin interfaces

func Init

func Init(ctx context.Context, config *Config, logger schemas.Logger, logsStore logstore.LogStore, pricingManager *modelcatalog.ModelCatalog, mcpCatalog *mcpcatalog.MCPCatalog) (*LoggerPlugin, error)

Init creates new logger plugin with given log store

func (*LoggerPlugin) Cleanup

func (p *LoggerPlugin) Cleanup() error

Cleanup is called when the plugin is being shut down

func (*LoggerPlugin) GetAvailableMCPVirtualKeys added in v1.4.17

func (p *LoggerPlugin) GetAvailableMCPVirtualKeys(ctx context.Context) []KeyPair

GetAvailableMCPVirtualKeys returns all unique virtual key ID-Name pairs from MCP tool logs

func (*LoggerPlugin) GetAvailableModels

func (p *LoggerPlugin) GetAvailableModels(ctx context.Context) []string

GetAvailableModels returns all unique models from logs

func (*LoggerPlugin) GetAvailableRoutingEngines added in v1.4.19

func (p *LoggerPlugin) GetAvailableRoutingEngines(ctx context.Context) []string

GetAvailableRoutingEngines returns all unique routing engine types used in logs

func (*LoggerPlugin) GetAvailableRoutingRules added in v1.4.17

func (p *LoggerPlugin) GetAvailableRoutingRules(ctx context.Context) []KeyPair

func (*LoggerPlugin) GetAvailableSelectedKeys added in v1.3.24

func (p *LoggerPlugin) GetAvailableSelectedKeys(ctx context.Context) []KeyPair

func (*LoggerPlugin) GetAvailableVirtualKeys added in v1.3.24

func (p *LoggerPlugin) GetAvailableVirtualKeys(ctx context.Context) []KeyPair

func (*LoggerPlugin) GetCostHistogram added in v1.4.13

func (p *LoggerPlugin) GetCostHistogram(ctx context.Context, filters logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.CostHistogramResult, error)

GetCostHistogram returns time-bucketed cost data with model breakdown for the given filters

func (*LoggerPlugin) GetHistogram added in v1.4.13

func (p *LoggerPlugin) GetHistogram(ctx context.Context, filters logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.HistogramResult, error)

GetHistogram returns time-bucketed request counts for the given filters

func (*LoggerPlugin) GetModelHistogram added in v1.4.13

func (p *LoggerPlugin) GetModelHistogram(ctx context.Context, filters logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.ModelHistogramResult, error)

GetModelHistogram returns time-bucketed model usage with success/error breakdown for the given filters

func (*LoggerPlugin) GetName

func (p *LoggerPlugin) GetName() string

GetName returns the name of the plugin

func (*LoggerPlugin) GetPluginLogManager

func (p *LoggerPlugin) GetPluginLogManager() *PluginLogManager

GetPluginLogManager returns a LogManager interface for this plugin

func (*LoggerPlugin) GetStats added in v1.3.32

GetStats calculates statistics for logs matching the given filters

func (*LoggerPlugin) GetTokenHistogram added in v1.4.13

func (p *LoggerPlugin) GetTokenHistogram(ctx context.Context, filters logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.TokenHistogramResult, error)

GetTokenHistogram returns time-bucketed token usage for the given filters

func (*LoggerPlugin) HTTPTransportPostHook added in v1.4.9

func (p *LoggerPlugin) HTTPTransportPostHook(ctx *schemas.BifrostContext, req *schemas.HTTPRequest, resp *schemas.HTTPResponse) error

HTTPTransportPostHook is not used for this plugin

func (*LoggerPlugin) HTTPTransportPreHook added in v1.4.9

func (p *LoggerPlugin) HTTPTransportPreHook(ctx *schemas.BifrostContext, req *schemas.HTTPRequest) (*schemas.HTTPResponse, error)

HTTPTransportPreHook is not used for this plugin

func (*LoggerPlugin) HTTPTransportStreamChunkHook added in v1.4.15

func (p *LoggerPlugin) HTTPTransportStreamChunkHook(ctx *schemas.BifrostContext, req *schemas.HTTPRequest, chunk *schemas.BifrostStreamChunk) (*schemas.BifrostStreamChunk, error)

HTTPTransportStreamChunkHook passes through streaming chunks unchanged

func (*LoggerPlugin) PostLLMHook added in v1.4.17

PostLLMHook is called after a response is received - FULLY ASYNC, NO DATABASE I/O Parameters:

  • ctx: The Bifrost context
  • result: The Bifrost response to be processed
  • bifrostErr: The Bifrost error to be processed

Returns:

  • *schemas.BifrostResponse: The processed response
  • *schemas.BifrostError: The processed error
  • error: Any error that occurred during processing

func (*LoggerPlugin) PostMCPHook added in v1.4.17

PostMCPHook is called after an MCP tool execution - updates the log entry with results Parameters:

  • ctx: The Bifrost context
  • resp: The MCP response containing tool execution result
  • bifrostErr: Any error that occurred during execution

Returns:

  • *schemas.BifrostMCPResponse: The unmodified response
  • *schemas.BifrostError: The unmodified error
  • error: nil (errors are logged but don't fail the request)

func (*LoggerPlugin) PreLLMHook added in v1.4.17

PreLLMHook is called before a request is processed - FULLY ASYNC, NO DATABASE I/O Parameters:

  • ctx: The Bifrost context
  • req: The Bifrost request

Returns:

  • *schemas.BifrostRequest: The processed request
  • *schemas.LLMPluginShortCircuit: The plugin short circuit if the request is not allowed
  • error: Any error that occurred during processing

func (*LoggerPlugin) PreMCPHook added in v1.4.17

PreMCPHook is called before an MCP tool execution - creates initial log entry Parameters:

  • ctx: The Bifrost context
  • req: The MCP request containing tool call information

Returns:

  • *schemas.BifrostMCPRequest: The unmodified request
  • *schemas.MCPPluginShortCircuit: nil (no short-circuiting)
  • error: nil (errors are logged but don't fail the request)

func (*LoggerPlugin) RecalculateCosts added in v1.3.50

func (p *LoggerPlugin) RecalculateCosts(ctx context.Context, filters logstore.SearchFilters, limit int) (*RecalculateCostResult, error)

RecalculateCosts recomputes cost for log entries that are missing cost values

func (*LoggerPlugin) SearchLogs

SearchLogs searches logs with filters and pagination using GORM

func (*LoggerPlugin) SetLogCallback

func (p *LoggerPlugin) SetLogCallback(callback LogCallback)

SetLogCallback sets a callback function that will be called for each log entry

func (*LoggerPlugin) SetMCPToolLogCallback added in v1.4.17

func (p *LoggerPlugin) SetMCPToolLogCallback(callback MCPToolLogCallback)

SetMCPToolLogCallback sets a callback function that will be called for each MCP tool log entry

type MCPToolLogCallback added in v1.4.17

type MCPToolLogCallback func(*logstore.MCPToolLog)

MCPToolLogCallback is a function that gets called when a new MCP tool log entry is created or updated

type PluginLogManager

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

PluginLogManager implements LogManager interface wrapping the plugin

func (*PluginLogManager) DeleteLog added in v1.3.37

func (p *PluginLogManager) DeleteLog(ctx context.Context, id string) error

DeleteLog deletes a log from the log store

func (*PluginLogManager) DeleteLogs added in v1.3.37

func (p *PluginLogManager) DeleteLogs(ctx context.Context, ids []string) error

DeleteLogs deletes multiple logs from the log store

func (*PluginLogManager) DeleteMCPToolLogs added in v1.4.17

func (p *PluginLogManager) DeleteMCPToolLogs(ctx context.Context, ids []string) error

DeleteMCPToolLogs deletes multiple MCP tool log entries by their IDs

func (*PluginLogManager) GetAvailableMCPVirtualKeys added in v1.4.17

func (p *PluginLogManager) GetAvailableMCPVirtualKeys(ctx context.Context) []KeyPair

GetAvailableMCPVirtualKeys returns all unique virtual key ID-Name pairs from MCP tool logs

func (*PluginLogManager) GetAvailableModels

func (p *PluginLogManager) GetAvailableModels(ctx context.Context) []string

GetAvailableModels returns all unique models from logs

func (*PluginLogManager) GetAvailableRoutingEngines added in v1.4.19

func (p *PluginLogManager) GetAvailableRoutingEngines(ctx context.Context) []string

GetAvailableRoutingEngines returns all unique routing engine types from logs

func (*PluginLogManager) GetAvailableRoutingRules added in v1.4.17

func (p *PluginLogManager) GetAvailableRoutingRules(ctx context.Context) []KeyPair

GetAvailableRoutingRules returns all unique routing rule ID-Name pairs from logs

func (*PluginLogManager) GetAvailableSelectedKeys added in v1.3.24

func (p *PluginLogManager) GetAvailableSelectedKeys(ctx context.Context) []KeyPair

GetAvailableSelectedKeys returns all unique selected key ID-Name pairs from logs

func (*PluginLogManager) GetAvailableServerLabels added in v1.4.17

func (p *PluginLogManager) GetAvailableServerLabels(ctx context.Context) ([]string, error)

GetAvailableServerLabels returns all unique server labels from MCP tool logs

func (*PluginLogManager) GetAvailableToolNames added in v1.4.17

func (p *PluginLogManager) GetAvailableToolNames(ctx context.Context) ([]string, error)

GetAvailableToolNames returns all unique tool names from MCP tool logs

func (*PluginLogManager) GetAvailableVirtualKeys added in v1.3.24

func (p *PluginLogManager) GetAvailableVirtualKeys(ctx context.Context) []KeyPair

GetAvailableVirtualKeys returns all unique virtual key ID-Name pairs from logs

func (*PluginLogManager) GetCostHistogram added in v1.4.13

func (p *PluginLogManager) GetCostHistogram(ctx context.Context, filters *logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.CostHistogramResult, error)

func (*PluginLogManager) GetDroppedRequests

func (p *PluginLogManager) GetDroppedRequests(ctx context.Context) int64

func (*PluginLogManager) GetHistogram added in v1.4.13

func (p *PluginLogManager) GetHistogram(ctx context.Context, filters *logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.HistogramResult, error)

func (*PluginLogManager) GetMCPToolLogStats added in v1.4.17

GetMCPToolLogStats calculates statistics for MCP tool logs matching the given filters

func (*PluginLogManager) GetModelHistogram added in v1.4.13

func (p *PluginLogManager) GetModelHistogram(ctx context.Context, filters *logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.ModelHistogramResult, error)

func (*PluginLogManager) GetStats added in v1.3.32

func (*PluginLogManager) GetTokenHistogram added in v1.4.13

func (p *PluginLogManager) GetTokenHistogram(ctx context.Context, filters *logstore.SearchFilters, bucketSizeSeconds int64) (*logstore.TokenHistogramResult, error)

func (*PluginLogManager) RecalculateCosts added in v1.3.50

func (p *PluginLogManager) RecalculateCosts(ctx context.Context, filters *logstore.SearchFilters, limit int) (*RecalculateCostResult, error)

func (*PluginLogManager) Search

func (*PluginLogManager) SearchMCPToolLogs added in v1.4.17

SearchMCPToolLogs searches for MCP tool log entries based on filters and pagination

type RecalculateCostResult added in v1.3.50

type RecalculateCostResult struct {
	TotalMatched int64 `json:"total_matched"`
	Updated      int   `json:"updated"`
	Skipped      int   `json:"skipped"`
	Remaining    int64 `json:"remaining"`
}

RecalculateCostResult represents summary stats from a cost backfill operation

type UpdateLogData

type UpdateLogData struct {
	Status                string
	TokenUsage            *schemas.BifrostLLMUsage
	Cost                  *float64        // Cost in dollars from pricing plugin
	ListModelsOutput      []schemas.Model // For list models requests
	ChatOutput            *schemas.ChatMessage
	ResponsesOutput       []schemas.ResponsesMessage
	EmbeddingOutput       []schemas.EmbeddingData
	ErrorDetails          *schemas.BifrostError
	SpeechOutput          *schemas.BifrostSpeechResponse          // For non-streaming speech responses
	TranscriptionOutput   *schemas.BifrostTranscriptionResponse   // For non-streaming transcription responses
	ImageGenerationOutput *schemas.BifrostImageGenerationResponse // For non-streaming image generation responses
	RawRequest            interface{}
	RawResponse           interface{}
}

UpdateLogData contains data for log entry updates

Jump to

Keyboard shortcuts

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