Skip to content

Commit 6162673

Browse files
authored
Merge pull request newrelic#2 from newrelic/nwolfe/mysql2-promise
Added mysql2 promise support and tests
2 parents b4f6e7a + ee928cf commit 6162673

File tree

10 files changed

+191
-119
lines changed

10 files changed

+191
-119
lines changed

lib/instrumentation.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ exports.callbackInitialize = function callbackInitialize(shim, mysql) {
4343
}
4444
}
4545

46-
exports.promiseInitialize = function promiseInitialize(shim, mysql) {
46+
exports.promiseInitialize = function promiseInitialize(shim) {
47+
const callbackAPI = shim.require('./index')
4748

49+
if (callbackAPI && !shim.isWrapped(callbackAPI.createConnection)) {
50+
exports.callbackInitialize(shim, callbackAPI)
51+
}
4852
}
4953

5054
function wrapGetConnection(shim, connectable) {

tests/versioned/common/basic-pool.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ const urltils = require('newrelic/lib/util/urltils') // TODO: Expose via test ut
77
const utils = require('@newrelic/test-utilities')
88

99
const params = setup.params
10-
const DBUSER = 'root'
11-
const DBNAME = 'agent_integration'
1210

1311
var config = getConfig({})
1412
function getConfig(extras) {
1513
var conf = {
1614
connectionLimit: 10,
17-
host: params.mysql_host,
18-
port: params.mysql_port,
19-
user: DBUSER,
20-
database: DBNAME
15+
host: params.host,
16+
port: params.port,
17+
user: params.user,
18+
database: params.database
2119
}
2220

2321
for (var key in extras) { // eslint-disable-line guard-for-in
@@ -43,8 +41,8 @@ module.exports = (t, requireMySQL) => {
4341
var badConfig = {
4442
connectionLimit: 10,
4543
host: 'nohost',
46-
user: DBUSER,
47-
database: DBNAME
44+
user: params.user,
45+
database: params.database
4846
}
4947

5048
t.tearDown(() => {
@@ -129,7 +127,7 @@ module.exports = (t, requireMySQL) => {
129127
)
130128
t.equal(
131129
seg.parameters.database_name,
132-
DBNAME,
130+
params.database,
133131
'set database name'
134132
)
135133
t.equal(
@@ -160,7 +158,7 @@ module.exports = (t, requireMySQL) => {
160158
)
161159
t.equal(
162160
seg.parameters.database_name,
163-
DBNAME,
161+
params.database,
164162
'should set database name'
165163
)
166164
helper.agent.config.datastore_tracer.instance_reporting.enabled = true
@@ -219,7 +217,7 @@ module.exports = (t, requireMySQL) => {
219217
)
220218
t.equal(
221219
seg.parameters.database_name,
222-
DBNAME,
220+
params.database,
223221
'set database name'
224222
)
225223
t.equal(seg.parameters.port_path_or_id, String(defaultConfig.port), 'set port')
@@ -248,7 +246,7 @@ module.exports = (t, requireMySQL) => {
248246
)
249247
t.equal(
250248
seg.parameters.database_name,
251-
DBNAME,
249+
params.database,
252250
'should set database name'
253251
)
254252
t.equal(
@@ -381,7 +379,7 @@ module.exports = (t, requireMySQL) => {
381379
)
382380
t.equal(
383381
seg.parameters.database_name,
384-
DBNAME,
382+
params.database,
385383
'set database name'
386384
)
387385
txn.end(socketPool.end.bind(socketPool, t.end))

tests/versioned/common/basic.js

+3-43
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ module.exports = (t, requireMySQL) => {
157157
})
158158
})
159159

160-
t.test('ensure database name changes with a use statement', (t) => {
160+
t.test('database name should change with use statement', (t) => {
161161
helper.runInTransaction((txn) => {
162162
withRetry.getClient((err, client) => {
163163
if (!t.error(err)) {
@@ -177,9 +177,9 @@ module.exports = (t, requireMySQL) => {
177177
t.ok(seg, 'there is a segment')
178178
t.equal(
179179
seg.parameters.host,
180-
urltils.isLocalhost(params.mysql_host)
180+
urltils.isLocalhost(params.host)
181181
? helper.agent.config.getHostnameSafe()
182-
: params.mysql_host,
182+
: params.host,
183183
'set host'
184184
)
185185
t.equal(
@@ -354,46 +354,6 @@ module.exports = (t, requireMySQL) => {
354354
})
355355
})
356356
})
357-
358-
t.test('ensure database name changes with a use statement', (t) => {
359-
helper.runInTransaction((txn) => {
360-
withRetry.getClient((err, client) => {
361-
client.query('create database if not exists test_db;', (err) => {
362-
t.error(err)
363-
client.query('use test_db;', (err) => {
364-
t.error(err)
365-
client.query('SELECT 1 + 1 AS solution', (err) => {
366-
var seg = helper.agent.tracer.getSegment().parent
367-
t.error(err)
368-
if (t.ok(seg, 'should have a segment')) {
369-
t.equal(
370-
seg.parameters.host,
371-
urltils.isLocalhost(params.mysql_host)
372-
? helper.agent.config.getHostnameSafe()
373-
: params.mysql_host,
374-
'should set host parameter'
375-
)
376-
t.equal(
377-
seg.parameters.database_name,
378-
'test_db',
379-
'should use new database name'
380-
)
381-
t.equal(
382-
seg.parameters.port_path_or_id,
383-
'3306',
384-
'should set port parameter'
385-
)
386-
}
387-
client.query('drop test_db;', () => {
388-
withRetry.release(client)
389-
txn.end(t.end)
390-
})
391-
})
392-
})
393-
})
394-
})
395-
})
396-
})
397357
})
398358
}
399359

tests/versioned/common/pooling.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ const setup = require('./setup')
55
const utils = require('@newrelic/test-utilities')
66

77

8-
const DBNAME = 'agent_integration'
9-
const DBTABLE = 'test'
10-
11-
128
module.exports = (t, requireMySQL) => {
139
t.test('MySQL instrumentation with a connection pool', {timeout: 30000}, (t) => {
1410
let helper = utils.TestAgent.makeInstrumented()
@@ -64,10 +60,7 @@ module.exports = (t, requireMySQL) => {
6460
return
6561
}
6662

67-
var query =
68-
'SELECT *' +
69-
' FROM ' + DBNAME + '.' + DBTABLE +
70-
' WHERE id = ?'
63+
var query = `SELECT * FROM ${setup.params.database}.test WHERE id = ?`
7164
client.query(query, [params.id], (err, results) => {
7265
withRetry.release(client) // always release back to the pool
7366

@@ -104,22 +97,26 @@ module.exports = (t, requireMySQL) => {
10497
return
10598
}
10699

107-
t.equals(row.id, 1, 'node-mysql should still work (found id)')
108-
t.equals(row.test_value, 'hamburgefontstiv',
109-
'mysql driver should still work (found value)')
100+
t.equals(row.id, 1, 'mysql should still work (found id)')
101+
t.equals(
102+
row.test_value,
103+
'hamburgefontstiv',
104+
'mysql driver should still work (found value)'
105+
)
110106

111107
txn.end()
112108

113109
var trace = txn.trace
114110
t.ok(trace, 'trace should exist')
115111
t.ok(trace.root, 'root element should exist.')
116-
t.equals(trace.root.children.length, 1, 'There should be only one child.')
117112

118-
var selectSegment = trace.root.children[0]
113+
t.ok(trace.root.children.length < 3, 'should have one or two children')
114+
115+
var selectSegment = trace.root.children[trace.root.children.length - 1]
119116
t.ok(selectSegment, 'trace segment for first SELECT should exist')
120117
t.equals(
121118
selectSegment.name,
122-
'Datastore/statement/MySQL/agent_integration.test/select',
119+
`Datastore/statement/MySQL/${setup.params.database}.test/select`,
123120
'should register as SELECT'
124121
)
125122

tests/versioned/common/setup.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ const async = require('async')
66
module.exports = exports = setup
77
exports.pool = setupPool
88
const params = exports.params = {
9-
mysql_host: process.env.NR_NODE_TEST_MYSQL_HOST || 'localhost',
10-
mysql_port: process.env.NR_NODE_TEST_MYSQL_PORT || 3306
9+
host: process.env.NR_NODE_TEST_MYSQL_HOST || 'localhost',
10+
port: process.env.NR_NODE_TEST_MYSQL_PORT || 3306,
11+
user: 'test_user',
12+
database: 'agent_integration_' + Math.floor(Math.random() * 1000)
1113
}
1214

1315
function setup(mysql, cb) {
@@ -22,9 +24,9 @@ function setup(mysql, cb) {
2224
})
2325

2426
async.eachSeries([
25-
'CREATE USER test_user',
26-
'GRANT ALL ON *.* TO `test_user`',
27-
'CREATE DATABASE IF NOT EXISTS `agent_integration`'
27+
`CREATE USER ${params.user}`,
28+
`GRANT ALL ON *.* TO ${params.user}`,
29+
`CREATE DATABASE IF NOT EXISTS ${params.database}`
2830
], (sql, cb) => {
2931
client.query(sql, (err) => {
3032
// Travis uses MySQL 5.4 which does not support `IF NOT EXISTS` for
@@ -45,12 +47,7 @@ function setup(mysql, cb) {
4547

4648
// 2. Create the table and data as test user.
4749
(cb) => {
48-
var client = mysql.createConnection({
49-
host: params.mysql_host,
50-
port: params.mysql_port,
51-
user: 'test_user',
52-
database: 'agent_integration'
53-
})
50+
var client = mysql.createConnection(params)
5451

5552
async.eachSeries([
5653
[
@@ -83,12 +80,7 @@ function setupPool(mysql, logger) {
8380
log: (message) => logger.info(message),
8481

8582
create: (callback) => {
86-
var client = mysql.createConnection({
87-
user: 'test_user',
88-
database: 'agent_integration',
89-
host: params.mysql_host,
90-
port: params.mysql_port
91-
})
83+
var client = mysql.createConnection(params)
9284

9385
client.on('error', (err) => {
9486
logger.error('MySQL connection errored out, destroying connection')

tests/versioned/common/transactions.js

+1-13
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const utils = require('@newrelic/test-utilities')
55

66

77
const params = setup.params
8-
const DBUSER = 'test_user'
9-
const DBNAME = 'agent_integration'
108

119

1210
module.exports = (t, requireMySQL) => {
@@ -15,24 +13,14 @@ module.exports = (t, requireMySQL) => {
1513

1614
// set up the instrumentation before loading MySQL
1715
const helper = utils.TestAgent.makeInstrumented()
18-
helper.registerInstrumentation({
19-
moduleName: 'mysql',
20-
type: 'datastore',
21-
onRequire: require('../../../lib/instrumentation').callbackInitialize
22-
})
2316
const mysql = requireMySQL(helper)
2417

2518
t.tearDown(() => helper.unload())
2619

2720
setup(mysql, function(error) {
2821
t.error(error)
2922

30-
const client = mysql.createConnection({
31-
user: DBUSER,
32-
database: DBNAME,
33-
host: params.mysql_host,
34-
port: params.mysql_port
35-
})
23+
const client = mysql.createConnection(params)
3624

3725
t.tearDown(() => client.end())
3826

tests/versioned/mysql/newrelic.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
'use strict'
2+
13
exports.config = {
2-
app_name : ['My Application'],
3-
license_key : 'license key here',
4-
logging : {
5-
level : 'debug',
6-
filepath : '../../../newrelic_agent.log'
4+
app_name: ['My Application'],
5+
license_key: 'license key here',
6+
logging: {
7+
level: 'debug',
8+
filepath: '../../../newrelic_agent.log'
79
},
810
utilization: {
911
detect_aws: false,
@@ -13,11 +15,11 @@ exports.config = {
1315
detect_docker: false
1416
},
1517
slow_sql: {
16-
enabled: true,
18+
enabled: true
1719
},
18-
transaction_tracer : {
20+
transaction_tracer: {
1921
record_sql: 'raw',
2022
explain_threshold: 0,
21-
enabled : true
23+
enabled: true
2224
}
2325
}

tests/versioned/mysql2/newrelic.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
'use strict'
2+
13
exports.config = {
2-
app_name : ['My Application'],
3-
license_key : 'license key here',
4-
logging : {
5-
level : 'debug',
6-
filepath : '../../../newrelic_agent.log'
4+
app_name: ['My Application'],
5+
license_key: 'license key here',
6+
logging: {
7+
level: 'debug',
8+
filepath: '../../../newrelic_agent.log'
79
},
810
utilization: {
911
detect_aws: false,
@@ -13,11 +15,11 @@ exports.config = {
1315
detect_docker: false
1416
},
1517
slow_sql: {
16-
enabled: true,
18+
enabled: true
1719
},
18-
transaction_tracer : {
20+
transaction_tracer: {
1921
record_sql: 'raw',
2022
explain_threshold: 0,
21-
enabled : true
23+
enabled: true
2224
}
2325
}

tests/versioned/mysql2/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"basic-pool.tap.js",
1515
"basic.tap.js",
1616
"pooling.tap.js",
17-
"transactions.tap.js"
17+
"transactions.tap.js",
18+
"promises.tap.js"
1819
]
1920
},
2021
{
@@ -28,7 +29,8 @@
2829
"basic-pool.tap.js",
2930
"basic.tap.js",
3031
"pooling.tap.js",
31-
"transactions.tap.js"
32+
"transactions.tap.js",
33+
"promises.tap.js"
3234
]
3335
}
3436
],

0 commit comments

Comments
 (0)