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

Commit

Permalink
Fix SplunkHEC not logging because of non-JSON body (#81)
Browse files Browse the repository at this point in the history
* Use `json-stringify-safe` to stringify splunk body
* Add logging when there is an error logging to splunk
  • Loading branch information
taktran authored Nov 2, 2018
1 parent 9a16211 commit 35df521
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"isomorphic-fetch": "^2.2.1",
"json-stringify-safe": "^5.0.1",
"request": "^2.83.0",
"winston": "^2.4.0"
},
Expand Down
24 changes: 21 additions & 3 deletions src/lib/transports/splunkHEC.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import winston from 'winston';
import { inspect } from 'util';
import stringify from 'json-stringify-safe';
import formatHEC from '../formatHEC';

const https = require('https');

const throwIfNotOk = res => {
const { ok, status, url } = res;
if (!ok) {
return res.json()
.then(errorBody => {
console.error('Fetch error:', status, url, errorBody); // eslint-disable-line no-console
throw errorBody;
});
}
return res;
};

class SplunkHEC extends winston.Transport {

log (level, message, meta) {
Expand All @@ -25,8 +38,13 @@ class SplunkHEC extends winston.Transport {
},
pool: httpsAgent,
timeout: 3000,
body: inspect(data)
}).catch(() => {});
body: stringify(data)
})
.then(throwIfNotOk)
.catch((error) => {
/* eslint no-console: 0 */
console.log('Error logging to splunk', error, data);
});
};

}
Expand Down
30 changes: 28 additions & 2 deletions test/lib/transports/splunkHEC.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const chai = require('chai');
const { expect } = chai;
chai.should();

require('isomorphic-fetch');
Expand Down Expand Up @@ -65,12 +66,37 @@ describe('SplunkHEC', () => {
it('should send a log if data is circularly referenced', () => {
nock('https://http-inputs-financialtimes.splunkcloud.com')
.post('/services/collector/event')
.reply(201, { text: 'Successful request', code: 0 });
// NOTE: Returning requestBody for further tests
.reply(201, (uri, requestBody) => {
return requestBody;
});

const splunkHECTransport = new SplunkHEC();
const circularData = { field: 'value' };
circularData.circularData = circularData;
return splunkHECTransport.log('info', 'a message', circularData);
return splunkHECTransport.log('info', 'a message', circularData)
// Process JSON, to check returned requestBody
.then(res => res.json())
.then(data => {
expect(data).to.be.an('object');
});;
});

it('sent log body should be JSON.parse-able', () => {
nock('https://http-inputs-financialtimes.splunkcloud.com')
.post('/services/collector/event')
// NOTE: Returning requestBody for further tests
.reply(201, (uri, requestBody) => {
return requestBody;
});

const splunkHECTransport = new SplunkHEC();
return splunkHECTransport.log('info', 'a message', { field: 'value' })
// Process JSON, to check returned requestBody
.then(res => res.json())
.then(data => {
expect(data).to.be.an('object');
});
});
});

Expand Down

0 comments on commit 35df521

Please sign in to comment.