Madgex Datadog Logging and Monitoring
All your Hapi + Datadog needs, in one handy package.
Usage
As a library
When used as a module, this library exports hapi-pino and a function to set up dd-trace. You can use one or both of these in the setup file for your server like so:
const { pino, trace } = require('@madgex/datadog-monitoring');
async function createServer() {
await trace({
hostname: DD_AGENT_HOSTNAME || '',
service: 'my-service-name',
hapiOptions: {
blacklist: ['/healthcheck']
},
});
await server.register([
{
plugin: pino,
options: {
prettyPrint: IS_DEV,
level: LOG_LEVEL,
redact: ['req.headers.authorization'],
ignorePaths: ['/healthcheck'],
},
},
]);
return server;
}
All available options for the dd-trace Hapi plugin can be passed as hapiOptions
. hostname
, if not set, will default to the discoverable Datadog agent host on AWS. The trace
function returns the tracer instance so further plugin configuration can be added if you wish, eg:
async function createServer() {
const tracer = await trace({
hostname: DD_AGENT_HOSTNAME || '',
service: 'my-service-name',
debug: true
hapiOptions: {
blacklist: ['/healthcheck']
},
});
tracer.use('redis', { analytics: true });
return server;
}
The hapi-pino plugin should be set up as described in its documentation.
From the command line
This library also includes a custom transport to pipe Pino logs from a server's stdout to a Datadog agent over UDP, transforming the JSON format for processing and display. Optionally it can also echo the transformed messages to stdout- however this flag should not be used in production. It should be used in your npm scripts like so:
"start": "dd-monitor /path/to/server.js --hostname [hostname] --port [port] --echo --debug"
Hostname and port relate to the Datadog agent to which the transport should transmit logs, and are optional. If not set, hostname will default first to a DD_AGENT_HOSTNAME environment variable, and then to looking up the discoverable host on AWS. Port will default to a Logging__DataDogLoggingPort environment variable, but the CLI tool will throw an error and exit if no port number can be found. Passing the debug flag will additionally log when a packet is transmitted to the UDP socket; this should also not be enabled in production.