querycollector

package
v0.19.1237 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: AGPL-3.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CHQueryRecord added in v0.19.927

type CHQueryRecord struct {
	Table        string    `gorm:"column:table"`
	Operation    string    `gorm:"column:operation"`
	SQL          string    `gorm:"column:sql"`
	DurationMS   float64   `gorm:"column:duration_ms"`
	RowsAffected int64     `gorm:"column:rows_affected"`
	ResponseSize int       `gorm:"column:response_size"`
	PreloadCount int       `gorm:"column:preload_count"`
	Timestamp    time.Time `gorm:"column:timestamp"`
	Error        string    `gorm:"column:error"`
	Caller       string    `gorm:"column:caller"`
	CallerURL    string    `gorm:"column:caller_url"`
	DBType       string    `gorm:"column:db_type"`
	Source       string    `gorm:"column:source"`
	Endpoint     string    `gorm:"column:endpoint"`
	ProcessID    string    `gorm:"column:process_id"`
}

CHQueryRecord is the GORM model for the ClickHouse `queries` table.

func (CHQueryRecord) TableName added in v0.19.927

func (CHQueryRecord) TableName() string

type Collector

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

Collector accumulates query records in a fixed-size ring buffer.

func NewCollector

func NewCollector(maxSize int) *Collector

NewCollector creates a new collector with the given max buffer size.

func (*Collector) Add

func (c *Collector) Add(r QueryRecord)

Add inserts a record into the ring buffer and forwards to the writer if set.

func (*Collector) Clear

func (c *Collector) Clear()

Clear resets the collector.

func (*Collector) Records

func (c *Collector) Records() []QueryRecord

Records returns a snapshot of all collected records (newest first).

func (*Collector) SetWriter added in v0.19.927

func (c *Collector) SetWriter(w *Writer)

SetWriter attaches a Writer that persists records to ClickHouse.

func (*Collector) Tables

func (c *Collector) Tables() []string

Tables returns the distinct table names seen in the buffer.

func (*Collector) Total

func (c *Collector) Total() int

Total returns the total number of queries captured (including evicted).

type Plugin

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

Plugin is a GORM plugin that records every query into a Collector.

func NewPlugin

func NewPlugin(collector *Collector, dbType string) *Plugin

NewPlugin creates a plugin that tags records with the given dbType ("psql" or "ch").

func (*Plugin) Initialize

func (p *Plugin) Initialize(db *gorm.DB) error

func (*Plugin) Name

func (p *Plugin) Name() string

type QueryRecord

type QueryRecord struct {
	Table        string        `json:"table"`
	Operation    string        `json:"operation"`
	SQL          string        `json:"sql"`
	Duration     time.Duration `json:"duration_ns"`
	DurationMS   float64       `json:"duration_ms"`
	RowsAffected int64         `json:"rows_affected"`
	ResponseSize int           `json:"response_size"`
	PreloadCount int           `json:"preload_count"`
	Timestamp    time.Time     `json:"timestamp"`
	Error        string        `json:"error,omitempty"`
	Caller       string        `json:"caller"`
	CallerURL    string        `json:"caller_url,omitempty"`
	DBType       string        `json:"db_type"`
	Source       string        `json:"source"`
	Endpoint     string        `json:"endpoint"`
}

QueryRecord holds the captured data for a single query execution.

type Writer added in v0.19.927

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

Writer buffers QueryRecords and flushes them to ClickHouse in batches.

func NewWriter added in v0.19.927

func NewWriter(cfg WriterConfig) *Writer

NewWriter creates a Writer. Call Start() to begin the background flush loop.

func (*Writer) Start added in v0.19.927

func (w *Writer) Start()

Start begins the background flush goroutine. Call Stop() to shut down.

func (*Writer) Stop added in v0.19.927

func (w *Writer) Stop()

Stop signals the flush goroutine to drain and exit, then blocks until done.

func (*Writer) Write added in v0.19.927

func (w *Writer) Write(r QueryRecord)

Write enqueues a record for persistence. Non-blocking; drops if the channel is full.

type WriterConfig added in v0.19.927

type WriterConfig struct {
	DB             *gorm.DB
	Logger         *zap.Logger
	DisabledTables map[string]struct{}
	BufferSize     int
	FlushSize      int
	FlushInterval  time.Duration
}

WriterConfig configures the buffered ClickHouse writer.

Jump to

Keyboard shortcuts

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