Skip to content

Commit

Permalink
tweak CSM config to be able to enabled by only environment or config (#…
Browse files Browse the repository at this point in the history
…2480)

* tweak CSM config to be able to enabled by only environment or config

* fix funtional test

* move unit test to better place to live
  • Loading branch information
AllanZhengYP authored Jan 11, 2019
1 parent 3e10a5e commit a515a3c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "feature",
"category": "client side monitoring",
"description": "CSM now can be enabled globally only from environment without code change"
}
9 changes: 7 additions & 2 deletions lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);});
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 38 additions & 1 deletion test/publisher/configuration.spec.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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) {});
});
})
}
3 changes: 1 addition & 2 deletions test/publisher/functional_test/runner.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe('run functional test', () => {
let defaultConfiguration;
let processEnvBackup;
before(() => {
AWS.config.clientSideMonitoring = true;
processEnvBackup = Object.assign({}, process.env);
});

Expand All @@ -25,6 +24,7 @@ describe('run functional test', () => {

afterEach(async () => {
process.env = processEnvBackup;
AWS.Service.prototype.publisher = undefined;
});

for (const scenario of schemes.cases) {
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/service.spec.js

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

0 comments on commit a515a3c

Please sign in to comment.