forked from ExpansiveWorlds/instrumentedsql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sql_example_test.go
56 lines (44 loc) · 2.14 KB
/
sql_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package instrumentedsql_test
import (
"context"
"database/sql"
"log"
"github.com/go-sql-driver/mysql"
"github.com/mattn/go-sqlite3"
"github.com/lebiathan/instrumentedsql"
"github.com/lebiathan/instrumentedsql/google"
"github.com/lebiathan/instrumentedsql/opentracing"
)
// WrapDriverGoogle demonstrates how to call wrapDriver and register a new driver.
// This example uses MySQL and google tracing to illustrate this
func ExampleWrapDriver_google() {
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
}
// WrapDriverOpentracing demonstrates how to call wrapDriver and register a new driver.
// This example uses MySQL and opentracing to illustrate this
func ExampleWrapDriver_opentracing() {
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
}
// WrapDriverJustLogging demonstrates how to call wrapDriver and register a new driver.
// This example uses sqlite, but does not trace, but merely logs all calls
func ExampleWrapDriver_justLogging() {
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
}