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

Commit

Permalink
feat: convert to JSON model (#2) [MYCS-9469]
Browse files Browse the repository at this point in the history
* feat: convert to key-value model

* fix: update tests

* feat: add r7insight_node/lib/serialize

* fix: test, update readme
  • Loading branch information
denkorolenko authored Feb 19, 2019
1 parent 9aca297 commit ff50d04
Show file tree
Hide file tree
Showing 6 changed files with 1,259 additions and 43 deletions.
24 changes: 24 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 2
jobs:
build:
docker:
- image: circleci/node:8.10

steps:
- checkout

- restore_cache:
key: dependency-cache-{{ checksum "package.json" }}

- run:
name: install-npm
command: npm install

- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- ./node_modules

- run:
name: test
command: npm test
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# good-console
# good-console-json

`good-console` is a transform stream useful for turning [good](https://github.com/hapijs/good) server events into formatted strings.

[![Build Status](https://travis-ci.org/hapijs/good-console.svg?branch=master)](http://travis-ci.org/hapijs/good-console)
[![Current Version](https://img.shields.io/npm/v/good-console.svg)](https://www.npmjs.com/package/good-console)

Lead Maintainer: [Open Position](https://github.com/hapijs/good-console/issues/108)
`good-consol-json` is a transform stream useful for turning [good](https://github.com/hapijs/good) server events into formatted strings.
Base on using JSON format, [see](https://docs.logentries.com/docs/json)

## Usage

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

Below are example outputs for the designated event type:

[TODO] examples should be updated to JSON format
- "ops" - 160318/013330.957, [ops] memory: 29Mb, uptime (seconds): 6, load: [1.650390625,1.6162109375,1.65234375]
- "error" - 160318/013330.957, [error,`event.tags`] message: Just a simple error, stack: `event.error.stack`
- "request" - 160318/013330.957, [request,`event.tags`] data: you made a request
Expand Down
44 changes: 30 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const Hoek = require('hoek');
const Moment = require('moment');
const Stream = require('stream');
const SafeStringify = require('json-stringify-safe');
const BuildSerialize = require('r7insight_node/lib/serialize');

const internals = {
defaults: {
Expand All @@ -15,6 +15,8 @@ const internals = {
}
};

const serialize = BuildSerialize({});

internals.utility = {
formatOutput(event, settings) {

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

timestamp = timestamp.format(settings.format);

event.tags = event.tags.toString();
const tags = ` [${event.tags}] `;

// add event id information if available, typically for 'request' events
const id = event.id ? ` (${event.id})` : '';

const output = `${timestamp},${id}${tags}${event.data}`;
const output = serialize(Object.assign({}, {
timestamp,
tags: event.tags
// add event id information if available, typically for 'request' events
// eventId: event.id
}, event.data));

return output + '\n';
},
Expand Down Expand Up @@ -78,14 +79,21 @@ internals.utility = {

formatResponse(event, tags, settings) {

const query = event.query ? SafeStringify(event.query) : '';
const query = event.query || '';
const method = internals.utility.formatMethod(event.method, settings);
const statusCode = internals.utility.formatStatusCode(event.statusCode, settings) || '';

// event, timestamp, id, instance, labels, method, path, query, responseTime,
// statusCode, pid, httpVersion, source, remoteAddress, userAgent, referer, log
// method, pid, error
const output = `${event.instance}: ${method} ${event.path} ${query} ${statusCode} (${event.responseTime}ms)`;
const output = {
// eventInstance: event.instance,
method,
path: event.path,
query,
statusCode,
responseTimeMs: event.responseTime
};

const response = {
id: event.id,
Expand All @@ -100,7 +108,11 @@ internals.utility = {
formatOps(event, tags, settings) {

const memory = Math.round(event.proc.mem.rss / (1024 * 1024));
const output = `memory: ${memory}Mb, uptime (seconds): ${event.proc.uptime}, load: [${event.os.load}]`;
const output = {
memoryMb: memory,
uptimeSeconds: event.proc.uptime,
load: event.os.load
};

const ops = {
timestamp: event.timestamp,
Expand All @@ -113,7 +125,10 @@ internals.utility = {

formatError(event, tags, settings) {

const output = `message: ${event.error.message}, stack: ${event.error.stack}`;
const output = {
message: event.error.message,
stack: event.error.stack
};

const error = {
id: event.id,
Expand All @@ -127,8 +142,9 @@ internals.utility = {

formatDefault(event, tags, settings) {

const data = typeof event.data === 'object' ? SafeStringify(event.data) : event.data;
const output = `data: ${data}`;
const output = {
data: event.data
};

const defaults = {
timestamp: event.timestamp,
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "good-console",
"name": "good-console-json",
"version": "7.1.0",
"repository": "git://github.com/hapijs/good-console",
"description": "Console broadcasting for Good process monitor",
"repository": "git://github.com/mycsHQ/good-console-json",
"description": "Console broadcasting for Good process monitor in JSON",
"main": "lib/index.js",
"scripts": {
"test": "lab -t 100 -vLa code"
},
"dependencies": {
"hoek": "4.x.x",
"joi": "12.x.x",
"json-stringify-safe": "5.0.x",
"moment": "2.20.x"
"moment": "2.20.x",
"r7insight_node": "^1.8.2"
},
"devDependencies": {
"code": "4.x.x",
Expand Down
39 changes: 22 additions & 17 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

const Lab = require('lab');
const Moment = require('moment');
// const buildSerialize = require('r7insight_node/lib/serialize');

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

// const serialize = buildSerialize({});

// Declare internals

const internals = {
Expand Down Expand Up @@ -125,7 +128,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -146,7 +149,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -168,7 +171,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -187,7 +190,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -211,7 +214,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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`);
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`);
done();
});
});
Expand All @@ -233,7 +236,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -255,7 +258,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -277,7 +280,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -299,7 +302,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -323,7 +326,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(20);
expect(out.data[0]).to.be.equal('160318/013330.957, [ops] memory: 29Mb, uptime (seconds): 6, load: [1.650390625,1.6162109375,1.65234375]\n');
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');
done();
});
});
Expand All @@ -345,7 +348,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
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');
done();
});
});
Expand All @@ -367,7 +370,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

expect(out.data).to.have.length(1);
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');
expect(out.data[0]).to.be.equal('{"timestamp":"160318/013330.957","tags":["request","user","info"],"data":"you made a request"}\n');
done();
});
});
Expand All @@ -389,7 +392,7 @@ describe('GoodConsole', () => {
reader.once('end', () => {

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

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

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

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

expect(out.data).to.have.length(1);
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');
const logObject = JSON.parse(out.data[0]);
logObject.stack = logObject.stack.split('\n')[0];
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"}');
done();
});
});
Expand Down
Loading

0 comments on commit ff50d04

Please sign in to comment.