beeline-go: github.com/honeycombio/beeline-go/wrappers/hnysql Index | Examples | Files

package hnysql

import "github.com/honeycombio/beeline-go/wrappers/hnysql"

Package hnysql wraps `database.sql` to emit one Honeycomb event per DB call.

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

Additionally, if hnysql is used in conjunction with one of the Honeycomb HTTP wrappers *and* you use the context-aware version of the DB calls, the trace ID picked up in the HTTP event will appear in the SQL event to allow easy identification of what HTTP call triggers which SQL calls.

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.

Code:

// Initialize beeline. The only required field is WriteKey.
beeline.Init(beeline.Config{
    WriteKey: "abcabc123123",
    Dataset:  "sql",
    // 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 sql.DB connection
odb, err := sql.Open("mysql", "root:@tcp(127.0.0.1)/donut")
if err != nil {
    fmt.Printf("connection err: %s\n", err)
    return
}

// replace it with a wrapped hnysql.DB
db := hnysql.WrapDB(odb)
// and start up a trace to capture all the calls
ctx, span := beeline.StartSpan(context.Background(), "start")
defer span.Send()

// from here on, all SQL calls will emit events.

db.ExecContext(ctx, "insert into flavors (flavor) values ('rose')")
fv := "rose"
rows, err := db.QueryContext(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)
}

Index

Examples

Package Files

doc.go sql.go

type Conn Uses

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

func (*Conn) BeginTx Uses

func (c *Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

func (*Conn) Close Uses

func (c *Conn) Close() error

func (*Conn) ExecContext Uses

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

func (*Conn) PingContext Uses

func (c *Conn) PingContext(ctx context.Context) error

func (*Conn) PrepareContext Uses

func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error)

func (*Conn) QueryContext Uses

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

func (*Conn) QueryRowContext Uses

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

type DB Uses

type DB struct {

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

func WrapDB Uses

func WrapDB(s *sql.DB) *DB

func (*DB) Begin Uses

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

func (*DB) BeginTx Uses

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

func (*DB) Close Uses

func (db *DB) Close() error

func (*DB) Conn Uses

func (db *DB) Conn(ctx context.Context) (*Conn, error)

func (*DB) Driver Uses

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

these are not instrumented calls since they're more configuration-esque

func (*DB) Exec Uses

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

func (*DB) ExecContext Uses

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

func (*DB) Ping Uses

func (db *DB) Ping() error

func (*DB) PingContext Uses

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

func (*DB) Prepare Uses

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

func (*DB) PrepareContext Uses

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

func (*DB) Query Uses

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

func (*DB) QueryContext Uses

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

func (*DB) QueryRow Uses

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

func (*DB) QueryRowContext Uses

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

func (*DB) SetConnMaxLifetime Uses

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

func (*DB) SetMaxIdleConns Uses

func (db *DB) SetMaxIdleConns(n int)

func (*DB) SetMaxOpenConns Uses

func (db *DB) SetMaxOpenConns(n int)

func (*DB) Stats Uses

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

type Stmt Uses

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

func (*Stmt) Close Uses

func (s *Stmt) Close() error

func (*Stmt) Exec Uses

func (s *Stmt) Exec(args ...interface{}) (sql.Result, error)

func (*Stmt) ExecContext Uses

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

func (*Stmt) Query Uses

func (s *Stmt) Query(args ...interface{}) (*sql.Rows, error)

func (*Stmt) QueryContext Uses

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

func (*Stmt) QueryRow Uses

func (s *Stmt) QueryRow(args ...interface{}) *sql.Row

func (*Stmt) QueryRowContext Uses

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

type Tx Uses

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

func (*Tx) Commit Uses

func (tx *Tx) Commit() error

func (*Tx) Exec Uses

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

func (*Tx) ExecContext Uses

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

func (*Tx) Prepare Uses

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

func (*Tx) PrepareContext Uses

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

func (*Tx) Query Uses

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

func (*Tx) QueryContext Uses

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

func (*Tx) QueryRow Uses

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

func (*Tx) QueryRowContext Uses

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

func (*Tx) Rollback Uses

func (tx *Tx) Rollback() error

func (*Tx) Stmt Uses

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

func (*Tx) StmtContext Uses

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

Package hnysql imports 7 packages (graph) and is imported by 1 packages. Updated 2018-09-14. Refresh now. Tools for package owners.