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 ¶
- type DB
- func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *DB) Beginx() (*Tx, error)
- func (db *DB) Close() error
- func (db *DB) Driver() driver.Driver
- func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (db *DB) Get(dest interface{}, query string, args ...interface{}) error
- func (db *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- func (db *DB) GetWrappedDB() *sqlx.DB
- func (db *DB) MapperFunc(mf func(string) string)
- func (db *DB) MustBegin() *Tx
- func (db *DB) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx
- func (db *DB) MustExec(query string, args ...interface{}) sql.Result
- func (db *DB) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result
- func (db *DB) NamedExec(query string, arg interface{}) (sql.Result, error)
- func (db *DB) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
- func (db *DB) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
- func (db *DB) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
- func (db *DB) Ping() error
- func (db *DB) PingContext(ctx context.Context) error
- func (db *DB) PrepareNamed(query string) (*NamedStmt, error)
- func (db *DB) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error)
- func (db *DB) Preparex(query string) (*Stmt, error)
- func (db *DB) PreparexContext(ctx context.Context, query string) (*Stmt, error)
- func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (db *DB) QueryRowx(query string, args ...interface{}) *sqlx.Row
- func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
- func (db *DB) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
- func (db *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
- func (db *DB) Rebind(query string) string
- func (db *DB) Select(dest interface{}, query string, args ...interface{}) error
- func (db *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- func (db *DB) SetConnMaxLifetime(d time.Duration)
- func (db *DB) SetMaxIdleConns(n int)
- func (db *DB) SetMaxOpenConns(n int)
- func (db *DB) Stats() sql.DBStats
- type NamedStmt
- func (n *NamedStmt) Close() error
- func (n *NamedStmt) Exec(arg interface{}) (sql.Result, error)
- func (n *NamedStmt) ExecContext(ctx context.Context, arg interface{}) (sql.Result, error)
- func (n *NamedStmt) Get(dest interface{}, arg interface{}) error
- func (n *NamedStmt) GetContext(ctx context.Context, dest interface{}, arg interface{}) error
- func (n *NamedStmt) GetWrappedNamedStmt() *sqlx.NamedStmt
- func (n *NamedStmt) MustExec(arg interface{}) sql.Result
- func (n *NamedStmt) MustExecContext(ctx context.Context, arg interface{}) sql.Result
- func (n *NamedStmt) Query(arg interface{}) (*sql.Rows, error)
- func (n *NamedStmt) QueryContext(ctx context.Context, arg interface{}) (*sql.Rows, error)
- func (n *NamedStmt) QueryRow(arg interface{}) *sqlx.Row
- func (n *NamedStmt) QueryRowContext(ctx context.Context, arg interface{}) *sqlx.Row
- func (n *NamedStmt) QueryRowx(arg interface{}) *sqlx.Row
- func (n *NamedStmt) QueryRowxContext(ctx context.Context, arg interface{}) *sqlx.Row
- func (n *NamedStmt) Queryx(arg interface{}) (*sqlx.Rows, error)
- func (n *NamedStmt) QueryxContext(ctx context.Context, arg interface{}) (*sqlx.Rows, error)
- func (n *NamedStmt) Select(dest interface{}, arg interface{}) error
- func (n *NamedStmt) SelectContext(ctx context.Context, dest interface{}, arg interface{}) error
- func (n *NamedStmt) Unsafe() *NamedStmt
- type Stmt
- func (s *Stmt) Close() error
- func (s *Stmt) Get(dest interface{}, args ...interface{}) error
- func (s *Stmt) GetContext(ctx context.Context, dest interface{}, args ...interface{}) error
- func (s *Stmt) MustExec(args ...interface{}) sql.Result
- func (s *Stmt) MustExecContext(ctx context.Context, args ...interface{}) sql.Result
- func (s *Stmt) QueryRowx(args ...interface{}) *sqlx.Row
- func (s *Stmt) QueryRowxContext(ctx context.Context, args ...interface{}) *sqlx.Row
- func (s *Stmt) Queryx(args ...interface{}) (*sqlx.Rows, error)
- func (s *Stmt) QueryxContext(ctx context.Context, args ...interface{}) (*sqlx.Rows, error)
- func (s *Stmt) Select(dest interface{}, args ...interface{}) error
- func (s *Stmt) SelectContext(ctx context.Context, dest interface{}, args ...interface{}) error
- func (s *Stmt) Unsafe() *Stmt
- type Tx
- func (tx *Tx) BindNamed(query string, arg interface{}) (string, []interface{}, error)
- func (tx *Tx) Commit() error
- func (tx *Tx) CommitContext(ctx context.Context) error
- func (tx *Tx) DriverName() string
- func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error)
- func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (tx *Tx) Get(dest interface{}, query string, args ...interface{}) error
- func (tx *Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- func (tx *Tx) GetWrappedTx() *sqlx.Tx
- func (tx *Tx) MustExec(query string, args ...interface{}) sql.Result
- func (tx *Tx) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result
- func (tx *Tx) NamedExec(query string, arg interface{}) (sql.Result, error)
- func (tx *Tx) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
- func (tx *Tx) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
- func (tx *Tx) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
- func (tx *Tx) NamedStmt(stmt *NamedStmt) *NamedStmt
- func (tx *Tx) NamedStmtContext(ctx context.Context, stmt *NamedStmt) *NamedStmt
- func (tx *Tx) PrepareNamed(query string) (*NamedStmt, error)
- func (tx *Tx) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error)
- func (tx *Tx) Preparex(query string) (*Stmt, error)
- func (tx *Tx) PreparexContext(ctx context.Context, query string) (*Stmt, error)
- func (tx *Tx) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (tx *Tx) QueryRow(query string, args ...interface{}) *sql.Row
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (tx *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row
- func (tx *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
- func (tx *Tx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
- func (tx *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
- func (tx *Tx) Rebind(query string) string
- func (tx *Tx) Rollback() error
- func (tx *Tx) RollbackContext(ctx context.Context) error
- func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error
- func (tx *Tx) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- func (tx *Tx) Stmtx(stmt *Stmt) *Stmt
- func (tx *Tx) StmtxContext(ctx context.Context, stmt *Stmt) *Stmt
- func (tx *Tx) Unsafe() *Tx
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 (*DB) ExecContext ¶
func (*DB) GetContext ¶
func (*DB) GetWrappedDB ¶ added in v0.4.7
func (*DB) MapperFunc ¶
func (*DB) MustExecContext ¶
func (*DB) NamedExecContext ¶
func (*DB) NamedQueryContext ¶
func (*DB) PrepareNamedContext ¶
func (*DB) PreparexContext ¶
func (*DB) QueryContext ¶
func (*DB) QueryRowContext ¶
func (*DB) QueryRowxContext ¶
func (*DB) QueryxContext ¶
func (*DB) SelectContext ¶
func (*DB) SetConnMaxLifetime ¶
func (*DB) SetMaxIdleConns ¶
func (*DB) SetMaxOpenConns ¶
type NamedStmt ¶
func (*NamedStmt) ExecContext ¶
func (*NamedStmt) GetContext ¶
func (*NamedStmt) GetWrappedNamedStmt ¶ added in v0.2.0
func (*NamedStmt) MustExecContext ¶
func (*NamedStmt) QueryContext ¶
func (*NamedStmt) QueryRowContext ¶
func (*NamedStmt) QueryRowxContext ¶
func (*NamedStmt) QueryxContext ¶
func (*NamedStmt) SelectContext ¶
type Stmt ¶
type Stmt struct { Builder *libhoney.Builder Mapper *reflectx.Mapper // contains filtered or unexported fields }
func (*Stmt) GetContext ¶
func (*Stmt) MustExecContext ¶
func (*Stmt) QueryRowxContext ¶
func (*Stmt) QueryxContext ¶
func (*Stmt) SelectContext ¶
type Tx ¶
type Tx struct { Builder *libhoney.Builder Mapper *reflectx.Mapper // contains filtered or unexported fields }
func (*Tx) CommitContext ¶ added in v0.1.2
CommitContext is the same as `Commit`, but is passed a context to ensure that commits show up as part of a parent trace