forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_driver_test.go
187 lines (169 loc) · 6.9 KB
/
sql_driver_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build go1.10
package newrelic
import (
"context"
"database/sql/driver"
"strings"
"testing"
"github.com/newrelic/go-agent/internal"
)
var (
driverTestMetrics = []internal.WantMetric{
{Name: "OtherTransaction/all", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransaction/Go/hello", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransactionTotalTime", Scope: "", Forced: true, Data: nil},
{Name: "OtherTransactionTotalTime/Go/hello", Scope: "", Forced: false, Data: nil},
{Name: "Datastore/all", Scope: "", Forced: true, Data: nil},
{Name: "Datastore/allOther", Scope: "", Forced: true, Data: nil},
{Name: "Datastore/MySQL/all", Scope: "", Forced: true, Data: nil},
{Name: "Datastore/MySQL/allOther", Scope: "", Forced: true, Data: nil},
{Name: "Datastore/operation/MySQL/myoperation", Scope: "", Forced: false, Data: nil},
{Name: "Datastore/statement/MySQL/mycollection/myoperation", Scope: "", Forced: false, Data: nil},
{Name: "Datastore/statement/MySQL/mycollection/myoperation", Scope: "OtherTransaction/Go/hello", Forced: false, Data: nil},
{Name: "Datastore/instance/MySQL/myhost/myport", Scope: "", Forced: false, Data: nil},
}
)
type testDriver struct{}
type testConnector struct{}
type testConn struct{}
type testStmt struct{}
func (d testDriver) OpenConnector(name string) (driver.Connector, error) { return testConnector{}, nil }
func (d testDriver) Open(name string) (driver.Conn, error) { return testConn{}, nil }
func (c testConnector) Connect(context.Context) (driver.Conn, error) { return testConn{}, nil }
func (c testConnector) Driver() driver.Driver { return testDriver{} }
func (c testConn) Prepare(query string) (driver.Stmt, error) { return testStmt{}, nil }
func (c testConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
return testStmt{}, nil
}
func (c testConn) Close() error { return nil }
func (c testConn) Begin() (driver.Tx, error) { return nil, nil }
func (c testConn) ExecContext(context.Context, string, []driver.NamedValue) (driver.Result, error) {
return nil, nil
}
func (c testConn) QueryContext(context.Context, string, []driver.NamedValue) (driver.Rows, error) {
return nil, nil
}
func (s testStmt) Close() error {
return nil
}
func (s testStmt) NumInput() int {
return 0
}
func (s testStmt) Exec(args []driver.Value) (driver.Result, error) {
return nil, nil
}
func (s testStmt) Query(args []driver.Value) (driver.Rows, error) {
return nil, nil
}
func (s testStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
return nil, nil
}
func (s testStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
return nil, nil
}
var (
testBuilder = SQLDriverSegmentBuilder{
BaseSegment: DatastoreSegment{
Product: DatastoreMySQL,
},
ParseDSN: func(segment *DatastoreSegment, dsn string) {
fields := strings.Split(dsn, ",")
segment.Host = fields[0]
segment.PortPathOrID = fields[1]
segment.DatabaseName = fields[2]
},
ParseQuery: func(segment *DatastoreSegment, query string) {
fields := strings.Split(query, ",")
segment.Operation = fields[0]
segment.Collection = fields[1]
},
}
)
func TestDriverStmtExecContext(t *testing.T) {
// Test that driver.Stmt.ExecContext calls get instrumented.
app := testApp(nil, nil, t)
dr := InstrumentSQLDriver(testDriver{}, testBuilder)
txn := app.StartTransaction("hello", nil, nil)
conn, _ := dr.Open("myhost,myport,mydatabase")
stmt, _ := conn.Prepare("myoperation,mycollection")
ctx := NewContext(context.Background(), txn)
stmt.(driver.StmtExecContext).ExecContext(ctx, nil)
txn.End()
app.ExpectMetrics(t, driverTestMetrics)
}
func TestDriverStmtQueryContext(t *testing.T) {
// Test that driver.Stmt.PrepareContext calls get instrumented.
app := testApp(nil, nil, t)
dr := InstrumentSQLDriver(testDriver{}, testBuilder)
txn := app.StartTransaction("hello", nil, nil)
conn, _ := dr.Open("myhost,myport,mydatabase")
stmt, _ := conn.(driver.ConnPrepareContext).PrepareContext(context.Background(), "myoperation,mycollection")
ctx := NewContext(context.Background(), txn)
stmt.(driver.StmtQueryContext).QueryContext(ctx, nil)
txn.End()
app.ExpectMetrics(t, driverTestMetrics)
}
func TestDriverConnExecContext(t *testing.T) {
// Test that driver.Conn.ExecContext calls get instrumented.
app := testApp(nil, nil, t)
dr := InstrumentSQLDriver(testDriver{}, testBuilder)
txn := app.StartTransaction("hello", nil, nil)
conn, _ := dr.Open("myhost,myport,mydatabase")
ctx := NewContext(context.Background(), txn)
conn.(driver.ExecerContext).ExecContext(ctx, "myoperation,mycollection", nil)
txn.End()
app.ExpectMetrics(t, driverTestMetrics)
}
func TestDriverConnQueryContext(t *testing.T) {
// Test that driver.Conn.QueryContext calls get instrumented.
app := testApp(nil, nil, t)
dr := InstrumentSQLDriver(testDriver{}, testBuilder)
txn := app.StartTransaction("hello", nil, nil)
conn, _ := dr.Open("myhost,myport,mydatabase")
ctx := NewContext(context.Background(), txn)
conn.(driver.QueryerContext).QueryContext(ctx, "myoperation,mycollection", nil)
txn.End()
app.ExpectMetrics(t, driverTestMetrics)
}
func TestDriverContext(t *testing.T) {
// Test that driver.OpenConnector returns an instrumented connector.
app := testApp(nil, nil, t)
dr := InstrumentSQLDriver(testDriver{}, testBuilder)
txn := app.StartTransaction("hello", nil, nil)
connector, _ := dr.(driver.DriverContext).OpenConnector("myhost,myport,mydatabase")
conn, _ := connector.Connect(context.Background())
ctx := NewContext(context.Background(), txn)
conn.(driver.ExecerContext).ExecContext(ctx, "myoperation,mycollection", nil)
txn.End()
app.ExpectMetrics(t, driverTestMetrics)
}
func TestInstrumentSQLConnector(t *testing.T) {
// Test that connections returned by an instrumented driver.Connector
// are instrumented.
app := testApp(nil, nil, t)
bld := testBuilder
bld.BaseSegment.Host = "myhost"
bld.BaseSegment.PortPathOrID = "myport"
bld.BaseSegment.DatabaseName = "mydatabase"
connector := InstrumentSQLConnector(testConnector{}, bld)
txn := app.StartTransaction("hello", nil, nil)
conn, _ := connector.Connect(context.Background())
ctx := NewContext(context.Background(), txn)
conn.(driver.ExecerContext).ExecContext(ctx, "myoperation,mycollection", nil)
txn.End()
app.ExpectMetrics(t, driverTestMetrics)
}
func TestConnectorToDriver(t *testing.T) {
// Test that driver.Connector.Driver returns an instrumented Driver.
app := testApp(nil, nil, t)
connector := InstrumentSQLConnector(testConnector{}, testBuilder)
txn := app.StartTransaction("hello", nil, nil)
dr := connector.Driver()
conn, _ := dr.Open("myhost,myport,mydatabase")
ctx := NewContext(context.Background(), txn)
conn.(driver.ExecerContext).ExecContext(ctx, "myoperation,mycollection", nil)
txn.End()
app.ExpectMetrics(t, driverTestMetrics)
}