Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bunyan logging #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
32 changes: 18 additions & 14 deletions lib/aws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
})
Expand All @@ -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)
Expand Down Expand Up @@ -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')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dry run? dry run was removed here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just made these debug statements. So you just have to change the level. Will remove the DRY RUN stuff from the log message.

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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions lib/loggers.js
Original file line number Diff line number Diff line change
@@ -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', {})
3 changes: 3 additions & 0 deletions lib/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -35,6 +36,8 @@ class Swarm {
}

getInfo () {
const log = logger.child({ method: 'getInfo' })
log.trace('Called')
return Promise.fromCallback((cb) => {
this._client.swarmInfo(cb)
})
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/swarm-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down