From 3304478c49de72d0e888150d5566eadcd6ec6298 Mon Sep 17 00:00:00 2001 From: thejsj Date: Tue, 6 Jun 2017 18:03:46 -0700 Subject: [PATCH 1/4] Add better logging. Remove all console.logs --- index.js | 16 ++++++++++++---- lib/aws.js | 32 ++++++++++++++++++-------------- lib/loggers.js | 27 +++++++++++++++++++++++++++ lib/swarm.js | 3 +++ package.json | 1 + 5 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 lib/loggers.js diff --git a/index.js b/index.js index f9ba0b1..67a3348 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('./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')) .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..c33bf8b 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..7e721c5 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" }, From f614014d162a57f5fe7e001d84fe9b486c8294df Mon Sep 17 00:00:00 2001 From: thejsj Date: Tue, 6 Jun 2017 18:09:43 -0700 Subject: [PATCH 2/4] Fix standard issues --- index.js | 1 - lib/aws.js | 2 +- package.json | 2 +- test/fixtures/swarm-info.js | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 67a3348..7835884 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,6 @@ const Swarm = require('./lib/swarm') const logger = require('./loggers').child({ module: 'models/main' }) const log = logger.child({ method: 'main' }) - const swarm = new Swarm() log.info('Start') diff --git a/lib/aws.js b/lib/aws.js index c33bf8b..ba29302 100644 --- a/lib/aws.js +++ b/lib/aws.js @@ -201,7 +201,7 @@ class AWSClass { } const org = find(awsDockInfo.Tags, (t) => { return t.Key === 'org' }) if (!org) { - log.error({ swarmHostInfo, awsDockInfo }, 'Could not find `org` tag in EC2 instance tags') + log.error({ swarmHostInfo, awsDockInfo }, 'Could not find `org` tag in EC2 instance tags') return } const orgID = org.Value diff --git a/package.json b/package.json index 7e721c5..3fb5992 100644 --- a/package.json +++ b/package.json @@ -46,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])) { From 5013211d4b6486053e8c33238117cae307b3f1ab Mon Sep 17 00:00:00 2001 From: thejsj Date: Tue, 6 Jun 2017 18:20:12 -0700 Subject: [PATCH 3/4] Fix reference --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 7835884..a612b00 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ const Promise = require('bluebird') const AWS = require('./lib/aws') const Swarm = require('./lib/swarm') -const logger = require('./loggers').child({ module: 'models/main' }) +const logger = require('./lib/loggers').child({ module: 'models/main' }) const log = logger.child({ method: 'main' }) const swarm = new Swarm() From 30d0643b2f54e7ad967ff12dd5d99e9f531b7965 Mon Sep 17 00:00:00 2001 From: thejsj Date: Tue, 6 Jun 2017 18:34:04 -0700 Subject: [PATCH 4/4] Add logs for finished succesfully --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index a612b00..034b891 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,7 @@ Promise.props({ .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) => { log.error({ err }, 'Error running script') process.exit(1)