Skip to content

Commit

Permalink
Wrap the sendColector status function to send the status to DD metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakhi authored and Rakhi committed Jul 10, 2023
1 parent fb7055e commit 56253fa
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
50 changes: 50 additions & 0 deletions paws_collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,22 @@ class PawsCollector extends AlAwsCollector {
prepareErrorStatus(errorString, collectorType = this.pawsCollectorType) {
return super.setCollectorStatus(collectorType, errorString);
}
/**
* Override the super method to send the status to CloudWatch and DataDog metrics
* @param {*} stream
* @param {*} collectorStatus
* @param {*} callback
*/
sendCollectorStatus(stream, collectorStatus, callback) {
super.sendCollectorStatus(stream, collectorStatus, (err) => {
// report status to metrics if collector_status send the responce succesful.
if (!err) {
return this.reportCollectorStatus(collectorStatus.status, callback);
}
else return callback(err);
}
);
}

setPawsSecret(secretValue){
const encryptPromise = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -739,6 +755,40 @@ class PawsCollector extends AlAwsCollector {
this.reportDDMetric("duplicate_messages", duplicateCount);
return cloudwatch.putMetricData(params, callback);
};
/**
* Report the collector status(ok/error)to dd metrics
* @param {*} status
* @param {*} callback
* @returns
*/
reportCollectorStatus(status, callback) {
var cloudwatch = new AWS.CloudWatch({apiVersion: '2010-08-01'});
const params = {
MetricData: [
{
MetricName: 'PawsCollectorStatus',
Dimensions: [
{
Name: 'CollectorType',
Value: this._pawsCollectorType
},
{
Name: 'FunctionName',
Value: process.env.AWS_LAMBDA_FUNCTION_NAME
}
],
Timestamp: new Date(),
Unit: 'Count',
Value: 1
}
],
Namespace: 'PawsCollectors'
};

this.reportDDMetric("collector_status", 1, [`status:${status}`]);
return cloudwatch.putMetricData(params, callback);
};

_storeCollectionState(pawsState, privCollectorState, invocationTimeout, callback) {
if (Array.isArray(privCollectorState)) {
return this._storeCollectionStateArray(pawsState, privCollectorState, invocationTimeout, callback);
Expand Down
24 changes: 24 additions & 0 deletions test/paws_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,30 @@ describe('Unit Tests', function() {
});
});
});

it('reportCollectorStatus', function(done) {
let ctx = {
invokedFunctionArn : pawsMock.FUNCTION_ARN,
fail : function(error) {
assert.fail(error);
done();
},
succeed : function() {
done();
}
};
AWS.mock('CloudWatch', 'putMetricData', (params, callback) => callback());
TestCollector.load().then(function(creds) {
var collector = new TestCollector(ctx, creds);
const status = 'ok';
collector.reportCollectorStatus(status, function(error) {
assert.equal(null, error);
AWS.restore('KMS');
AWS.restore('CloudWatch');
done();
});
});
});
});

describe('Register Tests', function() {
Expand Down

0 comments on commit 56253fa

Please sign in to comment.