Skip to content

Commit 3abe727

Browse files
jasonterandoJason Terando
andauthored
Add support for including sql query in sql subsegment for MySQL (#564)
* Add support for including sql query in sql subsegment for MySQL * Update createSqlData to accept values and sql as arguments, and the call to createSqlData to send those values from the arguments made to the sql call * Add function comments to mysqL_p.createSqlData --------- Co-authored-by: Jason Terando <[email protected]>
1 parent 0fe39de commit 3abe727

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

packages/core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ section for different usages.
5959
AWS_XRAY_DAEMON_ADDRESS For setting the daemon address and port.
6060
AWS_XRAY_CONTEXT_MISSING For setting the SDK behavior when trace context is missing. Valid values are 'RUNTIME_ERROR', 'IGNORE_ERROR' or 'LOG_ERROR'. The SDK's default behavior is 'LOG_ERROR'.
6161
AWS_XRAY_LOG_LEVEL Sets a log level for the SDK built in logger. This value is ignored if AWS_XRAY_DEBUG_MODE is set.
62-
AWS_XRAY_COLLECT_SQL_QUERIES Enables SQL query capture (currently only Postgres supported)
62+
AWS_XRAY_COLLECT_SQL_QUERIES Enables SQL query capture (currently only Postgres and MySQL supported)
6363

6464
### Daemon configuration
6565

packages/mysql/lib/mysql_p.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,7 @@ function captureOperation(name) {
242242
if (isPromise(command)) {
243243
command.then(() => {
244244
subsegment.close();
245-
});
246-
247-
command.catch(errorCapturer);
245+
}).catch (errorCapturer);
248246
} else {
249247
command.on('end', function() {
250248
subsegment.close();
@@ -254,19 +252,32 @@ function captureOperation(name) {
254252
}
255253
}
256254

257-
subsegment.addSqlData(createSqlData(config, command));
255+
subsegment.addSqlData(createSqlData(config, args.values, args.sql));
258256
subsegment.namespace = 'remote';
259257

260258
return command;
261259
};
262260
}
263261

264-
function createSqlData(config, command) {
265-
var commandType = command.values ? PREPARED : null;
266-
262+
/**
263+
* Generate a SQL data object. Note that this implementation differs from
264+
* that in postgres_p.js because the posgres client structures commands
265+
* and prepared statements differently than mysql/mysql2.
266+
*
267+
* @param {object} config
268+
* @param {any} values
269+
* @param {string} sql
270+
* @returns SQL data object
271+
*/
272+
function createSqlData(config, values, sql) {
273+
var commandType = values ? PREPARED : null;
267274
var data = new SqlData(DATABASE_VERS, DRIVER_VERS, config.user,
268275
config.host + ':' + config.port + '/' + config.database,
269276
commandType);
270277

278+
if (process.env.AWS_XRAY_COLLECT_SQL_QUERIES && sql) {
279+
data.sanitized_query = sql;
280+
}
281+
271282
return data;
272283
}

packages/mysql/test/unit/mysql_p.test.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('captureMySQL', function() {
141141
var stubDataInit = sandbox.stub(SqlData.prototype, 'init');
142142
var config = conn.config;
143143

144-
query.call(connectionObj, 'sql here');
144+
query.call(connectionObj, 'sql here', [1]);
145145

146146
stubDataInit.should.have.been.calledWithExactly(undefined, undefined, config.user,
147147
config.host + ':' + config.port + '/' + config.database, 'statement');
@@ -247,6 +247,34 @@ describe('captureMySQL', function() {
247247
done();
248248
}, 50);
249249
});
250+
it('should add query to the subsegments sql data when AWS_XRAY_COLLECT_SQL_QUERIES is truthy', function () {
251+
sandbox.stub(process, 'env').value({ ...AWSXRay, 'AWS_XRAY_COLLECT_SQL_QUERIES': true });
252+
var stubAddSql = sandbox.stub(subsegment, 'addSqlData');
253+
var stubDataInit = sandbox.stub(SqlData.prototype, 'init');
254+
var conParam = connectionObj.config;
255+
256+
query.call(connectionObj, 'sql here', [1]);
257+
258+
stubDataInit.should.have.been.calledWithExactly(process.env.MYSQL_DATABASE_VERSION, process.env.MYSQL_DRIVER_VERSION,
259+
conParam.user, conParam.host + ':' + conParam.port + '/' + conParam.database, 'statement');
260+
stubAddSql.should.have.been.calledWithExactly(sinon.match.instanceOf(SqlData));
261+
stubAddSql.should.have.been.calledWithExactly(sinon.match.has('sanitized_query', 'sql here'));
262+
});
263+
it('should NOT add query to the subsegments sql data when AWS_XRAY_COLLECT_SQL_QUERIES is not set', function () {
264+
sandbox.stub(process, 'env').value({ ...AWSXRay, 'AWS_XRAY_COLLECT_SQL_QUERIES': undefined });
265+
var stubAddSql = sandbox.stub(subsegment, 'addSqlData');
266+
var stubDataInit = sandbox.stub(SqlData.prototype, 'init');
267+
var conParam = connectionObj.config;
268+
269+
query.call(connectionObj, 'sql here', [1]);
270+
271+
stubDataInit.should.have.been.calledWithExactly(process.env.MYSQL_DATABASE_VERSION, process.env.MYSQL_DRIVER_VERSION,
272+
conParam.user, conParam.host + ':' + conParam.port + '/' + conParam.database, 'statement');
273+
stubAddSql.should.have.been.calledWithExactly(sinon.match.instanceOf(SqlData));
274+
sinon.assert.match(sinon.match, {
275+
'sanitized_query': undefined
276+
});
277+
});
250278
});
251279
});
252280

@@ -344,7 +372,6 @@ describe('captureMySQL', function() {
344372
stubClose.should.have.been.calledWithExactly(err);
345373
});
346374
});
347-
348375
});
349376
});
350377

@@ -430,7 +457,7 @@ describe('captureMySQL', function() {
430457
var stubDataInit = sandbox.stub(SqlData.prototype, 'init');
431458
var config = conn.config;
432459

433-
query.call(connectionObj, 'sql here');
460+
query.call(connectionObj, 'sql here', [1]);
434461

435462
stubDataInit.should.have.been.calledWithExactly(undefined, undefined, config.user,
436463
config.host + ':' + config.port + '/' + config.database, 'statement');
@@ -526,7 +553,7 @@ describe('captureMySQL', function() {
526553
var stubDataInit = sandbox.stub(SqlData.prototype, 'init');
527554
var config = conn.config;
528555

529-
query.call(connectionObj, 'sql here');
556+
query.call(connectionObj, 'sql here', [1]);
530557

531558
stubDataInit.should.have.been.calledWithExactly(undefined, undefined, config.user,
532559
config.host + ':' + config.port + '/' + config.database, 'statement');
@@ -673,7 +700,7 @@ describe('captureMySQL', function() {
673700
var stubDataInit = sandbox.stub(SqlData.prototype, 'init');
674701
var config = conn.config;
675702

676-
query.call(connectionObj, 'sql here');
703+
query.call(connectionObj, 'sql here', [1]);
677704

678705
stubDataInit.should.have.been.calledWithExactly(undefined, undefined, config.user,
679706
config.host + ':' + config.port + '/' + config.database, 'statement');

0 commit comments

Comments
 (0)