diff --git a/.changes/next-release/feature-client side monitoring-f83de457.json b/.changes/next-release/feature-client side monitoring-f83de457.json new file mode 100644 index 0000000000..3140d7b0de --- /dev/null +++ b/.changes/next-release/feature-client side monitoring-f83de457.json @@ -0,0 +1,5 @@ +{ + "type": "feature", + "category": "client side monitoring", + "description": "CSM now can be enabled globally only from environment without code change" +} \ No newline at end of file diff --git a/lib/service.js b/lib/service.js index 24e2de7b3c..2463910ad6 100644 --- a/lib/service.js +++ b/lib/service.js @@ -57,7 +57,7 @@ AWS.Service = inherit({ //enable attaching listeners to service client AWS.SequentialExecutor.call(this); AWS.Service.addDefaultMonitoringListeners(this); - if (this.config.clientSideMonitoring && this.publisher) { + if ((this.config.clientSideMonitoring || AWS.Service._clientSideMonitoring) && this.publisher) { var publisher = this.publisher; this.addNamedListener('PUBLISH_API_CALL', 'apiCall', function PUBLISH_API_CALL(event) { process.nextTick(function() {publisher.eventHandler(event);}); @@ -709,7 +709,12 @@ AWS.util.update(AWS.Service, { if (!this.prototype.publisher && AWS.util.clientSideMonitoring) { var Publisher = AWS.util.clientSideMonitoring.Publisher; var configProvider = AWS.util.clientSideMonitoring.configProvider; - this.prototype.publisher = new Publisher(configProvider()); + var publisherConfig = configProvider(); + this.prototype.publisher = new Publisher(publisherConfig); + if (publisherConfig.enabled) { + //if csm is enabled in environment, SDK should send all metrics + AWS.Service._clientSideMonitoring = true; + } } AWS.SequentialExecutor.call(svc.prototype); AWS.Service.addDefaultMonitoringListeners(svc.prototype); diff --git a/test/publisher/configuration.spec.js b/test/publisher/configuration.spec.js index 1a0f76a6c6..03f95168bd 100644 --- a/test/publisher/configuration.spec.js +++ b/test/publisher/configuration.spec.js @@ -1,6 +1,7 @@ var helpers = require('../helpers'); var AWS = helpers.AWS; var spyOn = helpers.spyOn; +var MockService = helpers.MockService; var monitoringConfig = require('../../lib/publisher/configuration'); var iniLoader = AWS.util.iniLoader; @@ -151,6 +152,42 @@ if (AWS.util.isNode()) { expect(monitoringConfig().enabled).to.equal(false); expect(ReadFileCalled).to.equal(0) }) - }) + }); + + it('should enable client-side monitoring globally if corresponding environment is set', function(done) { + process.env.AWS_CSM_ENABLED = "true"; + AWS.Service.prototype.publisher = undefined; + AWS.Service.defineService('acm', ['2015-12-08']); + expect(AWS.Service.prototype.publisher).not.equal(undefined); + var publisherInvoked = false + AWS.Service.prototype.publisher = { + eventHandler: function(event){ + if (!publisherInvoked) { + publisherInvoked = true; + done() //make sure publisher is invoked + } + } + } + var client = new MockService({}); + client.makeRequest('operationName', function(err, data) {}); + }); + + it('should enable client-side monitoring globally if corresponding config is set', function(done) { + helpers.spyOn(AWS.util, 'readFileSync').andReturn('[default]\ncsm_enabled=true'); + AWS.Service.prototype.publisher = undefined; + AWS.Service.defineService('acm', ['2015-12-08']); + expect(AWS.Service.prototype.publisher).not.equal(undefined); + var publisherInvoked = false + AWS.Service.prototype.publisher = { + eventHandler: function(event){ + if (!publisherInvoked) { + publisherInvoked = true; + done() //make sure publisher is invoked + } + } + } + var client = new MockService({}); + client.makeRequest('operationName', function(err, data) {}); + }); }) } diff --git a/test/publisher/functional_test/runner.spec.js b/test/publisher/functional_test/runner.spec.js index d37088eaad..1e331e44fe 100644 --- a/test/publisher/functional_test/runner.spec.js +++ b/test/publisher/functional_test/runner.spec.js @@ -14,7 +14,6 @@ describe('run functional test', () => { let defaultConfiguration; let processEnvBackup; before(() => { - AWS.config.clientSideMonitoring = true; processEnvBackup = Object.assign({}, process.env); }); @@ -25,6 +24,7 @@ describe('run functional test', () => { afterEach(async () => { process.env = processEnvBackup; + AWS.Service.prototype.publisher = undefined; }); for (const scenario of schemes.cases) { @@ -40,7 +40,6 @@ describe('run functional test', () => { process.env = Object.assign({}, process.env, scenarioConfiguration.environmentVariables); const resolvedCSMConfig = csmConfigProvider(); - AWS.Service.prototype.publisher = new Publisher(resolvedCSMConfig); //start a subprocess to echo back the udp datagrams let fakeAgent = await agentStart(resolvedCSMConfig.port || 31000); diff --git a/test/service.spec.js b/test/service.spec.js index 62f014eafa..50f7b995ab 100644 --- a/test/service.spec.js +++ b/test/service.spec.js @@ -988,8 +988,8 @@ expect(events[0].Type).to.equal('ApiCallAttempt'); expect(events[1].Type).to.equal('ApiCall'); done(); - }) - }) + }); + }); }); }).call(this);