Skip to content

Commit

Permalink
Register collector during checkin if unregistered (#52)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kkuzmin authored Apr 8, 2020
1 parent cffabab commit bcd3c85
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
38 changes: 31 additions & 7 deletions al_aws_collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class AlAwsCollector {
this._customHealthChecks = healthCheckFuns;
this._customStatsFuns = statsFuns;
this._collectorId = process.env.collector_id;
this._stackName = process.env.stack_name;
}

set context (context) {
Expand All @@ -134,6 +135,12 @@ class AlAwsCollector {
return this._invokeContext;
}

get registered () {
return this._collectorId != undefined &&
this._collectorId != '' &&
this._collectorId != 'none';
}

done(error) {
let context = this._invokeContext;
if (error) {
Expand Down Expand Up @@ -189,7 +196,8 @@ class AlAwsCollector {
functionName : this._name,
version : this._version,
dataType : this._ingestType,
collectorId : this._collectorId
collectorId : this._collectorId,
stackName : this._stackName
};
}

Expand Down Expand Up @@ -231,7 +239,8 @@ class AlAwsCollector {

register(event, custom, callback) {
let regValues = Object.assign(this.getProperties(), custom);
regValues.stackName = event.ResourceProperties.StackName;
regValues.stackName = event && event.ResourceProperties ?
event.ResourceProperties.StackName : regValues.stackName;

async.waterfall([
(asyncCallback) => {
Expand Down Expand Up @@ -281,7 +290,20 @@ class AlAwsCollector {

handleCheckin() {
var collector = this;
collector.checkin(function(err) {
async.waterfall([
function(asyncCallback) {
if (!collector.registered) {
collector.register(undefined, undefined, (err) => {
return asyncCallback(err);
});
} else {
return asyncCallback();
}
},
function(asyncCallback) {
return collector.checkin(asyncCallback);
}
], function(err) {
return collector.done(err);
});
}
Expand Down Expand Up @@ -326,10 +348,11 @@ class AlAwsCollector {
}

getHealthStatus(context, customChecks, callback) {
const appliedHealthChecks = customChecks.map(check => check.bind(this));
let collector = this;
const appliedHealthChecks = customChecks.map(check => check.bind(this));
async.parallel([
function(asyncCallback) {
m_healthChecks.checkCloudFormationStatus(process.env.stack_name, asyncCallback);
m_healthChecks.checkCloudFormationStatus(collector._stackName, asyncCallback);
}
].concat(appliedHealthChecks),
function(errMsg) {
Expand Down Expand Up @@ -386,7 +409,8 @@ class AlAwsCollector {
deregister(event, custom, callback) {
const context = this._invokeContext;
let regValues = Object.assign(this.getProperties(), custom);
regValues.stackName = event.ResourceProperties.StackName;
regValues.stackName = event && event.ResourceProperties ?
event.ResourceProperties.StackName : regValues.stackName;

this._azcollectc.deregister(regValues)
.then(resp => {
Expand All @@ -401,7 +425,7 @@ class AlAwsCollector {
sendStatus(status, callback) {
let collector = this;

if(!status){
if(!status || !collector.registered){
return callback(null);
} else {
zlib.deflate(JSON.stringify([status]), (compressionErr, compressed) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@alertlogic/al-aws-collector-js",
"version": "3.0.1",
"version": "3.0.2",
"license": "MIT",
"description": "Alert Logic AWS Collector Common Library",
"repository": {
Expand Down
4 changes: 3 additions & 1 deletion test/al_aws_collector_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,15 @@ describe('al_aws_collector tests', function() {
return callback(null, colMock.CF_DESCRIBE_STACKS_RESPONSE);
});
mockLambdaMetricStatistics();
colMock.initProcessEnv();
});

after(function() {
AWS.restore('CloudFormation', 'describeStacks');
AWS.restore('CloudWatch', 'getMetricStatistics');
});

it('checkin success', function(done) {
it('checkin success registered', function(done) {
var mockCtx = {
invokedFunctionArn : colMock.FUNCTION_ARN,
functionName : colMock.FUNCTION_NAME,
Expand All @@ -241,6 +242,7 @@ describe('al_aws_collector tests', function() {
done();
}
};

AlAwsCollector.load().then(function(creds) {
var collector = new AlAwsCollector(
mockCtx, 'cwe', AlAwsCollector.IngestTypes.SECMSGS,'1.0.0', creds, undefined, [], []);
Expand Down
3 changes: 3 additions & 0 deletions test/collector_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ const CHECKIN_AZCOLLECT_QUERY = {
details: [],
functionName: 'test-VpcFlowCollectLambdaFunction',
region: 'us-east-1',
stackName: 'test-stack-01',
version: '1.0.0',
status: 'ok',
statistics:[
Expand All @@ -138,6 +139,7 @@ const CHECKIN_AZCOLLECT_QUERY_CUSTOM_HEALTHCHECK_ERROR = {
dataType: 'secmsgs',
functionName: 'test-VpcFlowCollectLambdaFunction',
region: 'us-east-1',
stackName: 'test-stack-01',
version: '1.0.0',
status: 'error',
error_code: 'MYCODE',
Expand Down Expand Up @@ -181,6 +183,7 @@ const CHECKIN_ERROR_AZCOLLECT_QUERY = {
dataType: 'secmsgs',
functionName: 'test-VpcFlowCollectLambdaFunction',
region: 'us-east-1',
stackName: 'test-stack-01',
version: '1.0.0',
status: 'error',
error_code: 'ALAWS00002',
Expand Down

0 comments on commit bcd3c85

Please sign in to comment.