Skip to content

Commit

Permalink
US72282 - Move common libraries into this repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ikemsley committed Jan 17, 2018
1 parent f60c45b commit 21abaa2
Show file tree
Hide file tree
Showing 11 changed files with 935 additions and 1 deletion.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package-lock.json
node_modules/
test/report.xml
#idea
.idea/
nbproject/
.nyc_output/
al-cwe-collector.zip
coverage/
test/ide_test.js
.env
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 6
}
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sudo: false
language: node_js
node_js:
- "6"
install:
- make deps
script:
- make test
20 changes: 20 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.PHONY: test cfn

all: test

deps: node_modules

node_modules:
npm install

compile: deps
npm run lint

test: compile
npm run test

clean:
rm -rf node_modules
rm -f package-lock.json
rm -f test/report.xml
rm -rf ./coverage/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ For example:
"moment": "^2.19.2",
"request": "*",
"request-promise-native": "*",
"al-collector-js": "@alertlogic/al-collector-js"
"al-collector-js": "git://github.com/alertlogic/al-collector-js#master"
},
"author": "Alert Logic Inc."
}
Expand Down
106 changes: 106 additions & 0 deletions al_aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* -----------------------------------------------------------------------------
* @copyright (C) 2017, Alert Logic, Inc
* @doc
*
* Helper class for lambda function utility and helper methods.
*
* @end
* -----------------------------------------------------------------------------
*/

const AWS = require('aws-sdk');
const moment = require('moment');
const async = require('async');

const AWS_STATISTICS_PERIOD_MINUTES = 15;
const MAX_ERROR_MSG_LEN = 1024;

var selfUpdate = function (callback) {
var params = {
FunctionName: process.env.AWS_LAMBDA_FUNCTION_NAME,
S3Bucket: process.env.aws_lambda_s3_bucket,
S3Key: process.env.aws_lambda_zipfile_name
};
var lambda = new AWS.Lambda();
console.info('Performing lambda self-update with params: ', JSON.stringify(params));
lambda.updateFunctionCode(params, function(err, data) {
if (err) {
console.info('Lambda self-update error: ', err);
} else {
console.info('Lambda self-update successful. Data: ' + data);
}
return callback(err);
});
};

var getMetricStatistics = function (params, statistics, callback) {
var cloudwatch = new AWS.CloudWatch({apiVersion: '2010-08-01'});
cloudwatch.getMetricStatistics(params, function(err, data) {
if (err) {
statistics.push({
Label: params.MetricName,
StatisticsError: JSON.stringify(err).slice(0, MAX_ERROR_MSG_LEN)
});
} else {
statistics.push({
Label: data.Label,
Datapoints: data.Datapoints
});
}
return callback(null, statistics);
});
};

var getLambdaMetrics = function (functionName, metricName, statistics, callback) {
var params = {
Dimensions: [
{
Name: 'FunctionName',
Value: functionName
}
],
MetricName: metricName,
Namespace: 'AWS/Lambda',
Statistics: ['Sum'],
StartTime: moment().subtract(AWS_STATISTICS_PERIOD_MINUTES, 'minutes').toISOString(),
EndTime: new Date(),
Period: 60*AWS_STATISTICS_PERIOD_MINUTES /* 15 mins as seconds */
};
return getMetricStatistics(params, statistics, callback);
};

var getKinesisMetrics = function (streamName, metricName, statistics, callback) {
var params = {
Dimensions: [
{
Name: 'StreamName',
Value: streamName
}
],
MetricName: metricName,
Namespace: 'AWS/Kinesis',
Statistics: ['Sum'],
StartTime: moment().subtract(AWS_STATISTICS_PERIOD_MINUTES, 'minutes').toISOString(),
EndTime: new Date(),
Period: 60*AWS_STATISTICS_PERIOD_MINUTES /* 15 mins as seconds */
};
return getMetricStatistics(params, statistics, callback);
};

var arnToName = function (arn) {
const parsedArn = arn.split(':');
if (parsedArn.length > 3) {
const parsedId = parsedArn[parsedArn.length-1].split('/');
return parsedId[parsedId.length-1];
} else {
return undefined;
}
};

module.exports = {
getMetricStatistics : getMetricStatistics,
getLambdaMetrics : getLambdaMetrics,
getKinesisMetrics : getKinesisMetrics,
selfUpdate : selfUpdate,
arnToName : arnToName
};
Loading

0 comments on commit 21abaa2

Please sign in to comment.