From 3b86f0ecc046c4ced68a689a0812ee3f7e3ebe9f Mon Sep 17 00:00:00 2001 From: rakhimundhada15 <60738948+rakhimundhada15@users.noreply.github.com> Date: Thu, 22 Jun 2023 17:09:18 +0530 Subject: [PATCH] Send the status to collector_status service (#333) Co-authored-by: Rakhi --- package.json | 4 ++-- paws_collector.js | 16 ++++++------- test/paws_test.js | 60 ++++++++++++++++++++++++++++------------------- 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/package.json b/package.json index 4e361808..97f82762 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@alertlogic/paws-collector", - "version": "2.1.16", + "version": "2.1.17", "license": "MIT", "description": "Alert Logic AWS based API Poll Log Collector Library", "repository": { @@ -32,7 +32,7 @@ "yargs": "^17.6.2" }, "dependencies": { - "@alertlogic/al-aws-collector-js": "4.1.15", + "@alertlogic/al-aws-collector-js": "4.1.16", "async": "^3.2.4", "datadog-lambda-js": "^6.85.0", "debug": "^4.3.4", diff --git a/paws_collector.js b/paws_collector.js index f693cfdf..4164bd8b 100644 --- a/paws_collector.js +++ b/paws_collector.js @@ -187,9 +187,9 @@ class PawsCollector extends AlAwsCollector { // If stream exist in state then post the error status as part of stream specific ; // In check-in or self-update we don't get state ,so check collector_streams env variable and post error as part of paws_{applicationId}_status const streamType = pawsState && pawsState.priv_collector_state.stream ? - process.env.al_application_id + "_" + pawsState.priv_collector_state.stream : + pawsState.priv_collector_state.stream : process.env.al_application_id; - + super.done(error, streamType, sendStatus); } @@ -221,8 +221,8 @@ class PawsCollector extends AlAwsCollector { return Object.assign(pawsProps, baseProps); }; - prepareErrorStatus(errorString, streamName = 'none', collectorType = this.pawsCollectorType) { - return super.prepareErrorStatus(errorString, streamName, collectorType); + prepareErrorStatus(errorString, collectorType = this.pawsCollectorType) { + return super.setCollectorStatus(collectorType, errorString); } setPawsSecret(secretValue){ @@ -511,14 +511,14 @@ class PawsCollector extends AlAwsCollector { reportErrorStatus(error, pawsState, callback) { const streamType = pawsState && pawsState.priv_collector_state.stream ? - process.env.al_application_id + "_" + pawsState.priv_collector_state.stream : - process.env.al_application_id; + pawsState.priv_collector_state.stream : + process.env.al_application_id; const errorString = this.stringifyError(error); - const status = this.prepareErrorStatus(errorString, 'none', streamType); + const status = this.prepareErrorStatus(errorString, streamType); // Send the error status to assets only if retry count reached to 5. // To reduce the status fluctuation from healthy to unhealthy. if (pawsState.retry_count === MAX_ERROR_RETRIES) { - this.sendStatus(status, (sendError) => { + this.sendCollectorStatus(streamType, status, (sendError) => { return callback(sendError, errorString); }); } else { diff --git a/test/paws_test.js b/test/paws_test.js index 0e83b7ac..342ce371 100644 --- a/test/paws_test.js +++ b/test/paws_test.js @@ -24,16 +24,21 @@ function setAlServiceStub() { switch (path) { case '/residency/default/services/ingest/endpoint': ret = { - ingest : 'new-ingest-endpoint' - }; + ingest: 'new-ingest-endpoint' + }; + break; + case '/residency/default/services/azcollect/endpoint': + ret = { + azcollect: 'new-azcollect-endpoint' + }; + break; + case '/residency/default/services/collector_status/endpoint': + ret = { + azcollect: 'new-collector-status-endpoint' + }; + break; + default: break; - case '/residency/default/services/azcollect/endpoint': - ret = { - azcollect : 'new-azcollect-endpoint' - }; - break; - default: - break; } return resolve(ret); }); @@ -44,6 +49,12 @@ function setAlServiceStub() { return resolve(); }); }); + alserviceStub.put = sinon.stub(m_alCollector.AlServiceC.prototype, 'put').callsFake( + function fakeFn(path, extraOptions) { + return new Promise(function (resolve, reject) { + return resolve(); + }); + }); alserviceStub.del = sinon.stub(m_alCollector.AlServiceC.prototype, 'deleteRequest').callsFake( function fakeFn(path) { return new Promise(function(resolve, reject) { @@ -56,6 +67,7 @@ function restoreAlServiceStub() { alserviceStub.get.restore(); alserviceStub.post.restore(); alserviceStub.del.restore(); + alserviceStub.put.restore(); } @@ -691,7 +703,7 @@ describe('Unit Tests', function() { }); }); - it('Check sendStatus method call only after Five failed attempt', function (done) { + it('Check sendCollectorStatus method call only after Five failed attempt', function (done) { mockDDB(); let ctx = { invokedFunctionArn: pawsMock.FUNCTION_ARN, @@ -700,15 +712,15 @@ describe('Unit Tests', function() { done(); }, succeed: function () { - sinon.assert.callCount(mockSendStatus, 1); + sinon.assert.callCount(mockSendCollectorStatus, 1); sinon.assert.calledOnce(mockPawsGetLogs); mockPawsGetLogs.restore(); - mockSendStatus.restore(); + mockSendCollectorStatus.restore(); done(); } }; - let mockSendStatus = sinon.stub(m_al_aws.AlAwsCollector.prototype, 'sendStatus').callsFake( - function fakeFn(status, callback) { + let mockSendCollectorStatus = sinon.stub(m_al_aws.AlAwsCollector.prototype, 'sendCollectorStatus').callsFake( + function fakeFn(stream,status, callback) { return callback(null); }); @@ -732,7 +744,7 @@ describe('Unit Tests', function() { }); }); - it('Check sendStatus method not call if failed attempt less < 5', function (done) { + it('Check sendCollectorStatus method not call if failed attempt less < 5', function (done) { mockDDB(); let ctx = { invokedFunctionArn: pawsMock.FUNCTION_ARN, @@ -741,15 +753,15 @@ describe('Unit Tests', function() { done(); }, succeed: function () { - sinon.assert.callCount(mockSendStatus, 0); + sinon.assert.callCount(mockSendCollectorStatus, 0); sinon.assert.calledOnce(mockPawsGetLogs); mockPawsGetLogs.restore(); - mockSendStatus.restore(); + mockSendCollectorStatus.restore(); done(); } }; - let mockSendStatus = sinon.stub(m_al_aws.AlAwsCollector.prototype, 'sendStatus').callsFake( - function fakeFn(status, callback) { + let mockSendCollectorStatus = sinon.stub(m_al_aws.AlAwsCollector.prototype, 'sendCollectorStatus').callsFake( + function fakeFn(stream,status, callback) { return callback(null); }); @@ -773,7 +785,7 @@ describe('Unit Tests', function() { }); }); - it('Check if retry_count get added in state for existing collector and it will not call sendStatus method', function (done) { + it('Check if retry_count get added in state for existing collector and it will not call mockSendCollectorStatus method', function (done) { mockDDB(); let ctx = { invokedFunctionArn: pawsMock.FUNCTION_ARN, @@ -782,15 +794,15 @@ describe('Unit Tests', function() { done(); }, succeed: function () { - sinon.assert.callCount(mockSendStatus, 0); + sinon.assert.callCount(mockSendCollectorStatus, 0); sinon.assert.calledOnce(mockPawsGetLogs); mockPawsGetLogs.restore(); - mockSendStatus.restore(); + mockSendCollectorStatus.restore(); done(); } }; - let mockSendStatus = sinon.stub(m_al_aws.AlAwsCollector.prototype, 'sendStatus').callsFake( - function fakeFn(status, callback) { + let mockSendCollectorStatus = sinon.stub(m_al_aws.AlAwsCollector.prototype, 'sendCollectorStatus').callsFake( + function fakeFn(stream, status, callback) { return callback(null); });