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 🙏

© 2024 – Pkg Stats / Ryan Hefner

seek-datadog-custom-metrics

v4.6.4

Published

Helpers for sending Datadog custom metrics

Downloads

15,732

Readme

🐶 Datadog Custom Metrics

GitHub Release GitHub Validate Node.js version npm package Powered by skuba

Helpers for sending Datadog custom metrics via hot-shots.

yarn add seek-datadog-custom-metrics

Tagging convention

All custom metrics are prefixed by AppConfig.name. Two global tags are also added to every custom metric:

  • AppConfig.environment becomes env:${value}
  • AppConfig.version becomes version:${value}

These tags are consistent with tags sent by Gantry via Datadog's AWS integration.

API reference

createStatsDClient

createStatsDClient creates a hot-shots client configured with our tagging convention. This is intended for containerized services, particularly those deployed with Gantry.

import { StatsD } from 'hot-shots';
import { createStatsDClient } from 'seek-datadog-custom-metrics';

// Expects `name`, `version`, `environment` and `metricsServer` properties
import config from '../config';

// This example assumes Bunyan/pino
import { rootLogger } from '../logger';
const errorHandler = (err: Error) => {
  rootLogger.error('StatsD error', err);
};

// Returns a standard hot-shots StatsD instance
const metricsClient = createStatsDClient(StatsD, config, errorHandler);

createLambdaExtensionClient

createLambdaExtensionClient creates a Lambda extension client. This is intended for AWS Lambda functions and is a replacement for createCloudWatchClient.

This client will only submit metrics as a distribution which enables globally accurate aggregations for percentiles (p50, p75, p90, etc).

import { createLambdaExtensionClient } from 'seek-datadog-custom-metrics';

// Expects `name` and `metrics` properties
import config from '../config';

// Returns a standard hot-shots StatsD instance
const { metricsClient, withLambdaExtension } =
  createLambdaExtensionClient(config);

export const handler = withLambdaExtension((event, _ctx) => {
  try {
    logger.info('request');

    await lambdaFunction(event);
  } catch (err) {
    logger.error({ err }, 'request');

    metricsClient.increment('invocation_error');

    throw new Error('invoke error');
  }
});

createNoOpClient

createNoOpClient returns a no-op client. This is intended for use where a MetricsClient interface is expected but you do not wish to provide one, e.g in tests.

import { createNoOpClient } from 'seek-datadog-custom-metrics';

// Returns a `MetricsClient` subset of the full StatsD interface
const metricsClient = createNoOpClient();

createTimedSpan

createTimedSpan wraps an asynchronous closure and records custom Datadog metrics about its performance. This is intended as a lightweight alternative to APM where nested spans aren't required.

import { createTimedSpan } from 'seek-datadog-custom-metrics';

// Takes a StatsD instance or `MetricsClient`
const timedSpan = createTimedSpan(metricsClient);

const loadPrivateKey = async (): Promise<PrivateKey> =>
  await timedSpan(
    // Prefix for the custom metrics
    'secrets.load_private_key',
    // Closure to be timed
    () => client.getSecretValue({ SecretId }).promise(),
  );

httpTracingConfig

The dd-trace package can instrument your application and trace its outbound HTTP requests. However, its emitted trace.http.request metric only captures the HTTP method against the resource.name tag, which is not useful if your application makes HTTP requests to multiple resources and you want to inspect latency by resource.

This configuration object adds a hook to replace the resource.name with a HTTP method and semi-normalised URL. For example, if your application makes the following HTTP request:

PUT https://www.example.com/path/to/123?idempotencyKey=c1083fb6-519c-42bf-8619-08dfd6229954

The trace.http.request metric will see the following tag change:

- resource_name:put
+ resource_name:put_https://www.example.com/path/to/number?idempotencyKey=uuid

Apply the configuration object where you bootstrap your application with the Datadog tracer:

import { httpTracingConfig } from 'seek-datadog-custom-metrics';

// DataDog/dd-trace-js#1118
datadogTracer?.use('http', httpTracingConfig);

This configuration may be superseded in future if the underlying dd-trace implementation is corrected.

DataDog/dd-trace-js#1118