Skip to content

Commit

Permalink
fix(sqs): fix missing async context in recent aws-sdk/client-sqs version
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 authored and Bastian Krol committed May 15, 2023
1 parent 6c1e3da commit 6ae90e7
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ test-duration-breakdown
coverage
/db2
instana-*.tgz
.vscode
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ async function receiveAsync() {
const data = await sqsPromise;

return new Promise(resolve => {
// TODO: This could be simplified by wrapping the SQS.prototype.receiveMessage method.
// See v3/sqs.js
instana.sdk.runInAsyncContext(sqsPromise.instanaAsyncContext, async () => {
if (data && data.error) {
log('receive message data error', data.error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ app.get('/warn-logs', (req, res) => {
async function runAsPromise(isV2Style = false) {
const command = new awsSdk3.ReceiveMessageCommand(receiveParams);
numberOfReceiveMessageAttempts++;

const promise = isV2Style ? sqsv2.receiveMessage(receiveParams) : sqs.send(command);
let span;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const sendSnsNotificationToSqsQueue = require('./sendNonInstrumented').sendSnsNo

const sendingMethods = ['v3', 'cb', 'v2'];
const receivingMethods = ['v3', 'cb', 'v2'];

const getNextSendMethod = require('@instana/core/test/test_util/circular_list').getCircularList(sendingMethods);
const getNextReceiveMethod = require('@instana/core/test/test_util/circular_list').getCircularList(receivingMethods);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ function instrumentedReceiveMessage(ctx, originalReceiveMessage, originalArgs) {
// promise use case
if (typeof awsRequest.promise === 'function' && typeof originalCallback !== 'function') {
const originalPromiseFn = awsRequest.promise;

awsRequest.promise = cls.ns.bind(function () {
const promise = originalPromiseFn.apply(awsRequest, arguments);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ let onFileLoaded = false;
exports.init = function init() {
sqsConsumer.init();

// NOTE: each aws product can have it's own init fn to wrap or unwrap specific functions
awsProducts.forEach(awsProduct => awsProduct.init && awsProduct.init(requireHook, shimmer));

/**
* @aws-sdk/smithly-client >= 3.36.0 changed how the dist structure gets delivered
* https://github.com/aws/aws-sdk-js-v3/blob/main/packages/smithy-client/CHANGELOG.md#3360-2021-10-08
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ const operations = Object.keys(operationsInfo);
const SPAN_NAME = 'sqs';

class InstanaAWSSQS extends InstanaAWSProduct {
init(requireHook, shimmer) {
requireHook.onFileLoad(/@aws-sdk\/client-sqs\/dist-cjs\/SQS\.js/, function (module) {
shimmer.wrap(module.SQS.prototype, 'receiveMessage', function (originalReceiveMsgFn) {
return function instanaReceiveMessage() {
return cls.ns.runAndReturn(() => {
const ctx = cls.getAsyncContext();
this._instanaCtx = ctx;
const promise = originalReceiveMsgFn.apply(this, arguments);

promise.then = cls.ns.bind(promise.then);

if (promise.catch) {
promise.catch = cls.ns.bind(promise.catch);
}
if (promise.finally) {
promise.finally = cls.ns.bind(promise.finally);
}

return promise;
});
};
});
});
}

instrumentedSmithySend(ctx, originalSend, smithySendArgs) {
const commandName = smithySendArgs[0].constructor.name;
const operation = operationsInfo[commandName];
Expand Down Expand Up @@ -166,6 +191,8 @@ class InstanaAWSSQS extends InstanaAWSProduct {
const command = smithySendArgs[0];
const sendMessageInput = command.input;

// Note: we pass in ctx._instanaCtx as the second parameter to runAndReturn which will run the function in the
// context belonging to this SQS promise.
return cls.ns.runAndReturn(() => {
const self = this;
const span = cls.startSpan(SPAN_NAME, ENTRY);
Expand Down Expand Up @@ -237,6 +264,7 @@ class InstanaAWSSQS extends InstanaAWSProduct {
setImmediate(() => this.finishSpan(null, span));
} else if (data && data.Messages && data.Messages.length > 0) {
const messages = data.Messages;

let tracingAttributes = readTracingAttributes(messages[0].MessageAttributes);
if (!hasTracingAttributes(tracingAttributes)) {
tracingAttributes = readTracingAttributesFromSns(messages[0].Body);
Expand Down Expand Up @@ -283,7 +311,7 @@ class InstanaAWSSQS extends InstanaAWSProduct {

return request;
}
});
}, ctx._instanaCtx);
}

buildSpanData(operation, sendMessageInput) {
Expand Down
4 changes: 4 additions & 0 deletions packages/core/test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ exports.getTestTimeout = () => {
if (isCI()) {
return 30000;
}
// NOTE: Otherwise mocha will interrupt the debugging session quickly.
if (process.env.VSCODE_DEBUG === 'true') {
return 30000;
}
return 5000;
};

0 comments on commit 6ae90e7

Please sign in to comment.