Skip to content

Commit

Permalink
refactor: optimized aws v3 initialization (#1571)
Browse files Browse the repository at this point in the history
refs https://jsw.ibm.com/browse/INSTA-13498

Extracted from #1556

With this refactoring, we can easily forward the config object now to the aws v3 pkg. Each class is instantiated with the config object, which contains the logger.
  • Loading branch information
kirrg001 authored Feb 18, 2025
1 parent b6a62db commit 5752973
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const { InstanaAWSProduct } = require('./instana_aws_product');
const SPAN_NAME = 'dynamodb';

class InstanaAWSDynamoDB extends InstanaAWSProduct {
constructor() {
super(SPAN_NAME);
}

instrumentedSmithySend(ctx, isActive, originalSend, smithySendArgs) {
if (cls.skipExitTracing({ isActive })) {
return originalSend.apply(ctx, smithySendArgs);
Expand Down Expand Up @@ -93,4 +97,4 @@ class InstanaAWSDynamoDB extends InstanaAWSProduct {
}
}

module.exports = new InstanaAWSDynamoDB(SPAN_NAME);
module.exports = InstanaAWSDynamoDB;
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ const awsProducts = [
require('./lambda')
];

const awsProductInstances = [];
const sqsConsumer = require('./sqs-consumer');

/** @type {Object.<string, import('./instana_aws_product').InstanaAWSProduct} */
const operationMap = {};

awsProducts.forEach(awsProduct => {
Object.assign(operationMap, awsProduct.getOperations());
});

let isActive = false;

exports.init = function init() {
sqsConsumer.init();
awsProducts.forEach(AwsProductClass => {
const awsProductInstance = new AwsProductClass();
awsProductInstances.push(awsProductInstance);

Object.assign(operationMap, awsProductInstance.getOperations());

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

sqsConsumer.init();

/**
* @aws-sdk/smithly-client >= 3.36.0 changed how the dist structure gets delivered
Expand Down Expand Up @@ -78,7 +83,7 @@ function shimSmithySend(originalSend) {
const command = smithySendArgs[0];

const serviceId = self.config && self.config.serviceId;
let awsProduct = serviceId && awsProducts.find(aws => aws.getServiceIdName() === serviceId.toLowerCase());
let awsProduct = serviceId && awsProductInstances.find(aws => aws.getServiceIdName() === serviceId.toLowerCase());
if (awsProduct && awsProduct.supportsOperation(command.constructor.name)) {
return awsProduct.instrumentedSmithySend(self, isActive, originalSend, smithySendArgs);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const { InstanaAWSProduct } = require('./instana_aws_product');
const SPAN_NAME = 'kinesis';

class InstanaAWSKinesis extends InstanaAWSProduct {
constructor() {
super(SPAN_NAME);
}

instrumentedSmithySend(ctx, isActive, originalSend, smithySendArgs) {
if (cls.skipExitTracing({ isActive })) {
return originalSend.apply(ctx, smithySendArgs);
Expand Down Expand Up @@ -81,4 +85,4 @@ class InstanaAWSKinesis extends InstanaAWSProduct {
}
}

module.exports = new InstanaAWSKinesis(SPAN_NAME);
module.exports = InstanaAWSKinesis;
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const SPAN_NAME = 'aws.lambda.invoke';
const CUSTOM_SERVICE_NAME = 'lambda';

class InstanaAWSLambda extends InstanaAWSProduct {
constructor() {
super(SPAN_NAME, null, CUSTOM_SERVICE_NAME);
}

propagateInstanaHeaders(originalArgs, span, suppressed = false) {
const params = originalArgs[0].input;
let clientContextContentBase64;
Expand Down Expand Up @@ -118,4 +122,4 @@ class InstanaAWSLambda extends InstanaAWSProduct {
}
}

module.exports = new InstanaAWSLambda(SPAN_NAME, null, CUSTOM_SERVICE_NAME);
module.exports = InstanaAWSLambda;
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ const operationsInfo = {
};

const operations = Object.keys(operationsInfo);

const SPAN_NAME = 's3';

class InstanaAWSS3 extends InstanaAWSProduct {
constructor() {
super(SPAN_NAME, operations);
}

instrumentedSmithySend(ctx, isActive, originalSend, smithySendArgs) {
if (cls.skipExitTracing({ isActive })) {
return originalSend.apply(ctx, smithySendArgs);
Expand Down Expand Up @@ -119,4 +122,4 @@ class InstanaAWSS3 extends InstanaAWSProduct {
}
}

module.exports = new InstanaAWSS3(SPAN_NAME, operations);
module.exports = InstanaAWSS3;
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ const { InstanaAWSProduct } = require('./instana_aws_product');
const { logTooManyAttributesWarningOnce } = require('../aws_utils');

const SPAN_NAME = 'sns';

let logger = require('../../../../../logger').getLogger('tracing/sns/v3', newLogger => {
logger = newLogger;
});

class InstanaAWSNS extends InstanaAWSProduct {
constructor() {
super(SPAN_NAME);
}

instrumentedSmithySend(ctx, isActive, originalSend, smithySendArgs) {
const skipTracingResult = cls.skipExitTracing({ extendedResponse: true, isActive });

Expand Down Expand Up @@ -130,4 +133,4 @@ class InstanaAWSNS extends InstanaAWSProduct {
}
}

module.exports = new InstanaAWSNS(SPAN_NAME);
module.exports = InstanaAWSNS;
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@ const operationsInfo = {
};

const operations = Object.keys(operationsInfo);

const SPAN_NAME = 'sqs';

class InstanaAWSSQS extends InstanaAWSProduct {
constructor() {
super(SPAN_NAME, operations);
}

init(hook, shimmer) {
// < 3.481.0
// refs https://github.com/instana/nodejs/commit/6ae90e74fee5c47cc4ade67d21c4885d34c08847
Expand Down Expand Up @@ -403,4 +406,4 @@ class InstanaAWSSQS extends InstanaAWSProduct {
}
}

module.exports = new InstanaAWSSQS(SPAN_NAME, operations);
module.exports = InstanaAWSSQS;

0 comments on commit 5752973

Please sign in to comment.