Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WrapDriver ¶
WrapDriver will wrap the passed SQL driver and return a new sql driver that uses it and also logs and traces calls using the passed logger and tracer The returned driver will still have to be registered with the sql package before it can be used.
Important note: Seeing as the context passed into the various instrumentation calls this package calls, Any call without a context passed will not be instrumented. Please be sure to use the ___Context() and BeginTx() function calls added in Go 1.8 instead of the older calls which do not accept a context.
Example (Google) ¶
WrapDriverGoogle demonstrates how to call wrapDriver and register a new driver. This example uses MySQL and google tracing to illustrate this
package main import ( "context" "database/sql" "log" "github.com/go-sql-driver/mysql" "github.com/lebiathan/instrumentedsql" "github.com/lebiathan/instrumentedsql/google" ) func main() { logger := instrumentedsql.LoggerFunc(func(ctx context.Context, msg string, keyvals ...interface{}) { log.Printf("%s %v", msg, keyvals) }) sql.Register("instrumented-mysql", instrumentedsql.WrapDriver(mysql.MySQLDriver{}, instrumentedsql.WithTracer(google.NewTracer()), instrumentedsql.WithLogger(logger))) db, err := sql.Open("instrumented-mysql", "connString") // Proceed to handle connection errors and use the database as usual _, _ = db, err }
Output:
Example (JustLogging) ¶
WrapDriverJustLogging demonstrates how to call wrapDriver and register a new driver. This example uses sqlite, but does not trace, but merely logs all calls
logger := instrumentedsql.LoggerFunc(func(ctx context.Context, msg string, keyvals ...interface{}) { log.Printf("%s %v", msg, keyvals) }) sql.Register("instrumented-sqlite", instrumentedsql.WrapDriver(&sqlite3.SQLiteDriver{}, instrumentedsql.WithLogger(logger))) db, err := sql.Open("instrumented-sqlite", "connString") // Proceed to handle connection errors and use the database as usual _, _ = db, err
Output:
Example (Opentracing) ¶
WrapDriverOpentracing demonstrates how to call wrapDriver and register a new driver. This example uses MySQL and opentracing to illustrate this
package main import ( "context" "database/sql" "log" "github.com/go-sql-driver/mysql" "github.com/lebiathan/instrumentedsql" "github.com/lebiathan/instrumentedsql/opentracing" ) func main() { logger := instrumentedsql.LoggerFunc(func(ctx context.Context, msg string, keyvals ...interface{}) { log.Printf("%s %v", msg, keyvals) }) sql.Register("instrumented-mysql", instrumentedsql.WrapDriver(mysql.MySQLDriver{}, instrumentedsql.WithTracer(opentracing.NewTracer()), instrumentedsql.WithLogger(logger))) db, err := sql.Open("instrumented-mysql", "connString") // Proceed to handle connection errors and use the database as usual _, _ = db, err }
Output:
Types ¶
type Logger ¶
Logger is the interface needed to be implemented by any logging implementation we use, see also NewFuncLogger
type LoggerFunc ¶
LoggerFunc is an adapter which allows a function to be used as a Logger.
type Opt ¶
type Opt func(*wrappedDriver)
Opt is a functional option type for the wrapped driver
func WithLogger ¶
WithLogger sets the logger of the wrapped driver to the provided logger
func WithTracer ¶
WithTracer sets the tracer of the wrapped driver to the provided tracer