Skip to content
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

fix collection of queries for mysql2 #600

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Manual mode requires that you pass around the segment reference. See the example

MYSQL_DATABASE_VERSION Sets additional data for the sql subsegment.
MYSQL_DRIVER_VERSION Sets additional data for the sql subsegment.
AWS_XRAY_COLLECT_SQL_QUERIES Add query to the subsegments sql data.

### Lambda Example

Expand Down
10 changes: 5 additions & 5 deletions packages/mysql/lib/mysql_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ function resolveArguments(argsObj) {
// Patch for mysql2
if (argsObj[0].values) {
args.values = argsObj[0].values; // mysql implementation
} else if (typeof argsObj[2] === 'function') {
args.values = typeof argsObj[1] !== 'function' ? argsObj[1] : null; // mysql2 implementation
} else if (typeof argsObj[1] !== 'function') {
args.values = argsObj[1]; // mysql2 implementation
}
args.callback = typeof argsObj[1] === 'function'
? argsObj[1]
Expand Down Expand Up @@ -261,12 +261,12 @@ function captureOperation(name) {

/**
* Generate a SQL data object. Note that this implementation differs from
* that in postgres_p.js because the posgres client structures commands
* that in postgres_p.js because the postgres client structures commands
* and prepared statements differently than mysql/mysql2.
*
* @param {object} config
* @param {any} values
* @param {string} sql
* @param {string|Object} sql
* @returns SQL data object
*/
function createSqlData(config, values, sql) {
Expand All @@ -276,7 +276,7 @@ function createSqlData(config, values, sql) {
commandType);

if (process.env.AWS_XRAY_COLLECT_SQL_QUERIES && sql) {
data.sanitized_query = sql;
data.sanitized_query = typeof sql === 'object' ? sql.sql : sql;
}

return data;
Expand Down
13 changes: 13 additions & 0 deletions packages/mysql/test/unit/mysql_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ describe('captureMySQL', function() {
stubAddSql.should.have.been.calledWithExactly(sinon.match.instanceOf(SqlData));
stubAddSql.should.have.been.calledWithExactly(sinon.match.has('sanitized_query', 'sql here'));
});
it('should add query to the subsegments sql data when query options are used', function () {
sandbox.stub(process, 'env').value({ ...AWSXRay, 'AWS_XRAY_COLLECT_SQL_QUERIES': true });
var stubAddSql = sandbox.stub(subsegment, 'addSqlData');
var stubDataInit = sandbox.stub(SqlData.prototype, 'init');
var conParam = connectionObj.config;

query.call(connectionObj, { sql: 'sql with values binding = ?' }, [1]);

stubDataInit.should.have.been.calledWithExactly(process.env.MYSQL_DATABASE_VERSION, process.env.MYSQL_DRIVER_VERSION,
conParam.user, conParam.host + ':' + conParam.port + '/' + conParam.database, 'statement');
stubAddSql.should.have.been.calledWithExactly(sinon.match.instanceOf(SqlData));
stubAddSql.should.have.been.calledWithExactly(sinon.match.has('sanitized_query', 'sql with values binding = ?'));
});
it('should NOT add query to the subsegments sql data when AWS_XRAY_COLLECT_SQL_QUERIES is not set', function () {
sandbox.stub(process, 'env').value({ ...AWSXRay, 'AWS_XRAY_COLLECT_SQL_QUERIES': undefined });
var stubAddSql = sandbox.stub(subsegment, 'addSqlData');
Expand Down