npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@madgex/datadog-monitoring

v6.0.0

Published

All your Hapi + Datadog needs, in one handy package.

Downloads

642

Readme

Madgex Datadog Logging and Monitoring

All your Hapi + Datadog needs, in one handy package.

Install

npm install @madgex/datadog-monitoring

Install peer dependencies alongside:

npm install hot-shots dd-trace

Exports

| Path | Exports | |---|---| | @madgex/datadog-monitoring | trace, pino, autoLogErrors | | @madgex/datadog-monitoring/statsd | plugin, addToVisionContext | | @madgex/datadog-monitoring/utils | getAWSAgent |

Usage

Tracing

The tracer must be initialised before requiring Hapi to correctly instrument the APM.

const { trace } = require('@madgex/datadog-monitoring');

const tracer = await trace({
  hostname: DD_AGENT_HOSTNAME || '',
  service: 'my-service-name',
  hapiOptions: { blacklist: ['/healthcheck'] },
});

hostname, if not set, defaults to the discoverable Datadog agent host on AWS. All dd-trace Hapi plugin options can be passed as hapiOptions. The returned tracer instance allows further plugin configuration.

Logging

const { pino, autoLogErrors } = require('@madgex/datadog-monitoring');

await server.register([
  { plugin: pino, options: { level: LOG_LEVEL, redact: ['req.headers.authorization'] } },
  { plugin: autoLogErrors, options: { level: LOG_LEVEL, threshold: 399 } },
]);
  • level — log level, defaults to 'info'
  • threshold — status code above which to log as 'warn', defaults to 399

StatsD

const { plugin } = require('@madgex/datadog-monitoring/statsd');

await server.register({
  plugin,
  options: {
    host: DD_AGENT_HOSTNAME,  // required
    mock: false,              // default
    // all hot-shots options are passed through:
    port: 8125,
    prefix: 'myapp.',
    globalTags: ['env:production'],
  },
});

host is required. On AWS, use getAWSAgent() to discover the agent hostname:

const { getAWSAgent } = require('@madgex/datadog-monitoring/utils');
const host = await getAWSAgent();

The plugin decorates both server and request with a statsDClient:

server.statsDClient.increment('page.view', 1, ['route:home']);
request.statsDClient.histogram('api.latency', responseTime);

See hot-shots docs for supported options and methods.

Manifest.js (hapipal/boilerplate + haute-couture)

If you use hapipal/boilerplate, pass statsd options to your main ../lib plugin in manifest.js, then wire a loader in lib/plugins/statsd.js.

// server/manifest.js
register: {
  plugins: [
    {
      plugin: '../lib',
      options: {
        statsd: {
          host: {
            $filter: { $env: 'NODE_ENV' },
            test: {
              $env: 'DD_AGENT_HOSTNAME',
              $default: 'localhost',
            },
            $default: {
              $env: 'DD_AGENT_HOSTNAME',
              $default: null,
            },
          },
          port: {
            $env: 'DD_AGENT_DSTATS_PORT',
            $coerce: 'number',
            $default: 8125,
          },
          mock: {
            $filter: { $env: 'NODE_ENV' },
            $default: true,
            production: false,
          },
          prefix: 'my_service_',
        },
      },
    },
  ],
}
// lib/plugins/statsd.js
const { plugin } = require('@madgex/datadog-monitoring/statsd');
const { getAWSAgent } = require('@madgex/datadog-monitoring/utils');

module.exports = async (_server, incomingOptions) => {
  const options = { ...incomingOptions?.statsd };

  if (!options.host && !options.mock) {
    options.host = await getAWSAgent();
  }

  return { plugin, options };
};

This keeps metrics calls simple (server.statsDClient / request.statsDClient always decorated) while only sending metrics when mock is false.

Template metrics with addToVisionContext

addToVisionContext adds metric.increment, metric.histogram, and metric.timing to template context. Each returns '' for safe inline usage.

const { addToVisionContext } = require('@madgex/datadog-monitoring/statsd');

// In your view-manager / vision context function:
context = addToVisionContext(request, context);

In Nunjucks templates:

{{ metric.increment('page.rendered', ['theme:dark']) }}
{{ metric.timing('widget.load', loadTimeMs) }}

CLI

Pipe Pino logs from stdout to a Datadog agent over UDP:

"start": "dd-monitor /path/to/server.js --hostname [hostname] --port [port]"

Flags: --hostname/-h, --port/-p (required), --echo/-e, --debug/-d. Do not enable echo/debug in production.

Migrate statsd from pre-v6 versions of @madgex/datadog-monitoring

1. Install peers

npm install hot-shots dd-trace

2. Update imports

- const { statsDClient, trace, pino, autoLogErrors } = require('@madgex/datadog-monitoring');
+ const { trace, pino, autoLogErrors } = require('@madgex/datadog-monitoring');
+ const { plugin: statsdPlugin } = require('@madgex/datadog-monitoring/statsd');

If you import getAWSAgent from an internal path, switch to the public export:

- const { getAWSAgent } = require('@madgex/datadog-monitoring/src/utils');
+ const { getAWSAgent } = require('@madgex/datadog-monitoring/utils');

3. Update plugin options

  await server.register({
    plugin: statsdPlugin,
    options: {
-     DD_AGENT_HOSTNAME: hostname,
-     DD_AGENT_DSTATS_PORT: port,
+     host: hostname,  // required
+     port: port,
      mock: false,
    },
  });

host is now required — the plugin no longer falls back to AWS metadata. Call getAWSAgent() yourself if needed.

4. Template metrics (optional)

If you have a custom nunjucks.addGlobal('metric', ...), replace it with addToVisionContext:

- nunjucks.addGlobal('metric', function (name, ...args) {
-   const request = this.ctx.request;
-   if (request && request.statsDClient) {
-     try { request.statsDClient.increment(name, ...args); }
-     catch (err) { /* ... */ }
-   }
-   return '';
- });

+ const { addToVisionContext } = require('@madgex/datadog-monitoring/statsd');
+ // In your vision context function:
+ context = addToVisionContext(request, context);

Templates change from {{ metric('name', ['tag']) }} to {{ metric.increment('name', ['tag']) }}.

Development

Tests

npm test
npm run test:integration

Linting

npm run lint