hnysql

package
v0.4.9 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

README

Documentation available via godoc

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

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

func (*Conn) BeginTx

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

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) ExecContext

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

func (*Conn) PingContext

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

func (*Conn) PrepareContext

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

func (*Conn) QueryContext

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

func (*Conn) QueryRowContext

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

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
}

func WrapDB

func WrapDB(s *sql.DB) *DB

func (*DB) Begin

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

func (*DB) BeginTx

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

func (*DB) Close

func (db *DB) Close() error

func (*DB) Conn

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

func (*DB) Driver

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

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

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) Ping

func (db *DB) Ping() error

func (*DB) PingContext

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

func (*DB) Prepare

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

func (*DB) PrepareContext

func (db *DB) PrepareContext(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) 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 Stmt

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

func (*Stmt) Close

func (s *Stmt) Close() error

func (*Stmt) Exec

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

func (*Stmt) ExecContext

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

func (*Stmt) Query

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

func (*Stmt) QueryContext

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

func (*Stmt) QueryRow

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

func (*Stmt) QueryRowContext

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

type Tx

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

func (*Tx) Commit

func (tx *Tx) Commit() error

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) Prepare

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

func (*Tx) PrepareContext

func (tx *Tx) PrepareContext(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) Rollback

func (tx *Tx) Rollback() error

func (*Tx) Stmt

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

func (*Tx) StmtContext

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

Jump to

Keyboard shortcuts

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