querycol

package module
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 3 Imported by: 0

README

querycol - A wide-event inspired database query collector

Go Reference

Inspired by wide-event logging, I wanted to be able to collect all database queries that occurred in a request flow, and then output them at the conclusion of the event.

Design

The query collector contains a slice of queries, a mutex for locking, and a boolean representing whether the SQL of the queries should be included. SQL of queries should not be included in production as it can disclose sensitive data.

Each query contains the following attributes:

  • SQL - String value representing the raw SQL query made
  • Duration - Duration of the query
  • Rows - The number of rows the query affected
  • Error - Error message if the query encountered an error

The following attributes are returned when applying the query collector to an event:

  • TotalQueries - Number of queries performed
  • TotalDuration - Total duration of all queries
  • Versions - String map containing version information, set when creating a new query collector (I use it to track PostgreSQL, gorm, and gorm driver versions)
  • Queries - Slice of queries with the above attributes

Usage

For every event, a new query collector should be created and then passed through the event lifecycle. I recommend using a context to store the query collector.

The logger interface of the database client should be overridden to add queries to the query collector.

At the conclusion of an event, apply the query collector to the Zerolog event:

logEvent = queryCol.ApplyToEvent(logEvent)
Example Implementation for gorm.DB
type Logger struct {
	logLevel gormlogger.LogLevel
}

func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
	if l.logLevel == gormlogger.Silent {
		return
	}

	elapsed := time.Since(begin)
	sql, rows := fc()

	// Add query to the query collector
    // appctx.QueryCollector is a wrapper method to get the query collector from the context
	appctx.QueryCollector(ctx).Add(sql, elapsed, rows, err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetVersions

func SetVersions(versions map[string]string)

Set version information that you care to track e.g. database version, sql driver version, gorm version Allows to have greater insight and analysis towards DB performance and identify anomalies that can be related to version changes.

Types

type QueryCollector

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

Stores a list of database query information

func NewQueryCollector

func NewQueryCollector(includeSql bool) *QueryCollector

Creates a new query collector If includeSql is set to true, the SQL query is stored NOTE: Only set includeSql to true in a development environment, by nature of including SQL queries, it will disclose user information that should not be present in production logs

func (*QueryCollector) Add

func (q *QueryCollector) Add(sql string, duration time.Duration, rows int64, err error)

Adds a query to the query collector The sql string will only be stored in the collector if includeSql was set to true in the constructor

func (*QueryCollector) ApplyToEvent

func (q *QueryCollector) ApplyToEvent(event *zerolog.Event) *zerolog.Event

Applies all of the queries as well as a total queries and total query duration field to the database field of a zerolog event

func (*QueryCollector) HasQueries

func (q *QueryCollector) HasQueries() bool

Returns true if any queries were stored

func (*QueryCollector) TotalDuration

func (q *QueryCollector) TotalDuration() time.Duration

Returns the sum of all query durations

func (*QueryCollector) TotalQueries

func (q *QueryCollector) TotalQueries() int

Returns the total number of queries collected

type QueryInfo

type QueryInfo struct {
	SQL      string        `json:"sql,omitempty"`
	Duration time.Duration `json:"duration"`
	Rows     int64         `json:"rows"`
	Error    string        `json:"error,omitempty"`
}

Stores information about a database query

Jump to

Keyboard shortcuts

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