Skip to content

Instantly share code, notes, and snippets.

@eddywashere
Created May 1, 2017 17:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddywashere/4d42172cc9545a86fca49bc4dd1e44b7 to your computer and use it in GitHub Desktop.
Save eddywashere/4d42172cc9545a86fca49bc4dd1e44b7 to your computer and use it in GitHub Desktop.
get cloudwatch logs
const _ = require('lodash');
const AWS = require('aws-sdk');
const retry = require('bluebird-retry');
function recursiveReq(request, params, key, items = [], count = 0) {
return request(params).then(data => {
const newItems = items.concat(data[key]);
const lastItem = _.last(newItems);
const nextToken = _.get(data, 'nextToken', null);
if (data.events.length !== 0) {
let lastDate;
// update lastDate
data.events.forEach(event => {
let parsedTimestamp = new Date(event.timestamp);
if (lastDate === undefined || lastDate < parsedTimestamp) {
lastDate = parsedTimestamp;
}
});
params.startTime = lastDate.getTime();
}
if (nextToken) {
const newParams = _.merge({}, params, {
nextToken,
});
return recursiveReq(request, newParams, key, newItems, ++count);
}
console.info(
`[recursiveReq] Key: ${key}, Items: ${newItems.length}, Count: ${count}`
);
return {events: newItems, lastItem, nextToken};
});
}
const retryConfig = {
interval: 1000,
backoff: 2,
throw_original: true,
max_tries: 5,
max_interval: 10000,
};
const cloudwatchlogs = ({
start = new Date().getTime() - 60 * 1000 * 120,
end = new Date().getTime(),
nextToken,
name,
filter,
limit = 100,
region = 'us-west-2',
}) => {
let params = {
logGroupName: name,
limit,
nextToken,
filterPattern: filter,
// interleaved: true, // mix multiple log streams
};
if (start) {
params.startTime = start;
}
if (end) {
params.endTime = end;
}
const options = {};
options.region = region;
const log = new AWS.CloudWatchLogs(options);
const getLogs = params => {
return log.filterLogEvents(params).promise().catch(err => {
console.error('[CloudWatchLogs] Error fetching logs', err);
return Promise.reject(err);
});
};
return recursiveReq(getLogs, params, 'events');
};
module.exports = cloudwatchlogs;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment