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

[aws-xray-sdk-mysql] Handle operations that return a promise instead of an event emitter (mysql2) #328

Merged
merged 1 commit into from
Sep 8, 2020
Merged
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
26 changes: 17 additions & 9 deletions packages/mysql/lib/mysql_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function patchOf(poolCluster) {

var resultPool = poolCluster[baseFcn].apply(poolCluster, args);
return patchObject(resultPool);
}
};
}

function patchGetConnection(pool) {
Expand All @@ -105,13 +105,13 @@ function patchGetConnection(pool) {
args[args.length-1] = (err, connection) => {
if(connection) patchObject(connection);
return callback(err, connection);
}
};
}

var result = pool[baseFcn].apply(pool, args);
if (result && result.then instanceof Function) return result.then(patchObject);
else return result;
}
};
}

function patchObject(connection) {
Expand Down Expand Up @@ -208,10 +208,6 @@ function captureOperation(name) {
command = originalOperation.call(this, sql, args.values, args.callback);

if (!args.callback) {
command.on('end', function() {
subsegment.close();
});

var errorCapturer = function (err) {
subsegment.close(err);

Expand All @@ -222,14 +218,26 @@ function captureOperation(name) {
}
};

command.on(events.errorMonitor || 'error', errorCapturer);
if (command.then instanceof Function) {
command.then(() => {
subsegment.close();
});

command.catch(errorCapturer);
} else {
command.on('end', function() {
subsegment.close();
});

command.on(events.errorMonitor || 'error', errorCapturer);
}
}

subsegment.addSqlData(createSqlData(config, command));
subsegment.namespace = 'remote';

return command;
}
};
}

function createSqlData(config, command) {
Expand Down