Skip to main content
Version: 0.7.0

Observability API Reference

Reference for observers, middleware, OpenTelemetry, hooks, and notifications.

QueryObserver

type QueryObserver interface {
ObserveQuery(event QueryEvent)
}

Attach observers with WithQueryObserver:

type MetricsObserver struct{}

func (m *MetricsObserver) ObserveQuery(event quark.QueryEvent) {
metrics.Record(event.Table, event.Operation, event.Duration, event.Rows, event.Error)
}

client, err := quark.New("postgres", dsn,
quark.WithQueryObserver(&MetricsObserver{}),
)

QueryEvent

type QueryEvent struct {
SQL string
Args []any
Duration time.Duration
Rows int64
Error error
Table string
Operation string
}

Operations include values such as SELECT, EXEC, QUERY_ROW, SELECT (stream), SELECT (cursor), RAW_QUERY, and RAW_EXEC.

Middleware

type Middleware interface {
WrapExec(next ExecFunc) ExecFunc
WrapQuery(next QueryFunc) QueryFunc
WrapQueryRow(next QueryRowFunc) QueryRowFunc
}

Function types:

type ExecFunc func(ctx context.Context, exec Executor, sqlStr string, args []any) (sql.Result, error)
type QueryFunc func(ctx context.Context, exec Executor, sqlStr string, args []any) (*sql.Rows, error)
type QueryRowFunc func(ctx context.Context, exec Executor, sqlStr string, args []any) *sql.Row

Embed BaseMiddleware to override only the paths you need:

type LoggingMiddleware struct {
quark.BaseMiddleware
}

func (l *LoggingMiddleware) WrapQuery(next quark.QueryFunc) quark.QueryFunc {
return func(ctx context.Context, exec quark.Executor, sqlStr string, args []any) (*sql.Rows, error) {
start := time.Now()
rows, err := next(ctx, exec, sqlStr, args)
log.Printf("query duration=%s err=%v sql=%s", time.Since(start), err, sqlStr)
return rows, err
}
}

OpenTelemetry

import quarkotel "github.com/jcsvwinston/quark/otel"

client, err := quark.New("postgres", dsn,
quark.WithMiddleware(quarkotel.New()),
)

The middleware creates spans:

ExecutionSpan
ExecContextquark.exec
QueryContextquark.query
QueryRowContextquark.query_row

Attributes include db.statement and db.operation.

Hooks

Entity-level hooks:

type BeforeCreateHook interface {
BeforeCreate(ctx context.Context) error
}

Available hook interfaces:

InterfaceMethod
BeforeCreateHookBeforeCreate(context.Context) error
AfterCreateHookAfterCreate(context.Context) error
BeforeUpdateHookBeforeUpdate(context.Context) error
AfterUpdateHookAfterUpdate(context.Context) error
BeforeDeleteHookBeforeDelete(context.Context) error
AfterDeleteHookAfterDelete(context.Context) error

Notifications

Notify(ctx context.Context, provider ClientProvider, channel, payload string) error

Sends a database notification where supported.

err := quark.Notify(ctx, client, "user_events", `{"type":"signup","id":123}`)

Current support:

DialectBehavior
PostgreSQLUses pg_notify($1, $2).
MySQLReturns not supported.
SQLiteReturns not supported.
Other dialectsReturns not supported.

NewEventBus(client *Client) *EventBus

Creates an experimental event bus wrapper. CreateListener is not implemented in the current release and returns ErrDialectNotSupported.