Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update @elastic/good with all new changes from @hapi/good #4

Merged
merged 17 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 0 additions & 3 deletions .npmignore

This file was deleted.

20 changes: 6 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
language: node_js
version: ~> 1.0

node_js:
- "8"
- "10"
- "12"
- "node"

sudo: false

install:
- "npm install"
- "npm install @hapi/hapi@$HAPI_VERSION"

env:
- HAPI_VERSION="18"
import:
- hapijs/ci-config-travis:node_js.yml@main
- hapijs/ci-config-travis:install_plugin.yml@main
- hapijs/ci-config-travis:os.yml@main
- hapijs/ci-config-travis:env.yml@main
244 changes: 241 additions & 3 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,242 @@
## Introduction

## good

**good** is a process monitor that listens for one or more of the below 'event types'. All of these events, _except_ 'ops' map to a hapi event documented [here](https://github.com/hapijs/hapi/blob/master/API.md#server-events).
**good** is a hapi plugin process monitor that listens for one or more of the below 'event types'. All of these events, _except_ 'ops' map to a hapi event documented [here](https://github.com/hapijs/hapi/blob/master/API.md#server-events).

Applications with multiple server instances, each with its own monitor should only include one _log_ subscription per destination
as general events are a process-wide facility and will result in duplicated log events.

Note: this module is being deprecated on December 1st, 2019 due to lack to available support resources. Please consider using [another logging plugin](https://hapi.dev/plugins/#logging).

## Example Usage

### Log to Console

```js
const Hapi = require('@hapi/hapi');


const start = async function () {

const server = Hapi.server();

const handler = function (request, h) {

request.server.log(['a', 'b', 'c'], 'some message');
return 'ok';
};

server.route({ path: '/', method: 'GET', handler })

await server.register({
plugin: require('@hapi/good'),
options: {
ops: {
interval: 1000
},
reporters: {
myConsoleReporter: [
{
module: '@hapi/good-squeeze',
name: 'Squeeze',
args: [{ log: '*', response: '*', ops: '*' }]
},
{
module: '@hapi/good-console'
},
'stdout'
]
}
}
});

await server.start();
console.info(`Server started at ${server.info.uri}`);
await server.inject('/');
};

start();
```

This reporter spec logs ops statistics, request responses, and server log events to the console. It should log to the console something like:
```
Server started at http://localhost:41759
190921/234014.495, [log,a,b,c] data: some message
190921/234014.494, (1569109214494:localhost:3616:k0u742ki:10000) [response] http://localhost:41759: get / {} 200 (6ms)
190921/234015.494, [ops] memory: 52Mb, uptime (seconds): 1.148846148, load: [0.02734375,0.0302734375,0]
190921/234016.493, [ops] memory: 52Mb, uptime (seconds): 2.149207332, load: [0.02734375,0.0302734375,0]
190921/234017.494, [ops] memory: 52Mb, uptime (seconds): 3.149818512, load: [0.02734375,0.0302734375,0]
```

It uses `@hapi/good-squeeze` to select only log and response events.

Events that pass the filter stream to `@hapi/good-console` for formatting. See the `good console` [constructor docs](https://github.com/hapijs/good-console#new-goodconsoleconfig) for details on `args` values.

Formatted events stream to `process.stdout` for display.

### Log to file

Create a basic write stream class and use with good:

```js
const Fs = require('fs-extra');
const Hapi = require('@hapi/hapi');


class GoodFile extends Fs.WriteStream {

constructor(path, options) {

const defaults = {
encoding: 'utf8',
flags: 'a',
mode: 0o666
};

const settings = Object.assign({}, defaults, options);
settings.fd = -1; // Prevent open from being called in `super`

super(path, settings);
this.open();
}

open() {

this.fd = null;
Fs.ensureFile(this.path, (err) => {

if (err) {
this.destroy();
this.emit('error', err);
return;
}

super.open();
});
}
}


const start = async function () {

const server = Hapi.server();

const handler = function (request, h) {

request.server.log(['a', 'b', 'c'], 'some message');
return 'ok';
};

server.route({ path: '/', method: 'GET', handler })

await server.register({
plugin: require('@hapi/good'),
options: {
ops: {
interval: 1000
},
reporters: {
myReporter: [
{
module: '@hapi/good-squeeze', // Transform payload into a safe string
name: 'SafeJson'
},
{
module: GoodFile,
args: ['./log.json']
}
]
}
}
});

await server.start();
server.log(['log'], 'Server Started');
await server.inject('/');
};

start();
```

This will create or append a file `./log.json` with the following:
```json
{"event":"log","timestamp":1569109412144,"tags":["log"],"data":"Server Started","pid":3630}
{"event":"log","timestamp":1569109412148,"tags":["a","b","c"],"data":"some message","pid":3630}
{"event":"response","timestamp":1569109412146,"id":"1569109412146:localhost:3630:k0u78b2x:10000","instance":"http://localhost:44883","method":"get","path":"/","query":{},"responseTime":5,"statusCode":200,"pid":3630,"httpVersion":"1.1","route":"/","log":[],"source":{"remoteAddress":"127.0.0.1","userAgent":"shot"},"config":{}}
{"event":"ops","timestamp":1569109413146,"host":"localhost","pid":3630,"os":{"load":[0.0029296875,0.02001953125,0],"mem":{"total":16682434560,"free":6650597376},"uptime":851831},"proc":{"uptime":1.160361771,"mem":{"rss":54652928,"heapTotal":18948096,"heapUsed":8310912,"external":1271839},"delay":0.9361389875411987},"load":{"requests":{"44883":{"total":1,"disconnects":0,"statusCodes":{"200":1}}},"responseTimes":{"44883":{"avg":5,"max":5}},"sockets":{"http":{"total":0},"https":{"total":0}}}}
{"event":"ops","timestamp":1569109414145,"host":"localhost","pid":3630,"os":{"load":[0.0029296875,0.02001953125,0],"mem":{"total":16682434560,"free":6650597376},"uptime":851832},"proc":{"uptime":2.160405932,"mem":{"rss":54652928,"heapTotal":18948096,"heapUsed":8358032,"external":1271887},"delay":0.29865598678588867},"load":{"requests":{"44883":{"total":0,"disconnects":0,"statusCodes":{}}},"responseTimes":{"44883":{"avg":null,"max":0}},"sockets":{"http":{"total":0},"https":{"total":0}}}}
```

### Add custom transform stream

```js
const Stream = require('stream');
const Hapi = require('@hapi/hapi');


class LogDateTransform extends Stream.Transform {

constructor() {

super({ objectMode: true });
}

_transform(data, enc, next) {

const date = new Date(Date.now()).toLocaleString('en-US');

if (data.data) {
return next(null, `${data.event}: ${date}: ${data.data}\n`);
}

next(null);
}
}


const start = async function () {

const server = Hapi.server();

const handler = function (request, h) {

request.server.log(['a', 'b', 'c'], 'some message');
return 'ok';
};

server.route({ path: '/', method: 'GET', handler })

await server.register({
plugin: require('@hapi/good'),
options: {
ops: {
interval: 1000
},
reporters: {
myReporter: [
new LogDateTransform(),
'stdout'
]
}
}
});

await server.start();
server.log('info', 'Server is running...');
await server.inject('/');
};

start();
```

This reporter logs data with a date time, writing to the console something like:

```
log: 9 / 21 / 2019, 4: 47: 25 PM: Server is running...
log: 9 / 21 / 2019, 4: 47: 25 PM: some message
```

## Usage

### Options
- `[includes]` - optional configuration object
- `[request]` - array of extra hapi request object fields to supply to reporters on "request", "response", and "error" events. Valid values ['headers', 'payload']. Defaults to `[]`.
Expand Down Expand Up @@ -243,3 +474,10 @@ Because the extension payloads from hapi can vary from one version to another an
- `event` - the event name.
- `timestamp` - the time the event occurred.
- `payload` - array of arguments hapi passed to our event handler function

## Existing streams

The following streams are maintained by the hapi community and are known to work with good. Any transform or write stream can work with good, these are just a few inside the hapijs organization.

- [good-squeeze](https://github.com/hapijs/good-squeeze)
- [good-console](https://github.com/hapijs/good-console)
3 changes: 0 additions & 3 deletions CHANGELOG.md

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2014-2019, Sideway Inc, and project contributors
Copyright (c) 2014-2020, Sideway Inc, and project contributors
Copyright (c) 2012-2014, Walmart.
All rights reserved.

Expand Down
Loading