Documentation ¶
Overview ¶
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.
Example ¶
// 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) }
Output:
Index ¶
- type Conn
- func (c *Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (c *Conn) Close() error
- func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (c *Conn) PingContext(ctx context.Context) error
- func (c *Conn) PrepareContext(ctx context.Context, query string) (*Stmt, error)
- func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *DB) Close() error
- func (db *DB) Conn(ctx context.Context) (*Conn, 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) Ping() error
- func (db *DB) PingContext(ctx context.Context) error
- func (db *DB) Prepare(query string) (*Stmt, error)
- func (db *DB) PrepareContext(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) SetConnMaxLifetime(d time.Duration)
- func (db *DB) SetMaxIdleConns(n int)
- func (db *DB) SetMaxOpenConns(n int)
- func (db *DB) Stats() sql.DBStats
- type Stmt
- func (s *Stmt) Close() error
- func (s *Stmt) Exec(args ...interface{}) (sql.Result, error)
- func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)
- func (s *Stmt) Query(args ...interface{}) (*sql.Rows, error)
- func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error)
- func (s *Stmt) QueryRow(args ...interface{}) *sql.Row
- func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row
- type Tx
- func (tx *Tx) Commit() error
- 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) Prepare(query string) (*Stmt, error)
- func (tx *Tx) PrepareContext(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) Rollback() error
- func (tx *Tx) Stmt(stmt *Stmt) *Stmt
- func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
func (*Conn) ExecContext ¶
func (*Conn) PrepareContext ¶
func (*Conn) QueryContext ¶
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 // contains filtered or unexported fields }