diff --git a/index.js b/index.js index f9ba0b1..034b891 100644 --- a/index.js +++ b/index.js @@ -4,17 +4,25 @@ const Promise = require('bluebird') const AWS = require('./lib/aws') const Swarm = require('./lib/swarm') +const logger = require('./lib/loggers').child({ module: 'models/main' }) +const log = logger.child({ method: 'main' }) const swarm = new Swarm() +log.info('Start') Promise.props({ docks: AWS.getDocks(), swarmHosts: swarm.getInfo() }) - .tap((data) => (AWS.sendBasicInfoToCloudWatch(data.docks, data.swarmHosts))) - .tap((data) => (AWS.sendThresholdDataToCloudWatch(data.swarmHosts))) - .tap((data) => (AWS.sendMaximumAvailableToCloudWatch(data.swarmHosts))) + .tap(data => log.trace({ data }, 'Done fetching docks and swarm hots')) + .tap(data => (AWS.sendBasicInfoToCloudWatch(data.docks, data.swarmHosts))) + .tap(data => log.trace({ data }, 'Done sending basic info to cloudwatch')) + .tap(data => (AWS.sendThresholdDataToCloudWatch(data.swarmHosts))) + .tap(data => log.trace({ data }, 'Done sending threshold data to cloudwatch')) + .tap(data => (AWS.sendMaximumAvailableToCloudWatch(data.swarmHosts))) + .tap(data => log.trace({ data }, 'Done sending max available to cloudwatch')) + .tap(() => log.info('Finished Succsefully')) .catch((err) => { - console.log(err.stack || err.message || err) + log.error({ err }, 'Error running script') process.exit(1) }) diff --git a/lib/aws.js b/lib/aws.js index 1957f67..ba29302 100644 --- a/lib/aws.js +++ b/lib/aws.js @@ -22,6 +22,7 @@ const assign = require('101/assign') const AWS = require('aws-sdk') const find = require('101/find') const Promise = require('bluebird') +const logger = require('./loggers').child({ module: 'models/aws' }) const promiseWhile = require('./utils').promiseWhile @@ -37,12 +38,15 @@ class AWSClass { } getDocks () { + const log = logger.child({ method: 'getDocks' }) + log.trace('Called') return Promise.resolve({ instances: [] }) .then(promiseWhile( (data) => (data.done), (data) => { const opts = assign({}, FILTER_PARAMS) if (data.NextToken) { opts.NextToken = data.NextToken } + log.trace({ opts }, 'Query opts for EC2 instances') return Promise.fromCallback((cb) => { this.ec2.describeInstances(opts, cb) }) @@ -62,6 +66,8 @@ class AWSClass { } sendMaximumAvailableToCloudWatch (swarmData) { + const log = logger.child({ method: 'sendMaximumAvailableToCloudWatch' }) + log.trace('Called') const orgInfo = swarmData.reduce((memo, curr) => { if (memo[curr.org]) { memo[curr.org].min = Math.min(curr.usedMemoryGiB, memo[curr.org].min) @@ -92,12 +98,10 @@ class AWSClass { } ] } - if (DRY_RUN) { - console.log(JSON.stringify(postData)) - } + log.debug({ postData }, 'DRY RUN: Data to be posted to cloudwatch') return Promise.fromCallback((cb) => { if (DRY_RUN) { - console.log('dry run. would be putting data') + log.trace('Dry Run: Will not post data') return cb() } this.cloudwatch.putMetricData(postData, cb) @@ -106,6 +110,8 @@ class AWSClass { } sendThresholdDataToCloudWatch (swarmData) { + const log = logger.child({ method: 'sendThresholdDataToCloudWatch' }) + log.trace('Called') const orgInfo = swarmData.reduce((memo, curr) => { if (memo[curr.org]) { memo[curr.org].available += curr.availableMemoryGiB @@ -171,12 +177,10 @@ class AWSClass { } ] } - if (DRY_RUN) { - console.log(JSON.stringify(postData)) - } + log.debug({ postData }, 'DRY RUN: Data to be posted to cloudwatch') return Promise.fromCallback((cb) => { if (DRY_RUN) { - console.log('dry run. would be putting data') + log.trace('Dry Run: Not running') return cb() } this.cloudwatch.putMetricData(postData, cb) @@ -185,17 +189,19 @@ class AWSClass { } sendBasicInfoToCloudWatch (awsData, swarmData) { + const log = logger.child({ method: 'sendBasicInfoToCloudWatch' }) + log.trace('Called') return Promise.each(swarmData, (swarmHostInfo) => { const awsDockInfo = find(awsData, (dock) => { return dock.PrivateIpAddress === swarmHostInfo.Host }) if (!awsDockInfo) { - console.error('could not find match:', swarmHostInfo.host) + log.error({ swarmHostInfo }, 'Could not find matching dock in AWS') return } const org = find(awsDockInfo.Tags, (t) => { return t.Key === 'org' }) if (!org) { - console.error('could not find org tag') + log.error({ swarmHostInfo, awsDockInfo }, 'Could not find `org` tag in EC2 instance tags') return } const orgID = org.Value @@ -224,12 +230,10 @@ class AWSClass { } ] } - if (DRY_RUN) { - console.log(JSON.stringify(postData)) - } + log.debug({ postData }, 'Data to be posted to cloudwatch') return Promise.fromCallback((cb) => { if (DRY_RUN) { - console.log('dry run. would be putting data') + log.trace('Dry Run: Will not post data') return cb() } this.cloudwatch.putMetricData(postData, cb) diff --git a/lib/loggers.js b/lib/loggers.js new file mode 100644 index 0000000..195d373 --- /dev/null +++ b/lib/loggers.js @@ -0,0 +1,27 @@ +'use strict' + +const bunyan = require('bunyan') + +/** + * Creates a new logger with the given name and custom serializers. + * + * @param {string} name - Name for the bunyan logger. + * @returns {bunyan} - A bunyan logger. + */ +function create (name) { + return bunyan.createLogger({ + name: 'furry-cactus', + streams: [ + { + level: process.env.LOG_LEVEL || 'INFO', + stream: process.stdout + } + ] + }) +} + +/** + * Bunyan logger + * @module furry-cactus:logger + */ +module.exports = create('furry-cactus', {}) diff --git a/lib/swarm.js b/lib/swarm.js index 37fadb9..e1fa029 100644 --- a/lib/swarm.js +++ b/lib/swarm.js @@ -6,6 +6,7 @@ const fs = require('fs') const join = require('path').join const Swarmerode = require('swarmerode') const Promise = require('bluebird') +const logger = require('./loggers').child({ module: 'models/swarm' }) const utils = require('./utils') @@ -35,6 +36,8 @@ class Swarm { } getInfo () { + const log = logger.child({ method: 'getInfo' }) + log.trace('Called') return Promise.fromCallback((cb) => { this._client.swarmInfo(cb) }) diff --git a/package.json b/package.json index a0b31b9..3fb5992 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "101": "^1.5.0", "aws-sdk": "^2.3.6", "bluebird": "^3.3.5", + "bunyan": "^1.8.10", "dockerode": "^2.2.10", "swarmerode": "^3.0.0" }, @@ -45,6 +46,6 @@ "mocha": "^2.5.3", "sinon": "^1.17.4", "sinon-as-promised": "^4.0.0", - "standard": "^6.0.8" + "standard": "^10.0.0" } } diff --git a/test/fixtures/swarm-info.js b/test/fixtures/swarm-info.js index 183810b..8777788 100644 --- a/test/fixtures/swarm-info.js +++ b/test/fixtures/swarm-info.js @@ -8,7 +8,7 @@ const SWARM_INFO = swarmInfoGenerator([{ Labels: 'org=2000' }]) -const reservedMemoryRegexp = /Reserved\ Memory/ +const reservedMemoryRegexp = /Reserved Memory/ for (var i = 0; i < SWARM_INFO.SystemStatus.length; ++i) { if (reservedMemoryRegexp.test(SWARM_INFO.SystemStatus[i][0])) {