-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Knex #468
Merged
DominicKramer
merged 14 commits into
googleapis:master
from
DominicKramer:feature/support-knex
May 23, 2017
Merged
Support Knex #468
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
698af35
Initial commit
DominicKramer 6b9b5da
Fix the `test-config-plugins.js` test
DominicKramer a7c6bdf
Fix context loss in knex without patching bluebird
DominicKramer f80681f
Fix the test-config-plugins.js test
DominicKramer aa7279a
Intercept the module root to patch `transaction`
DominicKramer cead626
Intercept `lib/transaction.js` to preserve context
DominicKramer 93b2db2
Fix the use of rollback in the unit tests
DominicKramer 4b041f7
Add support for Knex 0.13
DominicKramer fe7dc68
Add tests for knex 0.11.x
DominicKramer cdd2c11
Add tests for knex 0.10.x
DominicKramer 74c46ab
Address GitHub comments
DominicKramer c74a04a
Remove an unused require
DominicKramer 19663ce
Rearrange functions to group related items
DominicKramer ec1d9c0
Move `require('assert')` to the top of the file
DominicKramer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
var shimmer = require('shimmer'); | ||
var util = require('util'); | ||
|
||
// knex 0.10.x and 0.11.x do not need patching | ||
var VERSIONS = '>=0.12 <=0.13'; | ||
|
||
function patchClient(Client, api) { | ||
shimmer.wrap(Client.prototype, 'runner', function(original) { | ||
return function() { | ||
var runner = original.apply(this, arguments); | ||
runner.query = api.wrap(runner.query); | ||
return runner; | ||
}; | ||
}); | ||
} | ||
|
||
function unpatchClient(Client) { | ||
shimmer.unwrap(Client.prototype, 'runner'); | ||
} | ||
|
||
function interceptTransaction(Transaction, api) { | ||
function WrappedTransaction(client, container, config, outerTx) { | ||
Transaction.call(this, client, api.wrap(container), config, outerTx); | ||
} | ||
util.inherits(WrappedTransaction, Transaction); | ||
return WrappedTransaction; | ||
} | ||
|
||
module.exports = [ | ||
{ | ||
file: 'lib/transaction.js', | ||
versions: VERSIONS, | ||
intercept: interceptTransaction | ||
}, | ||
{ | ||
file: 'lib/client.js', | ||
versions: VERSIONS, | ||
patch: patchClient, | ||
unpatch: unpatchClient | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('knex'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "knex0.10", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"dependencies": { | ||
"knex": "^0.10.0", | ||
"mysql": "^2.13.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('knex'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "knex0.11", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"dependencies": { | ||
"knex": "^0.11.0", | ||
"mysql": "^2.13.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('knex'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "knex0.12", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"dependencies": { | ||
"knex": "^0.12.9", | ||
"mysql": "^2.13.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('knex'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "knex0.13", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"dependencies": { | ||
"knex": "^0.13.0", | ||
"mysql": "^2.13.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
/** | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
|
||
var common = require('./common.js'); | ||
var traceLabels = require('../../src/trace-labels.js'); | ||
|
||
var RESULT_SIZE = 5; | ||
var TABLE_NAME = 't'; | ||
|
||
var obj = { | ||
k: 1, | ||
v: 'obj' | ||
}; | ||
|
||
var versions = { | ||
knex10: './fixtures/knex0.10', | ||
knex11: './fixtures/knex0.11', | ||
knex12: './fixtures/knex0.12', | ||
knex13: './fixtures/knex0.13' | ||
}; | ||
|
||
describe('test-trace-knex', function() { | ||
var agent; | ||
before(function() { | ||
agent = require('../..').start({ | ||
logLevel: 2, | ||
flushDelaySeconds: 1, | ||
enhancedDatabaseReporting: true, | ||
databaseResultReportingSize: RESULT_SIZE | ||
}); | ||
}); | ||
|
||
Object.keys(versions).forEach(function(version) { | ||
describe('with mysql and ' + version, function() { | ||
var knex; | ||
before(function() { | ||
knex = require(versions[version])({ | ||
client: 'mysql', | ||
connection: require('../mysql-config') | ||
}); | ||
}); | ||
|
||
beforeEach(function(done) { | ||
knex.schema.createTable(TABLE_NAME, function(table) { | ||
table.integer('k', 10); | ||
table.string('v', 10); | ||
}).then(function(result) { | ||
assert.ok(result); | ||
knex.insert(obj).into(TABLE_NAME).then(function(result) { | ||
assert.ok(result); | ||
common.cleanTraces(agent); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
afterEach(function(done) { | ||
knex.schema.dropTable(TABLE_NAME).then(function(result) { | ||
assert.ok(result); | ||
common.cleanTraces(agent); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should perform basic operations using ' + version, function(done) { | ||
common.runInTransaction(agent, function(endRootSpan) { | ||
knex(TABLE_NAME).select().then(function(res) { | ||
endRootSpan(); | ||
assert(res); | ||
assert.equal(res.length, 1); | ||
assert.equal(res[0].k, 1); | ||
assert.equal(res[0].v, 'obj'); | ||
var spans = common.getMatchingSpans(agent, function (span) { | ||
return span.name === 'mysql-query'; | ||
}); | ||
if (version === 'knex11') { | ||
assert.equal(spans.length, 2); | ||
assert.equal(spans[0].labels.sql, 'SELECT 1'); | ||
assert.equal(spans[1].labels.sql, 'select * from `t`'); | ||
} | ||
else { | ||
assert.equal(spans.length, 1); | ||
assert.equal(spans[0].labels.sql, 'select * from `t`'); | ||
} | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should propagate context using ' + version, function(done) { | ||
common.runInTransaction(agent, function(endRootSpan) { | ||
knex.select().from(TABLE_NAME).then(function(res) { | ||
assert.ok(common.hasContext()); | ||
endRootSpan(); | ||
done(); | ||
}).catch(function(e) { | ||
assert.ifError(e); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should remove trace frames from stack using ' + version, function(done) { | ||
common.runInTransaction(agent, function(endRootSpan) { | ||
knex.select().from(TABLE_NAME).then(function(res) { | ||
endRootSpan(); | ||
var spans = common.getMatchingSpans(agent, function (span) { | ||
return span.name === 'mysql-query'; | ||
}); | ||
var labels = spans[0].labels; | ||
var stackTrace = JSON.parse(labels[traceLabels.STACK_TRACE_DETAILS_KEY]); | ||
// Ensure that our patch is on top of the stack | ||
assert( | ||
stackTrace.stack_frame[0].method_name.indexOf('createQuery_trace') !== -1); | ||
done(); | ||
}).catch(function(e) { | ||
assert.ifError(e); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should work with events using ' + version, function(done) { | ||
common.runInTransaction(agent, function(endRootSpan) { | ||
knex.select().from(TABLE_NAME).on('query-response', function(response, obj, builder) { | ||
var row = response[0]; | ||
assert.ok(row); | ||
assert.equal(row.k, 1); | ||
assert.equal(row.v, 'obj'); | ||
}).on('query-error', function(err, obj) { | ||
assert.ifError(err); | ||
}).then(function(res) { | ||
endRootSpan(); | ||
var spans = common.getMatchingSpans(agent, function (span) { | ||
return span.name === 'mysql-query'; | ||
}); | ||
if (version === 'knex11') { | ||
assert.equal(spans.length, 2); | ||
assert.equal(spans[0].labels.sql, 'SELECT 1'); | ||
assert.equal(spans[1].labels.sql, 'select * from `t`'); | ||
} | ||
else { | ||
assert.equal(spans.length, 1); | ||
assert.equal(spans[0].labels.sql, 'select * from `t`'); | ||
} | ||
done(); | ||
}).catch(function(e) { | ||
assert.ifError(e); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should work without events or callback using ' + version, function(done) { | ||
common.runInTransaction(agent, function(endRootSpan) { | ||
knex.select().from(TABLE_NAME).then(function(result) { | ||
setTimeout(function() { | ||
endRootSpan(); | ||
var spans = common.getMatchingSpans(agent, function (span) { | ||
return span.name === 'mysql-query'; | ||
}); | ||
if (version === 'knex11') { | ||
assert.equal(spans.length, 2); | ||
assert.equal(spans[0].labels.sql, 'SELECT 1'); | ||
assert.equal(spans[1].labels.sql, 'select * from `t`'); | ||
} | ||
else { | ||
assert.equal(spans.length, 1); | ||
assert.equal(spans[0].labels.sql, 'select * from `t`'); | ||
} | ||
done(); | ||
}, 50); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should perform basic transaction using ' + version, function(done) { | ||
var obj2 = { | ||
k: 2, | ||
v: 'obj2' | ||
}; | ||
common.runInTransaction(agent, function(endRootSpan) { | ||
knex.transaction(function(trx) { | ||
knex.insert(obj2) | ||
.into(TABLE_NAME) | ||
.transacting(trx) | ||
.then(function(res) { | ||
return trx.select() | ||
.from(TABLE_NAME) | ||
.then(function(res) { | ||
assert.equal(res.length, 2); | ||
assert.equal(res[0].k, 1); | ||
assert.equal(res[0].v, 'obj'); | ||
assert.equal(res[1].k, 2); | ||
assert.equal(res[1].v, 'obj2'); | ||
}).catch(function(err) { | ||
assert.ifError(err); | ||
}); | ||
}) | ||
.then(function() { | ||
trx.rollback(new Error('Rolling back')); | ||
}).catch(function(err) { | ||
assert.ifError(err); | ||
}); | ||
}).catch(function(err) { | ||
assert.ok(err); | ||
assert.strictEqual(err.message, 'Rolling back'); | ||
knex.select() | ||
.from(TABLE_NAME) | ||
.then(function(res) { | ||
endRootSpan(); | ||
assert.equal(res.length, 1); | ||
assert.equal(res[0].k, 1); | ||
assert.equal(res[0].v, 'obj'); | ||
var spans = common.getMatchingSpans(agent, function (span) { | ||
return span.name === 'mysql-query'; | ||
}); | ||
var expectedCmds; | ||
if (version === 'knex10') { | ||
expectedCmds = ['BEGIN;', | ||
'insert into `t` (`k`, `v`) values (?, ?)', | ||
'select * from `t`', | ||
'ROLLBACK;', | ||
'select * from `t`']; | ||
} | ||
else if (version === 'knex11') { | ||
expectedCmds = ['SELECT 1', | ||
'BEGIN;', | ||
'insert into `t` (`k`, `v`) values (?, ?)', | ||
'select * from `t`', | ||
'ROLLBACK;', | ||
'SELECT 1', | ||
'select * from `t`']; | ||
} | ||
else { | ||
expectedCmds = ['insert into `t` (`k`, `v`) values (?, ?)', | ||
'select * from `t`', | ||
'ROLLBACK;', | ||
'select * from `t`']; | ||
} | ||
assert.equal(expectedCmds.length, spans.length); | ||
for (var i = 0; i < spans.length; i++) { | ||
assert.equal(spans[i].labels.sql, expectedCmds[i]); | ||
} | ||
done(); | ||
}).catch(function(err) { | ||
assert.ifError(err); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.