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

Commit

Permalink
format data sent to splunk HEC (#61)
Browse files Browse the repository at this point in the history
* format data sent to splunk HEC
 🐿 v2.7.0

* adding more format hec tests
 🐿 v2.7.0
  • Loading branch information
kitkat119 authored Feb 12, 2018
1 parent 0ee6883 commit 2ee8517
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/lib/formatHEC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const formatHEC = ({ level, message, meta = {} } = {}) => {
return message ? Object.assign({}, { level, message }, meta) : Object.assign({}, { level }, meta);
};

export default formatHEC;
4 changes: 3 additions & 1 deletion src/lib/transports/splunkHEC.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import winston from 'winston';
import formatHEC from '../formatHEC';
const https = require('https');

class SplunkHEC extends winston.Transport {

log (level, message, meta) {
const httpsAgent = new https.Agent({ keepAlive: true });
const formattedMessage = formatHEC({ level, message, meta });

const data = {
'time': Date.now(),
'host': 'localhost',
'source': `/var/log/apps/heroku/ft-${process.env.SYSTEM_CODE}.log`,
'sourcetype': '_json',
'index': 'heroku',
'event': { level, message, meta }
'event': formattedMessage
};

return fetch('https://http-inputs-financialtimes.splunkcloud.com/services/collector/event', {
Expand Down
66 changes: 66 additions & 0 deletions test/lib/formatHEC.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const chai = require('chai');
chai.should();
const expect = chai.expect;

import formatHEC from '../../dist/lib/formatHEC';

describe('FormatHEC', () => {

it('should exist', () => {
formatHEC.should.exist;
});

it('should format all data options', () => {
const options = {
level: 'info',
message: 'Hello world',
meta: { event: 'TEST' }
}

expect(formatHEC(options)).to.deep.equal({
level: 'info',
message: 'Hello world',
event: 'TEST'
});
});

it('should handle data containing an error', () => {
const err = new Error('This is an error');
const options = {
level: 'error',
meta: { event: 'TEST_ERROR', err }
}

expect(formatHEC(options)).to.deep.equal({
level: 'error',
event: 'TEST_ERROR', err
});
});

it('should handle no meta data ', () => {
const options = {
level: 'info',
message: 'a message'
}

expect(formatHEC(options)).to.deep.equal({
level: 'info',
message: 'a message'
});
});

it('should handle no message', () => {
const count = 10;
const options = {
level: 'info',
meta: { event: 'TEST', count }
}

expect(formatHEC(options)).to.deep.equal({
level: 'info',
event: 'TEST',
count: 10
});
});

});

0 comments on commit 2ee8517

Please sign in to comment.