Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@
"rules": {
"max-statements": ["off"]
}
},
{
"files": ["**/*.mjs"],
"parserOptions": {
"ecmaVersion": 2019,
"sourceType": "module"
}
}
]
}
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,48 @@ jobs:
run: |
npm run check-integration

test-deno-integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Deno
uses: denoland/setup-deno@v2
with:
deno-version: '~2.1'

- name: Install dependencies
run: deno install

- name: Check Integration
env:
DATADOG_API_KEY: ${{ secrets.DATADOG_API_KEY }}
DATADOG_APP_KEY: ${{ secrets.DATADOG_APP_KEY }}
DATADOG_SITE: ${{ secrets.DATADOG_API_HOST }}
run: |
deno --allow-all test-other/integration_check_deno.ts

test-bun-integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: '1.0.36'

- name: Install dependencies
run: bun install

- name: Check Integration
env:
DATADOG_API_KEY: ${{ secrets.DATADOG_API_KEY }}
DATADOG_APP_KEY: ${{ secrets.DATADOG_APP_KEY }}
DATADOG_SITE: ${{ secrets.DATADOG_API_HOST }}
run: |
bun test-other/integration_check.mjs '-bun'

lint:
runs-on: ubuntu-latest
steps:
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
[![NPM Version][npm-image]][npm-url]
[![Build Status][ci-status-image]][ci-status-url]
[![Downloads Stats][npm-downloads]][npm-url]
![Supports Deno 2.1 and Newer][deno-image]
![Supports Bun 1.0 and Newer][bun-image]

Datadog-metrics lets you collect application metrics through Datadog's HTTP API. Using the HTTP API has the benefit that you **don't need to install the Datadog Agent (StatsD)**. Just get an API key, install the module and you're ready to go.

The downside of using the HTTP API is that it can negatively affect your app's performance. Datadog-metrics **solves this issue by buffering metrics locally and periodically flushing them** to Datadog.

## Installation

Datadog-metrics is compatible with Node.js v14 and later. You can install it with NPM:
Datadog-metrics is compatible with Node.js v14+, Deno 2.1+, and Bun 1.0+. You can install it with NPM:

```sh
npm install datadog-metrics --save
Expand Down Expand Up @@ -371,13 +373,13 @@ Contributions are always welcome! For more info on how to contribute or develop

**Breaking Changes:**

* The minimum required Node.js version is now v14.0.0.
* The minimum required Node.js version is now v14.0.0, Deno version is 2.1.0, and Bun version is 1.0.0.

* The `code` property on `AuthorizationError` instances has been changed to `DATADOG_METRICS_AUTHORIZATION_ERROR` for clarity and consistency (it was previously `DATADOG_AUTHORIZATION_ERROR`). If you are using `errorInstance.code` to check types, make sure to update the string you were looking for.

**New Features:**

TBD
* Clarify this package is compatible with Deno (>= v2.1) and Bun (>= 1.0). We’ve silently worked on Deno and Bun for a long time, but never formally supported them before this release.

**Deprecations:**

Expand Down Expand Up @@ -799,3 +801,5 @@ Your contributions are always welcome! See [`CONTRIBUTING.md`](./CONTRIBUTING.md
[npm-downloads]: https://img.shields.io/npm/dm/datadog-metrics.svg?style=flat-square
[ci-status-image]: https://github.com/dbader/node-datadog-metrics/actions/workflows/ci.yml/badge.svg?branch=main
[ci-status-url]: https://github.com/dbader/node-datadog-metrics/actions/workflows/ci.yml?query=branch%3Amain
[deno-image]: https://img.shields.io/badge/Deno-^2.1-blue?logo=deno&color=70ffaf&logoColor=ffffff
[bun-image]: https://img.shields.io/badge/Bun-^1.0-blue?logo=bun&color=f368e0
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "datadog-metrics",
"version": "0.13.0-dev",
"description": "Buffered metrics reporting via the Datadog HTTP API",
"type": "commonjs",
"main": "index.js",
"types": "dist/index.d.ts",
"repository": {
Expand All @@ -14,7 +15,7 @@
"scripts": {
"prepack": "npm run clean && npm run build-types && npm run check-types",
"test": "mocha --reporter spec",
"check-integration": "node test-other/integration_check.js",
"check-integration": "node test-other/integration_check.mjs",
"check-codestyle": "npx eslint .",
"check-text": "test-other/lint_text.sh",
"build-types": "tsc --build",
Expand Down
12 changes: 12 additions & 0 deletions test-other/integration_check.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* A basic test of our complete integration with Datadog. This check sends some
* metrics, then queries to make sure they actually got ingested correctly by
* Datadog and will show up as expected.
*/

import { main } from './integration_check_lib.mjs';

main({ tagSuffix: process.argv[2] || '' }).catch(error => {
process.exitCode = 1;
console.error(error);
});
12 changes: 12 additions & 0 deletions test-other/integration_check_deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* A basic test of our complete integration with Datadog. This check sends some
* metrics, then queries to make sure they actually got ingested correctly by
* Datadog and will show up as expected.
*/

import { main } from './integration_check_lib.mjs';

main({ tagSuffix: '-deno' }).catch(error => {
process.exitCode = 1;
console.error(error);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
* Datadog and will show up as expected.
*/

'use strict';

const { client, v1 } = require('@datadog/datadog-api-client');
const datadogMetrics = require('..');
import { client, v1 } from '@datadog/datadog-api-client';
import datadogMetrics from '../index.js';

function floorTo(value, points) {
const factor = 10 ** points;
Expand Down Expand Up @@ -46,7 +44,13 @@ const testMetrics = [
},
];

async function main() {
export async function main({ tagSuffix = '' } = {}) {
if (tagSuffix) {
for (const metric of testMetrics) {
metric.tags = metric.tags.map(tag => `${tag}${tagSuffix}`);
}
}

datadogMetrics.init({ flushIntervalSeconds: 0 });

for (const metric of testMetrics) {
Expand All @@ -64,18 +68,16 @@ async function main() {
}
}

async function sendMetric(metric) {
export async function sendMetric(metric) {
console.log(`Sending random points for ${metric.type} "${metric.name}"`);

for (const [timestamp, value] of testPoints) {
datadogMetrics[metric.type](metric.name, value, metric.tags, timestamp);
await new Promise((resolve, reject) => {
datadogMetrics.flush(resolve, reject);
});
await datadogMetrics.flush();
}
}

async function queryMetric(metric) {
export async function queryMetric(metric) {
const configuration = client.createConfiguration({
authMethods: {
apiKeyAuth: process.env.DATADOG_API_KEY,
Expand All @@ -95,7 +97,7 @@ async function queryMetric(metric) {
return data.series && data.series[0];
}

async function waitForSentMetric(metric) {
export async function waitForSentMetric(metric) {
const endTime = Date.now() + MAX_WAIT_TIME;
while (Date.now() < endTime) {
console.log(`Querying Datadog for sent points in ${metric.type} "${metric.name}"...`);
Expand Down Expand Up @@ -131,10 +133,3 @@ async function waitForSentMetric(metric) {
console.log('✘ Nothing found and gave up waiting. Test failed!');
return false;
}

if (require.main === module) {
main().catch(error => {
process.exitCode = 1;
console.error(error);
});
}