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: disabling aws sdk instrumentation #866

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 13 additions & 5 deletions packages/core/src/tracing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const instrumentations = [
* @property {Function} [updateConfig]
* @property {boolean} [batchable]
* @property {string} [spanName]
* @property {string} [instrumentationName]
*/

/**
Expand Down Expand Up @@ -182,8 +183,14 @@ function initInstrumenations(_config) {
if (!instrumenationsInitialized) {
instrumentations.forEach(instrumentationKey => {
instrumentationModules[instrumentationKey] = require(instrumentationKey);
const instrumentationName = instrumentationKey.match(/.\/instrumentation\/[^/]*\/(.*)/)[1];
const isInstrumentationDisabled = _config.tracing.disabledTracers.includes(instrumentationName.toLowerCase());

const extractedInstrumentationName = instrumentationKey.match(/.\/instrumentation\/[^/]*\/(.*)/)[1];

const isInstrumentationDisabled =
_config.tracing.disabledTracers.includes(extractedInstrumentationName.toLowerCase()) ||
(instrumentationModules[instrumentationKey].instrumentationName &&
_config.tracing.disabledTracers.includes(instrumentationModules[instrumentationKey].instrumentationName));

kirrg001 marked this conversation as resolved.
Show resolved Hide resolved
if (!isInstrumentationDisabled) {
instrumentationModules[instrumentationKey].init(_config);
}
Expand Down Expand Up @@ -216,11 +223,12 @@ exports.activate = function activate(extraConfig = {}) {

if (automaticTracingEnabled) {
instrumentations.forEach(instrumentationKey => {
const instrumentationName = /.\/instrumentation\/[^/]*\/(.*)/.exec(instrumentationKey)[1];
const extractedInstrumentationName = /.\/instrumentation\/[^/]*\/(.*)/.exec(instrumentationKey)[1];

const isDisabled =
config.tracing.disabledTracers.findIndex(disabledKey => instrumentationName.toLowerCase() === disabledKey) !==
-1;
config.tracing.disabledTracers.includes(extractedInstrumentationName.toLowerCase()) ||
(instrumentationModules[instrumentationKey].instrumentationName &&
config.tracing.disabledTracers.includes(instrumentationModules[instrumentationKey].instrumentationName));

if (!isDisabled) {
instrumentationModules[instrumentationKey].activate(extraConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ awsProducts.forEach(awsProduct => {

let isActive = false;

exports.instrumentationName = 'aws-sdk/v2';

exports.isActive = function () {
return isActive;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ awsProducts.forEach(awsProduct => {

let isActive = false;

exports.instrumentationName = 'aws-sdk/v3';

exports.init = function init() {
sqsConsumer.init();

Expand Down
22 changes: 20 additions & 2 deletions packages/core/test/tracing/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const kafkaJs = require('../../src/tracing/instrumentation/messaging/kafkaJs');
const rdKafka = require('../../src/tracing/instrumentation/messaging/rdkafka');
const grpc = require('../../src/tracing/instrumentation/protocols/grpc');
const grpcJs = require('../../src/tracing/instrumentation/protocols/grpcJs');
const awsSdkv2 = require('../../src/tracing/instrumentation/cloud/aws-sdk/v2/index');
const testConfig = require('../config');

const expect = chai.expect;
Expand All @@ -31,18 +32,24 @@ mochaSuiteFn('[UNIT] tracing/index', function () {
let activateStubGrpcJs;
let activateStubKafkaJs;
let activateStubRdKafka;
let activateAwsSdkv2;

let initStubGrpc;
let initStubGrpcJs;
let initStubKafkaJs;
let initAwsSdkv2;

before(() => {
activateStubGrpc = sinon.stub(grpc, 'activate');
activateStubGrpcJs = sinon.stub(grpcJs, 'activate');
activateStubKafkaJs = sinon.stub(kafkaJs, 'activate');
activateStubRdKafka = sinon.stub(rdKafka, 'activate');
activateAwsSdkv2 = sinon.stub(awsSdkv2, 'activate');

initStubGrpc = sinon.stub(grpc, 'init');
initStubGrpcJs = sinon.stub(grpcJs, 'init');
initStubKafkaJs = sinon.stub(kafkaJs, 'init');
initAwsSdkv2 = sinon.stub(awsSdkv2, 'init');
});

beforeEach(() => {
Expand All @@ -56,9 +63,13 @@ mochaSuiteFn('[UNIT] tracing/index', function () {
activateStubGrpcJs.reset();
activateStubKafkaJs.reset();
activateStubRdKafka.reset();
activateAwsSdkv2.reset();

initStubGrpc.reset();
initStubGrpcJs.reset();
initStubKafkaJs.reset();
initAwsSdkv2.reset();

delete process.env.INSTANA_DISABLED_TRACERS;
});

Expand All @@ -67,21 +78,27 @@ mochaSuiteFn('[UNIT] tracing/index', function () {
});

it('deactivate instrumentation via config', () => {
initAndActivate({ tracing: { disabledTracers: ['grpc', 'kafkajs'] } });
initAndActivate({ tracing: { disabledTracers: ['grpc', 'kafkajs', 'aws-sdk/v2/index'] } });
expect(activateStubGrpc).to.not.have.been.called;
expect(activateStubGrpcJs).to.have.been.called;
expect(activateStubKafkaJs).to.not.have.been.called;
expect(activateStubRdKafka).to.have.been.called;
expect(initStubGrpc).not.to.have.been.called;

expect(initAwsSdkv2).not.to.have.been.called;
expect(activateAwsSdkv2).not.to.have.been.called;
kirrg001 marked this conversation as resolved.
Show resolved Hide resolved
});

it('deactivate instrumentation via env', () => {
process.env.INSTANA_DISABLED_TRACERS = 'grpc';
initAndActivate({});

expect(activateStubGrpc).to.not.have.been.called;
expect(activateStubGrpcJs).to.have.been.called;
expect(activateStubKafkaJs).to.have.been.called;
expect(activateStubRdKafka).to.have.been.called;
expect(activateAwsSdkv2).to.have.been.called;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: We could use the same pairs of init and activate checks as in the test case 'deactivate instrumentation via config' here. On the other hand, the test setup only deactivates grpc, so to be honest, I do not remember why we check for all the other instrumentations here as well 🤔 I think just having expectations for grpc and maybe grpcjs for their respective init and activate methods would be enough?


expect(initStubGrpc).not.to.have.been.called;
});

Expand All @@ -100,9 +117,10 @@ mochaSuiteFn('[UNIT] tracing/index', function () {
});

it('skip init step when instrumentation disabled', () => {
initAndActivate({ tracing: { disabledTracers: ['grpcjs', 'kafkajs'] } });
initAndActivate({ tracing: { disabledTracers: ['grpcjs', 'kafkajs', 'aws-sdk/v2'] } });
expect(initStubKafkaJs).not.to.have.been.called;
expect(initStubGrpcJs).not.to.have.been.called;
expect(initAwsSdkv2).to.not.have.been.called;
expect(initStubGrpc).to.have.been.called;
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though: We could merge this into 'deactivate instrumentation via config' instead of having a separate test case, see above.


Expand Down