Skip to content

Commit bcd3c85

Browse files
author
kkuzmin
authored
Register collector during checkin if unregistered (#52)
* Register collector during checkin if unregistered Register collector during checkin Add none as unregistered Fix checkin Do ot send status for unregistred collectors Add debug Fix registered Fix register Fix initial register * Fix tests
1 parent cffabab commit bcd3c85

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

al_aws_collector.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class AlAwsCollector {
125125
this._customHealthChecks = healthCheckFuns;
126126
this._customStatsFuns = statsFuns;
127127
this._collectorId = process.env.collector_id;
128+
this._stackName = process.env.stack_name;
128129
}
129130

130131
set context (context) {
@@ -134,6 +135,12 @@ class AlAwsCollector {
134135
return this._invokeContext;
135136
}
136137

138+
get registered () {
139+
return this._collectorId != undefined &&
140+
this._collectorId != '' &&
141+
this._collectorId != 'none';
142+
}
143+
137144
done(error) {
138145
let context = this._invokeContext;
139146
if (error) {
@@ -189,7 +196,8 @@ class AlAwsCollector {
189196
functionName : this._name,
190197
version : this._version,
191198
dataType : this._ingestType,
192-
collectorId : this._collectorId
199+
collectorId : this._collectorId,
200+
stackName : this._stackName
193201
};
194202
}
195203

@@ -231,7 +239,8 @@ class AlAwsCollector {
231239

232240
register(event, custom, callback) {
233241
let regValues = Object.assign(this.getProperties(), custom);
234-
regValues.stackName = event.ResourceProperties.StackName;
242+
regValues.stackName = event && event.ResourceProperties ?
243+
event.ResourceProperties.StackName : regValues.stackName;
235244

236245
async.waterfall([
237246
(asyncCallback) => {
@@ -281,7 +290,20 @@ class AlAwsCollector {
281290

282291
handleCheckin() {
283292
var collector = this;
284-
collector.checkin(function(err) {
293+
async.waterfall([
294+
function(asyncCallback) {
295+
if (!collector.registered) {
296+
collector.register(undefined, undefined, (err) => {
297+
return asyncCallback(err);
298+
});
299+
} else {
300+
return asyncCallback();
301+
}
302+
},
303+
function(asyncCallback) {
304+
return collector.checkin(asyncCallback);
305+
}
306+
], function(err) {
285307
return collector.done(err);
286308
});
287309
}
@@ -326,10 +348,11 @@ class AlAwsCollector {
326348
}
327349

328350
getHealthStatus(context, customChecks, callback) {
329-
const appliedHealthChecks = customChecks.map(check => check.bind(this));
351+
let collector = this;
352+
const appliedHealthChecks = customChecks.map(check => check.bind(this));
330353
async.parallel([
331354
function(asyncCallback) {
332-
m_healthChecks.checkCloudFormationStatus(process.env.stack_name, asyncCallback);
355+
m_healthChecks.checkCloudFormationStatus(collector._stackName, asyncCallback);
333356
}
334357
].concat(appliedHealthChecks),
335358
function(errMsg) {
@@ -386,7 +409,8 @@ class AlAwsCollector {
386409
deregister(event, custom, callback) {
387410
const context = this._invokeContext;
388411
let regValues = Object.assign(this.getProperties(), custom);
389-
regValues.stackName = event.ResourceProperties.StackName;
412+
regValues.stackName = event && event.ResourceProperties ?
413+
event.ResourceProperties.StackName : regValues.stackName;
390414

391415
this._azcollectc.deregister(regValues)
392416
.then(resp => {
@@ -401,7 +425,7 @@ class AlAwsCollector {
401425
sendStatus(status, callback) {
402426
let collector = this;
403427

404-
if(!status){
428+
if(!status || !collector.registered){
405429
return callback(null);
406430
} else {
407431
zlib.deflate(JSON.stringify([status]), (compressionErr, compressed) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alertlogic/al-aws-collector-js",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"license": "MIT",
55
"description": "Alert Logic AWS Collector Common Library",
66
"repository": {

test/al_aws_collector_test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,15 @@ describe('al_aws_collector tests', function() {
222222
return callback(null, colMock.CF_DESCRIBE_STACKS_RESPONSE);
223223
});
224224
mockLambdaMetricStatistics();
225+
colMock.initProcessEnv();
225226
});
226227

227228
after(function() {
228229
AWS.restore('CloudFormation', 'describeStacks');
229230
AWS.restore('CloudWatch', 'getMetricStatistics');
230231
});
231232

232-
it('checkin success', function(done) {
233+
it('checkin success registered', function(done) {
233234
var mockCtx = {
234235
invokedFunctionArn : colMock.FUNCTION_ARN,
235236
functionName : colMock.FUNCTION_NAME,
@@ -241,6 +242,7 @@ describe('al_aws_collector tests', function() {
241242
done();
242243
}
243244
};
245+
244246
AlAwsCollector.load().then(function(creds) {
245247
var collector = new AlAwsCollector(
246248
mockCtx, 'cwe', AlAwsCollector.IngestTypes.SECMSGS,'1.0.0', creds, undefined, [], []);

test/collector_mock.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ const CHECKIN_AZCOLLECT_QUERY = {
122122
details: [],
123123
functionName: 'test-VpcFlowCollectLambdaFunction',
124124
region: 'us-east-1',
125+
stackName: 'test-stack-01',
125126
version: '1.0.0',
126127
status: 'ok',
127128
statistics:[
@@ -138,6 +139,7 @@ const CHECKIN_AZCOLLECT_QUERY_CUSTOM_HEALTHCHECK_ERROR = {
138139
dataType: 'secmsgs',
139140
functionName: 'test-VpcFlowCollectLambdaFunction',
140141
region: 'us-east-1',
142+
stackName: 'test-stack-01',
141143
version: '1.0.0',
142144
status: 'error',
143145
error_code: 'MYCODE',
@@ -181,6 +183,7 @@ const CHECKIN_ERROR_AZCOLLECT_QUERY = {
181183
dataType: 'secmsgs',
182184
functionName: 'test-VpcFlowCollectLambdaFunction',
183185
region: 'us-east-1',
186+
stackName: 'test-stack-01',
184187
version: '1.0.0',
185188
status: 'error',
186189
error_code: 'ALAWS00002',

0 commit comments

Comments
 (0)