querypulse

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2023 License: MIT Imports: 6 Imported by: 0

README

QueryPulse

QueryPulse is a golang database driver that provides a callback so you can easily log database queries, their arguments and how long they took.

When working with databases it can be tricky to understand when queries are slow. Sometimes queries are fast for some inputs and not others. By logging slow queries you can easily find examples of queries to optimise.

Query Logs

Features

  • Log all queries
  • Configure your own logging function
    • Only log slow queries
    • Format how you like it
  • Supports all database drivers. PostgreSQL, MySQL SQLite etc.
  • Supports jmoiron/sqlx. See demo.

Install

go get github.com/stephennancekivell/querypulse

Usage

// Register the QueryPulse driver wrapping your other driver.
driverName, err := querypulse.Register(
    "postgres", // wrap the postgres driver
    querypulse.Options{
        // Provide an OnSuccess function to print all queries
        OnSuccess: func(query string, args []any, duration time.Duration) {
            fmt.Printf("OnSuccess: %v %v %v\n", query, args, duration)
        },
    })
...
// connect to the database using the driver like normal.
db, err := sql.Open(driverName, connStr)

// execute queries just an you normally would with the *sql.DB interface
rows, err := db.Query("select $1", 100)

Prints...

OnSuccess: select $1 [100] 135.176µs
Usage with slog
// Create a logger using slog.
jsonlog := slog.New(slog.NewJSONHandler(os.Stdout, nil))
slogDriver, err := qslog.Register("postgres", jsonlog)
..
slogDb, err := sql.Open(slogDriver, connStr)
if err != nil {
    panic(err)
}

rows, err = slogDb.Query("select $1", 300)

Logs...

{
  "time": "2023-08-25T13:24:16.210197191+10:00",
  "level": "INFO",
  "msg": "query success",
  "query": "select $1",
  "args": [300],
  "took_ms": 77027
}

Inspiration

This code was heavily inspired by zipkin-go-sql. Thanks to the maintainers for the great example.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(driverName string, options Options) (string, error)

Register initializes and registers our wrapped database driver identified by its driverName and using provided Options. On success it returns the generated driverName to use when calling sql.Open. It is possible to register multiple wrappers for the same database driver if needing different Options for different connections.

func Wrap

func Wrap(d driver.Driver, options Options) driver.Driver

Wrap takes a SQL driver and wraps it.

func WrapConn

func WrapConn(c driver.Conn, options Options) driver.Conn

WrapConn allows an existing driver.Conn to be wrapped.

func WrapConnector

func WrapConnector(dc driver.Connector, options Options) driver.Connector

WrapConnector allows wrapping a database driver.Connector which eliminates the need to register it as an available driver.Driver.

Types

type Options

type Options struct {
	OnSuccess func(ctx context.Context, query string, args []any, duration time.Duration)
	OnError   func(ctx context.Context, query string, args []any, duration time.Duration, err error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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