diff --git a/.eslintrc.json b/.eslintrc.json index 8d437c7..e457dc8 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -48,6 +48,13 @@ "rules": { "max-statements": ["off"] } + }, + { + "files": ["**/*.mjs"], + "parserOptions": { + "ecmaVersion": 2019, + "sourceType": "module" + } } ] } diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fb9c53f..4f88945 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/README.md b/README.md index 13c6f72..44654b0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ [![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. @@ -11,7 +13,7 @@ The downside of using the HTTP API is that it can negatively affect your app's p ## 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 @@ -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:** @@ -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 diff --git a/package.json b/package.json index 3c44beb..e0fa85a 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/test-other/integration_check.mjs b/test-other/integration_check.mjs new file mode 100644 index 0000000..d57f847 --- /dev/null +++ b/test-other/integration_check.mjs @@ -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); +}); diff --git a/test-other/integration_check_deno.ts b/test-other/integration_check_deno.ts new file mode 100644 index 0000000..d4bc933 --- /dev/null +++ b/test-other/integration_check_deno.ts @@ -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); +}); diff --git a/test-other/integration_check.js b/test-other/integration_check_lib.mjs similarity index 88% rename from test-other/integration_check.js rename to test-other/integration_check_lib.mjs index a9c884a..df29d31 100644 --- a/test-other/integration_check.js +++ b/test-other/integration_check_lib.mjs @@ -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; @@ -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) { @@ -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, @@ -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}"...`); @@ -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); - }); -}