-
Notifications
You must be signed in to change notification settings - Fork 416
/
example_test.go
126 lines (105 loc) · 4 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package sql_test
import (
"context"
"log"
sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"github.com/go-sql-driver/mysql"
"github.com/lib/pq"
sqlite "github.com/mattn/go-sqlite3" // Setup application to use Sqlite
)
func Example() {
// The first step is to register the driver that we will be using.
sqltrace.Register("postgres", &pq.Driver{})
// Followed by a call to Open.
db, err := sqltrace.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable")
if err != nil {
log.Fatal(err)
}
// Then, we continue using the database/sql package as we normally would, with tracing.
rows, err := db.Query("SELECT name FROM users WHERE age=?", 27)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
}
func Example_context() {
// Register the driver that we will be using (in this case mysql) under a custom service name.
sqltrace.Register("mysql", &mysql.MySQLDriver{}, sqltrace.WithServiceName("my-db"))
// Open a connection to the DB using the driver we've just registered with tracing.
db, err := sqltrace.Open("mysql", "user:password@/dbname")
if err != nil {
log.Fatal(err)
}
// Create a root span, giving name, server and resource.
span, ctx := tracer.StartSpanFromContext(context.Background(), "my-query",
tracer.SpanType(ext.SpanTypeSQL),
tracer.ServiceName("my-db"),
tracer.ResourceName("initial-access"),
)
// Subsequent spans inherit their parent from context.
rows, err := db.QueryContext(ctx, "SELECT * FROM city LIMIT 5")
if err != nil {
log.Fatal(err)
}
rows.Close()
span.Finish(tracer.WithError(err))
}
func Example_sqlite() {
// Register the driver that we will be using (in this case Sqlite) under a custom service name.
sqltrace.Register("sqlite", &sqlite.SQLiteDriver{}, sqltrace.WithServiceName("sqlite-example"))
// Open a connection to the DB using the driver we've just registered with tracing.
db, err := sqltrace.Open("sqlite", "./test.db")
if err != nil {
log.Fatal(err)
}
// Create a root span, giving name, server and resource.
span, ctx := tracer.StartSpanFromContext(context.Background(), "my-query",
tracer.SpanType("example"),
tracer.ServiceName("sqlite-example"),
tracer.ResourceName("initial-access"),
)
// Subsequent spans inherit their parent from context.
rows, err := db.QueryContext(ctx, "SELECT * FROM city LIMIT 5")
if err != nil {
log.Fatal(err)
}
rows.Close()
span.Finish(tracer.WithError(err))
}
func Example_dbmPropagation() {
// The first step is to set the dbm propagation mode when registering the driver. Note that this can also
// be done on sqltrace.Open for more granular control over the feature.
sqltrace.Register("postgres", &pq.Driver{}, sqltrace.WithDBMPropagation(tracer.DBMPropagationModeFull))
// Followed by a call to Open.
db, err := sqltrace.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable")
if err != nil {
log.Fatal(err)
}
// Then, we continue using the database/sql package as we normally would, with tracing.
rows, err := db.Query("SELECT name FROM users WHERE age=?", 27)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
}
func Example_dbStats() {
// Register the driver with the WithDBStats option to enable DBStats metric polling
sqltrace.Register("postgres", &pq.Driver{}, sqltrace.WithDBStats())
// Followed by a call to Open.
db, err := sqltrace.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable")
if err != nil {
log.Fatal(err)
}
// Tracing and metric polling is now enabled. Metrics will be submitted to Datadog with the prefix `datadog.tracer.sql`
rows, err := db.Query("SELECT name FROM users WHERE age=?", 27)
if err != nil {
log.Fatal(err)
}
rows.Close()
}