hnysqlx

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2019 License: Apache-2.0 Imports: 10 Imported by: 0

README

Documentation available via godoc

Documentation

Overview

Package hnysqlx wraps `jmoiron/sqlx` to emit one Honeycomb event per DB call.

After opening a DB connection, replace the *sqlx.DB object with a *hnysqlx.DB object. The *hnysqlx.DB struct implements all the same functions as the normal *sqlx.DB struct, and emits an event to Honeycomb with details about the SQL event made.

If you're using transactions, named statements, and so on, there will be a similar swap of `*sqlx` to `*hnysqlx` for each of the additional types you're using.

Additionally, if hnysqlx is used in conjunction with one of the Honeycomb HTTP wrappers *and* you're using the context-aware versions of the SQL calls, the trace ID picked up in the HTTP event will appear in the SQL event. This will ensure you can track any SQL call back to the HTTP event that triggered it.

It is strongly suggested that you use the context-aware version of all calls whenever possible; doing so not only lets you cancel your database calls, but dramatically increases the value of the SQL isntrumentation by letting you tie it back to individual HTTP requests.

If you need to differentiate multiple DB connections, there is a *libhoney.Builder associated with the *hnysqlx.DB (as well as with transactions and statements). Adding fields to this builder will add those fields to all events generated from that DB connection.

Example
// Initialize beeline. The only required field is WriteKey.
beeline.Init(beeline.Config{
	WriteKey: "abcabc123123",
	Dataset:  "sqlx",
	// for demonstration, send the event to STDOUT intead of Honeycomb.
	// Remove the STDOUT setting when filling in a real write key.
	STDOUT: true,
})
// and make sure we close to force flushing all pending events before shutdown
defer beeline.Close()

// open a regular sqlx connection
odb, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1)/donut")
if err != nil {
	fmt.Println("connection err")
}

// replace it with a wrapped hnysqlx.DB
db := hnysqlx.WrapDB(odb)
// and start up a trace for these statements to join
ctx, span := beeline.StartSpan(context.Background(), "start")
defer span.Send()

db.MustExecContext(ctx, "insert into flavors (flavor) values ('rose')")
fv := "rose"
rows, err := db.QueryxContext(ctx, "SELECT id FROM flavors WHERE flavor=?", fv)
if err != nil {
	log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
	var id int
	if err := rows.Scan(&id); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%d is %s\n", id, fv)
}
if err := rows.Err(); err != nil {
	log.Fatal(err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {

	// Builder is available in case you wish to add fields to every SQL event
	// that will be created.
	Builder *libhoney.Builder

	Mapper *reflectx.Mapper
	// contains filtered or unexported fields
}

func WrapDB

func WrapDB(s *sqlx.DB) *DB

func (*DB) BeginTxx

func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

func (*DB) Beginx

func (db *DB) Beginx() (*Tx, error)

func (*DB) Close

func (db *DB) Close() error

not implemented in the wrapper - should just fall through to the superclass

func (*DB) Driver

func (db *DB) Driver() driver.Driver

func (*DB) Exec

func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)

func (*DB) ExecContext

func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*DB) Get

func (db *DB) Get(dest interface{}, query string, args ...interface{}) error

func (*DB) GetContext

func (db *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*DB) MapperFunc

func (db *DB) MapperFunc(mf func(string) string)

func (*DB) MustBegin

func (db *DB) MustBegin() *Tx

func (*DB) MustBeginTx

func (db *DB) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx

func (*DB) MustExec

func (db *DB) MustExec(query string, args ...interface{}) sql.Result

func (*DB) MustExecContext

func (db *DB) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result

func (*DB) NamedExec

func (db *DB) NamedExec(query string, arg interface{}) (sql.Result, error)

func (*DB) NamedExecContext

func (db *DB) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)

func (*DB) NamedQuery

func (db *DB) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)

func (*DB) NamedQueryContext

func (db *DB) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)

func (*DB) Ping

func (db *DB) Ping() error

func (*DB) PingContext

func (db *DB) PingContext(ctx context.Context) error

func (*DB) PrepareNamed

func (db *DB) PrepareNamed(query string) (*NamedStmt, error)

func (*DB) PrepareNamedContext

func (db *DB) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error)

func (*DB) Preparex

func (db *DB) Preparex(query string) (*Stmt, error)

func (*DB) PreparexContext

func (db *DB) PreparexContext(ctx context.Context, query string) (*Stmt, error)

func (*DB) Query

func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*DB) QueryContext

func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

func (*DB) QueryRowx

func (db *DB) QueryRowx(query string, args ...interface{}) *sqlx.Row

func (*DB) QueryRowxContext

func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row

func (*DB) Queryx

func (db *DB) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)

func (*DB) QueryxContext

func (db *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

func (*DB) Rebind

func (db *DB) Rebind(query string) string

func (*DB) Select

func (db *DB) Select(dest interface{}, query string, args ...interface{}) error

func (*DB) SelectContext

func (db *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*DB) SetConnMaxLifetime

func (db *DB) SetConnMaxLifetime(d time.Duration)

func (*DB) SetMaxIdleConns

func (db *DB) SetMaxIdleConns(n int)

func (*DB) SetMaxOpenConns

func (db *DB) SetMaxOpenConns(n int)

func (*DB) Stats

func (db *DB) Stats() sql.DBStats

type NamedStmt

type NamedStmt struct {
	Builder *libhoney.Builder
	// contains filtered or unexported fields
}

func (*NamedStmt) Close

func (n *NamedStmt) Close() error

func (*NamedStmt) Exec

func (n *NamedStmt) Exec(arg interface{}) (sql.Result, error)

func (*NamedStmt) ExecContext

func (n *NamedStmt) ExecContext(ctx context.Context, arg interface{}) (sql.Result, error)

func (*NamedStmt) Get

func (n *NamedStmt) Get(dest interface{}, arg interface{}) error

func (*NamedStmt) GetContext

func (n *NamedStmt) GetContext(ctx context.Context, dest interface{}, arg interface{}) error

func (*NamedStmt) GetWrappedNamedStmt added in v0.2.0

func (n *NamedStmt) GetWrappedNamedStmt() *sqlx.NamedStmt

func (*NamedStmt) MustExec

func (n *NamedStmt) MustExec(arg interface{}) sql.Result

func (*NamedStmt) MustExecContext

func (n *NamedStmt) MustExecContext(ctx context.Context, arg interface{}) sql.Result

func (*NamedStmt) Query

func (n *NamedStmt) Query(arg interface{}) (*sql.Rows, error)

func (*NamedStmt) QueryContext

func (n *NamedStmt) QueryContext(ctx context.Context, arg interface{}) (*sql.Rows, error)

func (*NamedStmt) QueryRow

func (n *NamedStmt) QueryRow(arg interface{}) *sqlx.Row

func (*NamedStmt) QueryRowContext

func (n *NamedStmt) QueryRowContext(ctx context.Context, arg interface{}) *sqlx.Row

func (*NamedStmt) QueryRowx

func (n *NamedStmt) QueryRowx(arg interface{}) *sqlx.Row

func (*NamedStmt) QueryRowxContext

func (n *NamedStmt) QueryRowxContext(ctx context.Context, arg interface{}) *sqlx.Row

func (*NamedStmt) Queryx

func (n *NamedStmt) Queryx(arg interface{}) (*sqlx.Rows, error)

func (*NamedStmt) QueryxContext

func (n *NamedStmt) QueryxContext(ctx context.Context, arg interface{}) (*sqlx.Rows, error)

func (*NamedStmt) Select

func (n *NamedStmt) Select(dest interface{}, arg interface{}) error

func (*NamedStmt) SelectContext

func (n *NamedStmt) SelectContext(ctx context.Context, dest interface{}, arg interface{}) error

func (*NamedStmt) Unsafe

func (n *NamedStmt) Unsafe() *NamedStmt

type Stmt

type Stmt struct {
	Builder *libhoney.Builder
	Mapper  *reflectx.Mapper
	// contains filtered or unexported fields
}

func (*Stmt) Get

func (s *Stmt) Get(dest interface{}, args ...interface{}) error

func (*Stmt) GetContext

func (s *Stmt) GetContext(ctx context.Context, dest interface{}, args ...interface{}) error

func (*Stmt) MustExec

func (s *Stmt) MustExec(args ...interface{}) sql.Result

func (*Stmt) MustExecContext

func (s *Stmt) MustExecContext(ctx context.Context, args ...interface{}) sql.Result

func (*Stmt) QueryRowx

func (s *Stmt) QueryRowx(args ...interface{}) *sqlx.Row

func (*Stmt) QueryRowxContext

func (s *Stmt) QueryRowxContext(ctx context.Context, args ...interface{}) *sqlx.Row

func (*Stmt) Queryx

func (s *Stmt) Queryx(args ...interface{}) (*sqlx.Rows, error)

func (*Stmt) QueryxContext

func (s *Stmt) QueryxContext(ctx context.Context, args ...interface{}) (*sqlx.Rows, error)

func (*Stmt) Select

func (s *Stmt) Select(dest interface{}, args ...interface{}) error

func (*Stmt) SelectContext

func (s *Stmt) SelectContext(ctx context.Context, dest interface{}, args ...interface{}) error

func (*Stmt) Unsafe

func (s *Stmt) Unsafe() *Stmt

type Tx

type Tx struct {
	Builder *libhoney.Builder
	Mapper  *reflectx.Mapper
	// contains filtered or unexported fields
}

func (*Tx) BindNamed

func (tx *Tx) BindNamed(query string, arg interface{}) (string, []interface{}, error)

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) CommitContext added in v0.1.2

func (tx *Tx) CommitContext(ctx context.Context) error

CommitContext is the same as `Commit`, but is passed a context to ensure that commits show up as part of a parent trace

func (*Tx) DriverName

func (tx *Tx) DriverName() string

func (*Tx) Exec

func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error)

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*Tx) Get

func (tx *Tx) Get(dest interface{}, query string, args ...interface{}) error

func (*Tx) GetContext

func (tx *Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*Tx) GetWrappedTx added in v0.2.0

func (tx *Tx) GetWrappedTx() *sqlx.Tx

func (*Tx) MustExec

func (tx *Tx) MustExec(query string, args ...interface{}) sql.Result

func (*Tx) MustExecContext

func (tx *Tx) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result

func (*Tx) NamedExec

func (tx *Tx) NamedExec(query string, arg interface{}) (sql.Result, error)

func (*Tx) NamedExecContext

func (tx *Tx) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)

func (*Tx) NamedQuery

func (tx *Tx) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)

func (*Tx) NamedQueryContext added in v0.2.0

func (tx *Tx) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)

func (*Tx) NamedStmt

func (tx *Tx) NamedStmt(stmt *NamedStmt) *NamedStmt

func (*Tx) NamedStmtContext

func (tx *Tx) NamedStmtContext(ctx context.Context, stmt *NamedStmt) *NamedStmt

func (*Tx) PrepareNamed

func (tx *Tx) PrepareNamed(query string) (*NamedStmt, error)

func (*Tx) PrepareNamedContext

func (tx *Tx) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error)

func (*Tx) Preparex

func (tx *Tx) Preparex(query string) (*Stmt, error)

func (*Tx) PreparexContext

func (tx *Tx) PreparexContext(ctx context.Context, query string) (*Stmt, error)

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) (*sql.Rows, error)

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...interface{}) *sql.Row

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

func (*Tx) QueryRowx

func (tx *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row

func (*Tx) QueryRowxContext

func (tx *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row

func (*Tx) Queryx

func (tx *Tx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)

func (*Tx) QueryxContext

func (tx *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

func (*Tx) Rebind

func (tx *Tx) Rebind(query string) string

func (*Tx) Rollback

func (tx *Tx) Rollback() error

func (*Tx) RollbackContext added in v0.1.2

func (tx *Tx) RollbackContext(ctx context.Context) error

func (*Tx) Select

func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error

func (*Tx) SelectContext

func (tx *Tx) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

func (*Tx) Stmtx

func (tx *Tx) Stmtx(stmt *Stmt) *Stmt

func (*Tx) StmtxContext

func (tx *Tx) StmtxContext(ctx context.Context, stmt *Stmt) *Stmt

func (*Tx) Unsafe

func (tx *Tx) Unsafe() *Tx

Jump to

Keyboard shortcuts

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