This plugin will read, generate and track a tracer header on request
s that is then injected into the Good
log stream via an injected tracer
object.
{
"tracer": {
"uuid": "STRING",
"depth": "INTEGER"
}
}
The depth
value provides insight into the hierarchy chain of requests. Combining the uuid
+ depth
+ timestamp
of a request will provide a mapping to the request chain.
NOTE: This module uses the
debug
logging tool. UseDEBUG=hapi:plugins:good-tracer
to view debug logging.
$ npm install -S @goodwaygroup/lib-hapi-good-tracer
In your index.js
for the Hapi server, register the plugin:
await server.register({
plugin: require('@goodwaygroup/lib-hapi-good-tracer'),
options: {
traceUUIDHeader: 'x-custom-trace-uuid', // optional defaults to 'x-gg-trace-uuid'
traceDepthHeader: 'x-custom-trace-depth', // optional defaults to 'x-gg-trace-depth'
enableStatsRoute: true, // optional defaults to false
baseRoute: '/debug', // optional defaults to ''
cache: {
stdTTL: 60 // optional defaults to 3600 seconds
},
axios: {
main: { // defaults to {}
headers: {
common: {
'user-agent': 'service-yolo'
}
}
}
}
}
});
// add stream to Good log reporters
const logReporters = {
console: [
server.plugins.goodTracer.GoodSourceTracer, // Stream Transform that will inject the tracer object
{
module: 'good-squeeze',
name: 'Squeeze',
args: [{
response: { exclude: 'healthcheck' },
log: '*',
request: '*',
error: '*',
ops: '*'
}]
}, {
module: 'good-squeeze',
name: 'SafeJson'
}, 'stdout']
};
await server.register({
plugin: Good,
options: {
reporters: logReporters
}
});
Passing axios
plugin or route configuration will create Axios instances on the request
. Use these instances to have the trace headers injected. This will allow for chained request tracing.
await server.register({
plugin: require('@goodwaygroup/lib-hapi-good-tracer'),
options: {
axios: {
main: {}
}
}
});
const routes = [{
method: 'GET',
path: '/proxy/google',
config: {
tags: ['proxy'],
},
handler: async (request) => {
const { axios } = request.plugins.goodTracer;
return axios.main.get('https://google.com')
}
}];
exports.routes = server => server.route(routes);
And with Route level configuration
const routes = [{
method: 'GET',
path: '/proxy/google',
config: {
tags: ['proxy'],
plugins: {
goodTracer: {
axios: {
inroute: {}
}
}
}
},
handler: async (request) => {
const { axios } = request.plugins.goodTracer;
return axios.inroute.get('https://google.com')
}
}];
exports.routes = server => server.route(routes);
Consider The following request chain:
The depth
value provides insight into the hierarchy chain of requests. Combining the uuid
+ depth
+ timestamp
of a request will provide a mapping to the request chain.
This plugin uses an in-memory cache that is used to pass the tracer
information between the server, request and logger.
There is a global TTL per object that is reset on each get
of the object.
If an object is stale for the length of the TTL, it will be culled.
There is a max number of keys that can be active at any time to help with memory concerns.
The postResponseCleanup
option is on by default. This will delete the cached data associated with the request on a delay (default of 1000 ms
).
See node-cache for available settings.
When passing a configuration option, it will overwrite the defaults.
traceUUIDHeader
: defaults tox-gg-trace-uuid
. The header that is used for the Trace IDtraceDepthHeader
: defaults tox-gg-trace-depth
. The header that is used for the Depth IDenableStatsRoute
: defaults tofalse
. Publish a route to/good-tracer/stats
that exposes the current metrics fornode-cache
statistics.baseRoute
: defaults to''
. Prepends to the/good-tracer/stats
route.- Example:
baseRoute = /serivce-awesome
results in/serivce-awesome/good-tracer/stats
- Example:
postResponseCleanup: (Boolean | Object)
: defaults totrue
. If set to anything, the feature is enabled.delay: Number
: defaults to1
second. The amount of time to wait after reponse to delete a key from the cache. You can pass decimal values for sub-second times.
axios
: Configured axios instances provided to each request[key: string]: (Boolean | Object)
: if given, defaults to{}
. Pass in any validaxios
config options.
cache
: internal memory cache settings. See node-cache configurationstdTTL
: default3600
seconds (1 hour)checkperiod
: default60
secondsmaxKeys
: default-1
(no limit)useClones
: defaultfalse
extendTTLOnGet
: This feature will reset the TTL to the global TTL when a successfulget
occurs. This will extend the life of an item in the cache as a result. defaulttrue
Route level plugin configuration overwrites the options passed to the plugin.
axios
: Configured axios instances provided to each request[key: string]: (Boolean | Object)
: if given, defaults to{}
. Pass in any validaxios
config options.
To run tests, just run the following:
npm test
All commits are tested on CircleCI
To run eslint
:
npm run lint
To auto-resolve:
npm run lint:fix
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use milestones and npm
version to bump versions. We also employ git-chglog to manage the CHANGELOG.md. For the versions available, see the tags on this repository.
To initiate a version change:
npm version minor
- Derek Smith - Initial work - @clok
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details