Skip to content

Commit

Permalink
add "host" configuration to client-side monitoring configuration (#2736)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP authored Jun 21, 2019
1 parent 0e8c7af commit 202d3d2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "feature",
"category": "ClientSideMonitoring",
"description": "add a host config to client-side monitoring configuration, defaults to be '127.0.0.1'"
}
7 changes: 5 additions & 2 deletions lib/publisher/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function resolveMonitoringConfig() {
port: undefined,
clientId: undefined,
enabled: undefined,
host: undefined
};
if (fromEnvironment(config) || fromConfigFile(config)) return toJSType(config);
return toJSType(config);
Expand All @@ -27,7 +28,8 @@ function fromEnvironment(config) {
config.port = config.port || process.env.AWS_CSM_PORT;
config.enabled = config.enabled || process.env.AWS_CSM_ENABLED;
config.clientId = config.clientId || process.env.AWS_CSM_CLIENT_ID;
return config.port && config.enabled && config.clientId ||
config.host = config.host || process.env.AWS_CSM_HOST;
return config.port && config.enabled && config.clientId && config.host ||
['false', '0'].indexOf(config.enabled) >= 0; //no need to read shared config file if explicitely disabled
}

Expand All @@ -54,7 +56,8 @@ function fromConfigFile(config) {
config.port = config.port || sharedFileConfig.csm_port;
config.enabled = config.enabled || sharedFileConfig.csm_enabled;
config.clientId = config.clientId || sharedFileConfig.csm_client_id;
return config.port && config.enabled && config.clientId;
config.host = config.host || sharedFileConfig.csm_host;
return config.port && config.enabled && config.clientId && config.host;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/publisher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ function Publisher(options) {
this.enabled = options.enabled || false;
this.port = options.port || 31000;
this.clientId = options.clientId || '';
this.address = options.host || '127.0.0.1';
if (this.clientId.length > 255) {
// ClientId has a max length of 255
this.clientId = this.clientId.substr(0, 255);
}
this.messagesInFlight = 0;
this.address = 'localhost';
}

Publisher.prototype.fieldsToTrim = {
Expand Down
41 changes: 36 additions & 5 deletions test/publisher/configuration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ if (AWS.util.isNode()) {
expect(monitoringConfig()).to.eql({
enabled: true,
port: undefined,
clientId: undefined
clientId: undefined,
host: undefined
});
}
});
Expand All @@ -43,7 +44,8 @@ if (AWS.util.isNode()) {
expect(monitoringConfig()).to.eql({
enabled: false,
port: undefined,
clientId: undefined
clientId: undefined,
host: undefined
});
}
});
Expand All @@ -55,6 +57,7 @@ if (AWS.util.isNode()) {
enabled: false,
port: 31001,
clientId: undefined,
host: undefined
});
});

Expand All @@ -65,6 +68,18 @@ if (AWS.util.isNode()) {
enabled: false,
port: undefined,
clientId: 'client_id',
host: undefined
});
});

it('should get host', function() {
process.env.AWS_CSM_HOST = '127.0.0.2';
helpers.spyOn(AWS.util, 'readFileSync').andReturn('');
expect(monitoringConfig()).to.eql({
enabled: false,
port: undefined,
clientId: undefined,
host: '127.0.0.2'
});
});
});
Expand All @@ -79,7 +94,8 @@ if (AWS.util.isNode()) {
expect(monitoringConfig()).to.eql({
enabled: true,
port: undefined,
clientId: undefined
clientId: undefined,
host: undefined
});
}
});
Expand All @@ -91,7 +107,8 @@ if (AWS.util.isNode()) {
expect(monitoringConfig()).to.eql({
enabled: false,
port: undefined,
clientId: undefined
clientId: undefined,
host: undefined
});
}
});
Expand All @@ -103,7 +120,8 @@ if (AWS.util.isNode()) {
expect(monitoringConfig()).to.eql({
enabled: false,
port: undefined,
clientId: undefined
clientId: undefined,
host: undefined
});
});

Expand All @@ -113,6 +131,7 @@ if (AWS.util.isNode()) {
enabled: false,
port: 31001,
clientId: undefined,
host: undefined
});
});

Expand All @@ -122,6 +141,17 @@ if (AWS.util.isNode()) {
enabled: false,
port: undefined,
clientId: 'id',
host: undefined
});
});

it('should get host', function () {
helpers.spyOn(AWS.util, 'readFileSync').andReturn('[default]\ncsm_host=::1');
expect(monitoringConfig()).to.eql({
enabled: false,
port: undefined,
clientId: undefined,
host: '::1'
});
});
});
Expand All @@ -138,6 +168,7 @@ if (AWS.util.isNode()) {
enabled: true,
port: 54321,
clientId: 'clientid',
host: undefined
});
});

Expand Down
17 changes: 16 additions & 1 deletion test/publisher/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if (AWS.util.isNode()) {
});

expect(publisher.port).to.equal(1111);
expect(publisher.address).to.equal('127.0.0.1');
});

it('uses default port of 31000 if none was passed in', function () {
Expand All @@ -20,6 +21,20 @@ if (AWS.util.isNode()) {
expect(publisher.port).to.equal(31000);
});

it('uses "127.0.0.1" as default address', function() {
var publisher = new Publisher();

expect(publisher.address).to.equal('127.0.0.1');
});

it('setting "host" of the passed-in option will set the address', function() {
var publisher = new Publisher({
host: 'dnshost'
});

expect(publisher.address).to.equal('dnshost');
});

it('sets "clientId" to the passed in option', function () {
var publisher = new Publisher({
clientId: 'FooId'
Expand Down Expand Up @@ -55,7 +70,7 @@ if (AWS.util.isNode()) {
expect(publisher.enabled).to.equal(true);
});

it('uses default port of 31000 if none was passed in', function () {
it('uses set enabled as false if none was passed in', function () {
var publisher = new Publisher();

expect(publisher.enabled).to.equal(false);
Expand Down

0 comments on commit 202d3d2

Please sign in to comment.