Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit ff50d04

Browse files
authored
feat: convert to JSON model (#2) [MYCS-9469]
* feat: convert to key-value model * fix: update tests * feat: add r7insight_node/lib/serialize * fix: test, update readme
1 parent 9aca297 commit ff50d04

6 files changed

Lines changed: 1259 additions & 43 deletions

File tree

.circleci/config.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: 2
2+
jobs:
3+
build:
4+
docker:
5+
- image: circleci/node:8.10
6+
7+
steps:
8+
- checkout
9+
10+
- restore_cache:
11+
key: dependency-cache-{{ checksum "package.json" }}
12+
13+
- run:
14+
name: install-npm
15+
command: npm install
16+
17+
- save_cache:
18+
key: dependency-cache-{{ checksum "package.json" }}
19+
paths:
20+
- ./node_modules
21+
22+
- run:
23+
name: test
24+
command: npm test

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
# good-console
1+
# good-console-json
22

3-
`good-console` is a transform stream useful for turning [good](https://github.com/hapijs/good) server events into formatted strings.
4-
5-
[![Build Status](https://travis-ci.org/hapijs/good-console.svg?branch=master)](http://travis-ci.org/hapijs/good-console)
6-
[![Current Version](https://img.shields.io/npm/v/good-console.svg)](https://www.npmjs.com/package/good-console)
7-
8-
Lead Maintainer: [Open Position](https://github.com/hapijs/good-console/issues/108)
3+
`good-consol-json` is a transform stream useful for turning [good](https://github.com/hapijs/good) server events into formatted strings.
4+
Base on using JSON format, [see](https://docs.logentries.com/docs/json)
95

106
## Usage
117

@@ -21,6 +17,7 @@ Creates a new GoodConsole object with the following arguments:
2117

2218
Below are example outputs for the designated event type:
2319

20+
[TODO] examples should be updated to JSON format
2421
- "ops" - 160318/013330.957, [ops] memory: 29Mb, uptime (seconds): 6, load: [1.650390625,1.6162109375,1.65234375]
2522
- "error" - 160318/013330.957, [error,`event.tags`] message: Just a simple error, stack: `event.error.stack`
2623
- "request" - 160318/013330.957, [request,`event.tags`] data: you made a request

lib/index.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
const Hoek = require('hoek');
66
const Moment = require('moment');
77
const Stream = require('stream');
8-
const SafeStringify = require('json-stringify-safe');
8+
const BuildSerialize = require('r7insight_node/lib/serialize');
99

1010
const internals = {
1111
defaults: {
@@ -15,6 +15,8 @@ const internals = {
1515
}
1616
};
1717

18+
const serialize = BuildSerialize({});
19+
1820
internals.utility = {
1921
formatOutput(event, settings) {
2022

@@ -26,13 +28,12 @@ internals.utility = {
2628

2729
timestamp = timestamp.format(settings.format);
2830

29-
event.tags = event.tags.toString();
30-
const tags = ` [${event.tags}] `;
31-
32-
// add event id information if available, typically for 'request' events
33-
const id = event.id ? ` (${event.id})` : '';
34-
35-
const output = `${timestamp},${id}${tags}${event.data}`;
31+
const output = serialize(Object.assign({}, {
32+
timestamp,
33+
tags: event.tags
34+
// add event id information if available, typically for 'request' events
35+
// eventId: event.id
36+
}, event.data));
3637

3738
return output + '\n';
3839
},
@@ -78,14 +79,21 @@ internals.utility = {
7879

7980
formatResponse(event, tags, settings) {
8081

81-
const query = event.query ? SafeStringify(event.query) : '';
82+
const query = event.query || '';
8283
const method = internals.utility.formatMethod(event.method, settings);
8384
const statusCode = internals.utility.formatStatusCode(event.statusCode, settings) || '';
8485

8586
// event, timestamp, id, instance, labels, method, path, query, responseTime,
8687
// statusCode, pid, httpVersion, source, remoteAddress, userAgent, referer, log
8788
// method, pid, error
88-
const output = `${event.instance}: ${method} ${event.path} ${query} ${statusCode} (${event.responseTime}ms)`;
89+
const output = {
90+
// eventInstance: event.instance,
91+
method,
92+
path: event.path,
93+
query,
94+
statusCode,
95+
responseTimeMs: event.responseTime
96+
};
8997

9098
const response = {
9199
id: event.id,
@@ -100,7 +108,11 @@ internals.utility = {
100108
formatOps(event, tags, settings) {
101109

102110
const memory = Math.round(event.proc.mem.rss / (1024 * 1024));
103-
const output = `memory: ${memory}Mb, uptime (seconds): ${event.proc.uptime}, load: [${event.os.load}]`;
111+
const output = {
112+
memoryMb: memory,
113+
uptimeSeconds: event.proc.uptime,
114+
load: event.os.load
115+
};
104116

105117
const ops = {
106118
timestamp: event.timestamp,
@@ -113,7 +125,10 @@ internals.utility = {
113125

114126
formatError(event, tags, settings) {
115127

116-
const output = `message: ${event.error.message}, stack: ${event.error.stack}`;
128+
const output = {
129+
message: event.error.message,
130+
stack: event.error.stack
131+
};
117132

118133
const error = {
119134
id: event.id,
@@ -127,8 +142,9 @@ internals.utility = {
127142

128143
formatDefault(event, tags, settings) {
129144

130-
const data = typeof event.data === 'object' ? SafeStringify(event.data) : event.data;
131-
const output = `data: ${data}`;
145+
const output = {
146+
data: event.data
147+
};
132148

133149
const defaults = {
134150
timestamp: event.timestamp,

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"name": "good-console",
2+
"name": "good-console-json",
33
"version": "7.1.0",
4-
"repository": "git://github.com/hapijs/good-console",
5-
"description": "Console broadcasting for Good process monitor",
4+
"repository": "git://github.com/mycsHQ/good-console-json",
5+
"description": "Console broadcasting for Good process monitor in JSON",
66
"main": "lib/index.js",
77
"scripts": {
88
"test": "lab -t 100 -vLa code"
99
},
1010
"dependencies": {
1111
"hoek": "4.x.x",
1212
"joi": "12.x.x",
13-
"json-stringify-safe": "5.0.x",
14-
"moment": "2.20.x"
13+
"moment": "2.20.x",
14+
"r7insight_node": "^1.8.2"
1515
},
1616
"devDependencies": {
1717
"code": "4.x.x",

test/index.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
const Lab = require('lab');
77
const Moment = require('moment');
8+
// const buildSerialize = require('r7insight_node/lib/serialize');
89

910
const Streams = require('./fixture/streams');
1011
const GoodConsole = require('..');
1112

13+
// const serialize = buildSerialize({});
14+
1215
// Declare internals
1316

1417
const internals = {
@@ -125,7 +128,7 @@ describe('GoodConsole', () => {
125128
reader.once('end', () => {
126129

127130
expect(out.data).to.have.length(1);
128-
expect(out.data[0]).to.be.equal('160318/013330.957, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: \u001b[1;33mpost\u001b[0m /data {"name":"adam"} \u001b[32m200\u001b[0m (150ms)\n');
131+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["response"],"method":"\\u001b[1;33mpost\\u001b[0m","path":"/data","query":{"name":"adam"},"statusCode":"\\u001b[32m200\\u001b[0m","responseTimeMs":150}\n');
129132
done();
130133
});
131134
});
@@ -146,7 +149,7 @@ describe('GoodConsole', () => {
146149
reader.once('end', () => {
147150

148151
expect(out.data).to.have.length(1);
149-
expect(out.data[0]).to.be.equal('160318/013330.957, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: \u001b[1;33mpost\u001b[0m /data \u001b[32m200\u001b[0m (150ms)\n');
152+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["response"],"method":"\\u001b[1;33mpost\\u001b[0m","path":"/data","query":"","statusCode":"\\u001b[32m200\\u001b[0m","responseTimeMs":150}\n');
150153
done();
151154
});
152155
});
@@ -168,7 +171,7 @@ describe('GoodConsole', () => {
168171
reader.once('end', () => {
169172

170173
expect(out.data).to.have.length(1);
171-
expect(out.data[0]).to.be.equal('160318/013330.957, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: \u001b[1;33mpost\u001b[0m /data {"name":"adam"} (150ms)\n');
174+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["response"],"method":"\\u001b[1;33mpost\\u001b[0m","path":"/data","query":{"name":"adam"},"statusCode":"","responseTimeMs":150}\n');
172175
done();
173176
});
174177
});
@@ -187,7 +190,7 @@ describe('GoodConsole', () => {
187190
reader.once('end', () => {
188191

189192
expect(out.data).to.have.length(1);
190-
expect(out.data[0]).to.be.equal('160318/013330.957, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: post /data {"name":"adam"} 200 (150ms)\n');
193+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["response"],"method":"post","path":"/data","query":{"name":"adam"},"statusCode":200,"responseTimeMs":150}\n');
191194
done();
192195
});
193196
});
@@ -211,7 +214,7 @@ describe('GoodConsole', () => {
211214
reader.once('end', () => {
212215

213216
expect(out.data).to.have.length(1);
214-
expect(out.data[0]).to.be.equal(`${date}, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: \u001b[1;33mpost\u001b[0m /data {"name":"adam"} \u001b[32m200\u001b[0m (150ms)\n`);
217+
expect(out.data[0]).to.be.equal(`{"timestamp":"${date}","tags":["response"],"method":"\\u001b[1;33mpost\\u001b[0m","path":"/data","query":{"name":"adam"},"statusCode":"\\u001b[32m200\\u001b[0m","responseTimeMs":150}\n`);
215218
done();
216219
});
217220
});
@@ -233,7 +236,7 @@ describe('GoodConsole', () => {
233236
reader.once('end', () => {
234237

235238
expect(out.data).to.have.length(1);
236-
expect(out.data[0]).to.be.equal('160318/013330.957, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: \u001b[1;34mhead\u001b[0m /data {"name":"adam"} \u001b[32m200\u001b[0m (150ms)\n');
239+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["response"],"method":"\\u001b[1;34mhead\\u001b[0m","path":"/data","query":{"name":"adam"},"statusCode":"\\u001b[32m200\\u001b[0m","responseTimeMs":150}\n');
237240
done();
238241
});
239242
});
@@ -255,7 +258,7 @@ describe('GoodConsole', () => {
255258
reader.once('end', () => {
256259

257260
expect(out.data).to.have.length(1);
258-
expect(out.data[0]).to.be.equal('160318/013330.957, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: \u001b[1;33mpost\u001b[0m /data {"name":"adam"} \u001b[31m599\u001b[0m (150ms)\n');
261+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["response"],"method":"\\u001b[1;33mpost\\u001b[0m","path":"/data","query":{"name":"adam"},"statusCode":"\\u001b[31m599\\u001b[0m","responseTimeMs":150}\n');
259262
done();
260263
});
261264
});
@@ -277,7 +280,7 @@ describe('GoodConsole', () => {
277280
reader.once('end', () => {
278281

279282
expect(out.data).to.have.length(1);
280-
expect(out.data[0]).to.be.equal('160318/013330.957, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: \u001b[1;33mpost\u001b[0m /data {"name":"adam"} \u001b[33m418\u001b[0m (150ms)\n');
283+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["response"],"method":"\\u001b[1;33mpost\\u001b[0m","path":"/data","query":{"name":"adam"},"statusCode":"\\u001b[33m418\\u001b[0m","responseTimeMs":150}\n');
281284
done();
282285
});
283286
});
@@ -299,7 +302,7 @@ describe('GoodConsole', () => {
299302
reader.once('end', () => {
300303

301304
expect(out.data).to.have.length(1);
302-
expect(out.data[0]).to.be.equal('160318/013330.957, (1458264811279:localhost:16014:ilx17kv4:10001) [response] http://localhost:61253: \u001b[1;33mpost\u001b[0m /data {"name":"adam"} \u001b[36m304\u001b[0m (150ms)\n');
305+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["response"],"method":"\\u001b[1;33mpost\\u001b[0m","path":"/data","query":{"name":"adam"},"statusCode":"\\u001b[36m304\\u001b[0m","responseTimeMs":150}\n');
303306
done();
304307
});
305308
});
@@ -323,7 +326,7 @@ describe('GoodConsole', () => {
323326
reader.once('end', () => {
324327

325328
expect(out.data).to.have.length(20);
326-
expect(out.data[0]).to.be.equal('160318/013330.957, [ops] memory: 29Mb, uptime (seconds): 6, load: [1.650390625,1.6162109375,1.65234375]\n');
329+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["ops"],"memoryMb":29,"uptimeSeconds":6,"load":[1.650390625,1.6162109375,1.65234375]}\n');
327330
done();
328331
});
329332
});
@@ -345,7 +348,7 @@ describe('GoodConsole', () => {
345348
reader.once('end', () => {
346349

347350
expect(out.data).to.have.length(1);
348-
expect(out.data[0]).to.be.equal('160318/013330.957, (1419005623332:new-host.local:48767:i3vrb3z7:10000) [error,user,info] message: Just a simple error, stack: Error: Just a simple Error\n');
351+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["error","user","info"],"message":"Just a simple error","stack":"Error: Just a simple Error"}\n');
349352
done();
350353
});
351354
});
@@ -367,7 +370,7 @@ describe('GoodConsole', () => {
367370
reader.once('end', () => {
368371

369372
expect(out.data).to.have.length(1);
370-
expect(out.data[0]).to.be.equal('160318/013330.957, (1419005623332:new-host.local:48767:i3vrb3z7:10000) [request,user,info] data: you made a request\n');
373+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["request","user","info"],"data":"you made a request"}\n');
371374
done();
372375
});
373376
});
@@ -389,7 +392,7 @@ describe('GoodConsole', () => {
389392
reader.once('end', () => {
390393

391394
expect(out.data).to.have.length(1);
392-
expect(out.data[0]).to.be.equal('160318/013330.957, [request,user,info] data: you made a default\n');
395+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["request","user","info"],"data":"you made a default"}\n');
393396
done();
394397
});
395398
});
@@ -411,7 +414,7 @@ describe('GoodConsole', () => {
411414
reader.once('end', () => {
412415

413416
expect(out.data).to.have.length(1);
414-
expect(out.data[0]).to.be.equal('160318/013330.957, [request,user,info] data: (none)\n');
417+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["request","user","info"],"data":"(none)"}\n');
415418
done();
416419
});
417420
});
@@ -433,7 +436,7 @@ describe('GoodConsole', () => {
433436
reader.once('end', () => {
434437

435438
expect(out.data).to.have.length(1);
436-
expect(out.data[0]).to.be.equal('160318/013330.957, [request,user,info] data: {"hello":"world"}\n');
439+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["request","user","info"],"data":{"hello":"world"}}\n');
437440
done();
438441
});
439442
});
@@ -455,7 +458,7 @@ describe('GoodConsole', () => {
455458
reader.once('end', () => {
456459

457460
expect(out.data).to.have.length(1);
458-
expect(out.data[0]).to.be.equal('160318/013330.957, [request,test] data: you made a default\n');
461+
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["request","test"],"data":"you made a default"}\n');
459462
done();
460463
});
461464
});
@@ -477,7 +480,9 @@ describe('GoodConsole', () => {
477480
reader.once('end', () => {
478481

479482
expect(out.data).to.have.length(1);
480-
expect(out.data[0].split('\n')[0]).to.be.equal('160318/013330.957, [request,user,info] message: you logged an error, stack: Error: you logged an error');
483+
const logObject = JSON.parse(out.data[0]);
484+
logObject.stack = logObject.stack.split('\n')[0];
485+
expect(JSON.stringify(logObject)).to.be.equal('{"timestamp":"160318/013330.957","tags":["request","user","info"],"message":"you logged an error","stack":"Error: you logged an error"}');
481486
done();
482487
});
483488
});

0 commit comments

Comments
 (0)