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

@gymshark/datadog-client

v0.3.1

Published

An opinionated wrapper for instantiating client to push metrics and logs to DataDog

Downloads

80

Readme

DataDog Client JS

This is an opinionated client for integrating with DataDog directly for logging and metrics. This package is designed to be a wrapper for simplifying the instantiation of existing package implementations.

Build and Test Publish Package Codacy Badge Codacy Badge

Requirements

  • Node 18.x or higher
  • Datadog Account and have generated an API Key

Installation

npm install @gymshark/datadog-client

or

yarn add @gymshark/datadog-client

Usage

Logger Client

The logging client returns winston logging client that includes a HTTP transport for sending logs to DataDog. Some of the winston logger options are preset to ensure that logs are sent to DataDog in a JSON format, but any of the winston configuration can be overridden.

The logger is configured to have 2 transports:

  1. Console Transport to log to the console
  2. HTTP Transport to configured to send logs to DataDog

Using require to import dependencies

const client = require('@gymshark/datadog-client');

const logger = client.logger({
    apiKey: 'dd-api-key',
    service: 'test'
});

logger.info('Hello World');

Using import to import dependencies

import  client from '@gymshark/datadog-client';

const logger = client.logger({
    apiKey: 'dd-api-key',
    service: 'test'
});

logger.info('Hello World');

Using require to import dependencies

const client = require('@gymshark/datadog-client');

const logger = client.logger({
    apiKey: 'dd-api-key',
    service: 'test',
    ddRegion: 'eu'
});

logger.info('Hello World');

Using import to import dependencies

import  client from '@gymshark/datadog-client';

const logger = client.logger({
    apiKey: 'dd-api-key',
    service: 'test',
    ddRegion: 'eu'
});

logger.info('Hello World');

Using require to import dependencies

const client = require('@gymshark/datadog-client');
const winston = require('winston');

const logger = client.logger({
    apiKey: 'dd-api-key',
    service: 'test',
    ddRegion: 'eu',
    tags: 'env:develop,app:this-service,version:1.0.0',
    level: `debug`,
    format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.json()
    ),
  additionalTransports: [
        new winston.transports.File({ filename: 'error.log', level: 'error' })
    ]
});

logger.info('Hello World');

Using import to import dependencies

import  client from '@gymshark/datadog-client';
import winston from 'winston';

const logger = client.logger({
    apiKey: 'dd-api-key',
    service: 'test',
    ddRegion: 'eu',
    tags: 'env:develop,app:this-service,version:1.0.0',
    level: `debug`,
    format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.json()
    ),
  additionalTransports: [
        new winston.transports.File({ filename: 'error.log', level: 'error' })
    ]
});
logger.info('Hello World');

Logger Options

  • apiKey - Your DataDog API Key [required]
  • service - The name of the application or service generating the logs [required]
  • ddRegion - The region yourDataDog account is in. This is used to determine the DataDog domain.
  • tags - A comma separated string of tags to be included with each log entry when sending to Datadog
  • source - The source of the logs, typically the technology the log originated. See reserved attributes. Defaults to nodejs
  • additionalTransports - Any additional winston transports to be added to the logger. Setting transports in the options will result in the default transports been overridden.
  • Any winston logger options
    • Some are defaulted to the following:
      • level - info
      • format - winston.format.json()
      • exitOnError - false
      • transports - Console Transport and HTTP Transport

Metric Client

The metric client instantiation either returns datadog-metrics metric client with some additional logic for handling whether sampling of metrics. The sampling logic will take a unique key and a sampling rate, the key will be hashed and the hash will be used to determine if the metric should be sent to DataDog.

Using require to import dependencies

const client = require('@gymshark/datadog-client');

const metrics = client.metrics({
  apiKey: 'dd-api-key',
  prefix: 'test'
});

metrics.increment('api.call', 1, ['env:develop']);

Using import to import dependencies

import client from '@gymshark/datadog-client';

const metrics = client.metrics({
    apiKey: 'dd-api-key',
    prefix: 'test'
});

metrics.increment('api.call', 1, ['env:develop']);

Metric Options

  • apiKey - Your DataDog API Key [required]
  • prefix - The prefix to be added to all metrics [required]
  • ddRegion - The region your DataDog account is in. This is used to determine the DataDog domain.
  • samplingEnabled - A boolean to determine if sampling should be enabled. Defaults to false
  • samplingKey - A unique string to be used to determine if the metric should be sampled. [required if samplingEnabled is true]
    • The samplingKey is required in cases where you need to instantiate the metric client multiple times in different sandboxes, and you want to ensure that the same sampling setup is provided for each sandbox.
    • And example use case of this is Auth0 actions, where you may have multiple actions running in the same environment, and you want to ensure that the same sampling rate is applied to each action.
  • samplingRate - The rate at which metrics should be sampled. This value is a percentage representation of the rate. Defaults to 0
  • Any datadog-metrics options