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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
// 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/v3/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")
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")
conn, _ := dr.Open("myhost,myport,mydatabase")
stmt, _ := conn.(driver.ConnPrepareContext).PrepareContext(nil, "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")
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")
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")
connector, _ := dr.(driver.DriverContext).OpenConnector("myhost,myport,mydatabase")
conn, _ := connector.Connect(nil)
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")
conn, _ := connector.Connect(nil)
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")
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)
}
type testConnectorErr struct {
testConnector
}
func (c testConnectorErr) Connect(context.Context) (driver.Conn, error) { return testConnErr{}, nil }
type testConnErr struct {
testConn
}
func (c testConnErr) QueryContext(context.Context, string, []driver.NamedValue) (driver.Rows, error) {
return nil, driver.ErrSkip
}
func (c testConnErr) ExecContext(context.Context, string, []driver.NamedValue) (driver.Result, error) {
return nil, driver.ErrSkip
}
// Ensure that if the driver used returns driver.ErrSkip that spans still have correct parentage
func TestExecContextErrSkipReturned(t *testing.T) {
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
connector := InstrumentSQLConnector(testConnectorErr{}, testBuilder)
txn := app.StartTransaction("hello")
conn, _ := connector.Connect(nil)
ctx := NewContext(context.Background(), txn)
conn.(driver.ExecerContext).ExecContext(ctx, "myoperation,mycollection", nil)
txn.StartSegment("second").End()
txn.End()
parentGUID := "4981855ad8681d0d"
app.ExpectSpanEvents(t, []internal.WantEvent{
{
Intrinsics: map[string]interface{}{
"name": "Custom/second",
"parentId": parentGUID,
"category": "generic",
},
AgentAttributes: map[string]interface{}{},
},
{
Intrinsics: map[string]interface{}{
"name": "OtherTransaction/Go/hello",
"guid": parentGUID,
"nr.entryPoint": true,
"category": "generic",
"transaction.name": "OtherTransaction/Go/hello",
},
AgentAttributes: map[string]interface{}{},
},
})
}
// Ensure that if the driver used returns driver.ErrSkip that spans still have correct parentage
func TestQueryContextErrSkipReturned(t *testing.T) {
app := testApp(distributedTracingReplyFields, enableBetterCAT, t)
connector := InstrumentSQLConnector(testConnectorErr{}, testBuilder)
txn := app.StartTransaction("hello")
conn, _ := connector.Connect(nil)
ctx := NewContext(context.Background(), txn)
conn.(driver.QueryerContext).QueryContext(ctx, "myoperation,mycollection", nil)
txn.StartSegment("second").End()
txn.End()
parentGUID := "4981855ad8681d0d"
app.ExpectSpanEvents(t, []internal.WantEvent{
{
Intrinsics: map[string]interface{}{
"name": "Custom/second",
"parentId": parentGUID,
"category": "generic",
},
AgentAttributes: map[string]interface{}{},
},
{
Intrinsics: map[string]interface{}{
"name": "OtherTransaction/Go/hello",
"guid": parentGUID,
"nr.entryPoint": true,
"category": "generic",
"transaction.name": "OtherTransaction/Go/hello",
},
AgentAttributes: map[string]interface{}{},
},
})
}
// Ensure we don't panic if the txn is nil
func TestSQLNoTxnNoCry(t *testing.T) {
connector := InstrumentSQLConnector(testConnector{}, testBuilder)
conn, _ := connector.Connect(nil)
conn.(driver.QueryerContext).QueryContext(context.Background(), "myoperation,mycollection", nil)
}
|