Skip to content
This repository has been archived by the owner on Oct 18, 2019. It is now read-only.

Feature/healthcheck json #525

Merged
merged 3 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions broker-cli/commands/health-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,25 @@ const ENGINE_STATUS_CODES = Object.freeze({
*/

async function healthCheck (args, opts, logger) {
const { rpcAddress } = opts
const {
rpcAddress,
json
} = opts

try {
const client = new BrokerDaemonClient(rpcAddress)

const res = await client.adminService.healthCheck({})

if (json) {
return console.log(JSON.stringify(res))
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure about this: do we need to use logger for this to show up in scalyr?

Also, low priority, in wallet.js we do a pretty print (JSON.stringify(transactions, null, 2). Should we create a ticket to remove the pretty printing so it can also be consumed by other programs more easily?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bradleystachurski this is such a good observation. Thanks for the catch.

  1. we should use logger, i will make the change
  2. pretty printing is actually what we want to do, i've added this to the code

Pretty printing is only there to see it visually (the same for transactions). If there are any use-cases where this doesn't allow someone to use the --json flag effectively, then we can modify.

For us, our current use would be to make sure that different keys in the response are present and I wanted to do this through JSON instead of having to parse the stdout table (although they are arguably the same)

As an example, from the way the code is now, we can see if relayer has RELAYER_OK by running the following:

sparkswap healthcheck --json | grep 'RELAYER_OK' | wc -l

Returning 1 if we are OK and returning 0 if something is wrong

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha, that makes sense.

}

const {
engineStatus = [],
orderbookStatus: orderbookStatuses = [],
relayerStatus = STATUS_CODES.UNKNOWN
} = await client.adminService.healthCheck({})
} = res

const healthcheckTable = new Table({
head: ['Component', 'Status'],
Expand Down Expand Up @@ -114,5 +123,6 @@ module.exports = (program) => {
program
.command('healthcheck', 'Checks the connection between Broker and the Exchange')
.option('--rpc-address [rpc-address]', RPC_ADDRESS_HELP_STRING, validations.isHost)
.option('--json', 'Export result as json', program.BOOL, false)
.action(healthCheck)
}
29 changes: 21 additions & 8 deletions broker-cli/commands/health-check.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,30 @@ describe('healthCheck', () => {
let args
let opts
let logger
let revert
let infoSpy
let errorSpy
let healthCheckStub
let brokerStub
let rpcAddress
let instanceTableStub
let tableStub
let revertTable
let jsonStub
let healthCheckResponse
let reverts

const healthCheck = program.__get__('healthCheck')

beforeEach(() => {
reverts = []
jsonStub = {
stringify: sinon.stub()
}
rpcAddress = undefined
args = {}
opts = { rpcAddress }
infoSpy = sinon.spy()
errorSpy = sinon.spy()
healthCheckStub = sinon.stub().returns({
healthCheckResponse = {
engineStatus: [
{ symbol: 'BTC', status: 'VALIDATED' },
{ symbol: 'LTC', status: 'NOT_SYNCED' }
Expand All @@ -40,15 +45,17 @@ describe('healthCheck', () => {
{ market: 'BTC/LTC', status: 'ORDERBOOK_OK' },
{ market: 'ABC/XYZ', status: 'ORDERBOOK_NOT_SYNCED' }
]
})
}
healthCheckStub = sinon.stub().returns(healthCheckResponse)
instanceTableStub = { push: sinon.stub() }
tableStub = sinon.stub().returns(instanceTableStub)
revertTable = program.__set__('Table', tableStub)

brokerStub = sinon.stub()
brokerStub.prototype.adminService = { healthCheck: healthCheckStub }

revert = program.__set__('BrokerDaemonClient', brokerStub)
reverts.push(program.__set__('Table', tableStub))
reverts.push(program.__set__('BrokerDaemonClient', brokerStub))
reverts.push(program.__set__('JSON', jsonStub))

logger = {
info: infoSpy,
Expand All @@ -57,15 +64,21 @@ describe('healthCheck', () => {
})

afterEach(() => {
revert()
revertTable()
reverts.forEach(r => r())
})

it('makes a request to the broker', async () => {
await healthCheck(args, opts, logger)
expect(healthCheckStub).to.have.been.called()
})

it('returns json if the user specifies a json flag', async () => {
opts.json = true
await healthCheck(args, opts, logger)
expect(instanceTableStub.push).to.not.have.been.called()
expect(jsonStub.stringify).to.have.been.calledWith(healthCheckResponse)
treygriffith marked this conversation as resolved.
Show resolved Hide resolved
})

it('adds engine statuses to the healthcheck table', async () => {
await healthCheck(args, opts, logger)
expect(instanceTableStub.push).to.have.been.calledWith(['BTC Engine', 'OK'.green])
Expand Down